are elements of a list in sequence in list b
Arnaud Delobelle
arnodel at googlemail.com
Fri Feb 8 15:08:06 EST 2008
On Feb 8, 5:06 pm, Paul Hankin <paul.han... at gmail.com> wrote:
> Matthew_WAR... at bnpparibas.com wrote:
> > I need to search list a for the sequence of list b
>
> def list_contains(a, b):
> return any(a[i:i+len(b)] == b for i in range(len(a) - len(b) + 1))
>
> list_contains(range(1, 7), [2, 3, 4])
>
> --
> Paul Hankin
This is more careful but not necessarilly faster in python:
def issubseq(s, l):
indices = [0]
try:
for x in l:
indices = [i+1 for i in indices if s[i] == x]
indices.append(0)
return indices[0] == len(s)
except IndexError:
return True
issubseq('m&e', 'spam&eggs)
--
Arnaud
More information about the Python-list
mailing list