Python: find a list within members of another list(in order)

前端 未结 9 2120
栀梦
栀梦 2020-12-03 10:40

If I have this:

a=\'abcdefghij\'
b=\'de\'

Then this finds b in a:

b in a => True

Is there a way of doi

9条回答
  •  被撕碎了的回忆
    2020-12-03 11:19

    I suspect there are more pythonic ways of doing it, but at least it gets the job done:

    l=list('abcdefgh')
    pat=list('de')
    
    print pat in l # Returns False
    print any(l[i:i+len(pat)]==pat for i in xrange(len(l)-len(pat)+1))
    

提交回复
热议问题