How do I create a function sublist()
that takes two lists, list1
and list2
, and returns True
if list1
is a s
def sublist(a, b):
"if set_a is not subset of set_b then obvious answer is False"
if not set(a).issubset(set(b)):
return False
n = 0
for i in a:
if i in b[n:]:
"+1 is needed to skip consecutive duplicates, i.e. sublist([2,1,1],[2,1]) = False"
"if +1 is absent then sublist([2,1,1],[2,1]) = True"
"choose to use or not to use +1 according to your needs"
n += b[n:].index(i) + 1
else:
return False
return True
How about this: let's approach this from the other side:
def sublist(a,b):
"""returns True if a is contained in b and in the same order"""
return a == [ch for ch in b if ch in a]
This will fail in some circumstances (e.g. should [1,2,3]
be a subset of [1,1,8,2,3]
) but it's hard to say exactly how you want this implemented...
here is a better solution using regex
:
import re
def exiSt(short,long):
r = ''.join(["(.*"+str[x]+")" for x in short])
return re.match(r,','.join([str(x) for x in long])) == None
long = [1, 2, 3, 4, 5]
short1 = [1,2,5]
short2 = [1,5,3]
exiSt(short1,long)
>> True
exiSt(short2,long)
>> False
Here's one way to do it in linear time (and constant space) with an iterator:
def sublist(a, b):
seq = iter(b)
try:
for x in a:
while next(seq) != x: pass
else:
return True
except StopIteration:
pass
return False
Basically it goes through each element of the sublist, and sees if it can find that same element in the part of the complete list it hasn't looked at yet. If it makes it through the entire sublist it means we have a match (hence the else statement on the for loop). If we run out of elements to look at in the complete list, it means we don't have a match.
Edit: I have updated my solution so it works with Python 3. For Python 2.5 and older, next(seq)
needs to be replaced with seq.next()
.