define 'in' operator on lists
Duncan Booth
duncan.booth at invalid.invalid
Mon Oct 11 06:22:48 EDT 2004
..:: sjf ::.. wrote:
> I want to define 'in' operator (or any "magic" function) on list which
> returns True value if all of list (i.e. _list) elements appears in other
> list (i.e. L) in the same order and False otherwise.
How about:
>>> def containedinsequence(short, long):
ilong = iter(long)
for s in short:
for l in ilong:
if s==l:
break
else:
return False # ran out of long list
return True
>>> L = [1, 2, 3, 4, 5, 6]
>>> containedinsequence([3, 4, 6], L)
True
>>> containedinsequence([3, 4, 7], L)
False
>>> containedinsequence([3, 4, 3], L)
False
>>> containedinsequence([], L)
True
>>>
More information about the Python-list
mailing list