I have a list:
greeting = ['hello','my','name','is','bob','how','are','you']
I want to define a function that will find the first and last index of a sublist in this list. Thus:
find_sub_list(['my','name','is'], greeting)
should return:
1, 3
Suggestions?
If you want multiple matches, this works:
greeting = ['hello','my','name','is','bob','how','are','you','my','name','is']
def find_sub_list(sl,l):
results=[]
sll=len(sl)
for ind in (i for i,e in enumerate(l) if e==sl[0]):
if l[ind:ind+sll]==sl:
results.append((ind,ind+sll-1))
return results
print find_sub_list(['my','name','is'], greeting)
# [(1, 3), (8, 10)]
Or if you just want the first match:
greeting = ['hello','my','name','is','bob','how','are','you','my','name','is']
def find_sub_list(sl,l):
sll=len(sl)
for ind in (i for i,e in enumerate(l) if e==sl[0]):
if l[ind:ind+sll]==sl:
return ind,ind+sll-1
print find_sub_list(['my','name','is'], greeting)
# (1, 3)
Slice the list:
>>> greeting[0:3]
['hello', 'my', 'name']
>>> greeting[1:4]
['my', 'name', 'is']
>>> greeting[1:4] == ['my','name','is']
True
This should get your started:
for n in range(len(greeting) - len(sub_list) + 1):
...
If you're sure that your list will always be in your sublist you can just do:
def find_sub_list(sub_list,this_list):
return (this_list.index(sub_list[0]),len(sub_list))
If you want to be checking the items in the sublist exist in the list then use:
def find_sub_list(sub_list,this_list):
if set(sub_list).issubset(set(this_list)):
return(this_list.index(sub_list[0]),len(sub_list))
else:
return False
Lastly, if the order of the items in the sub_list is also going to be unknown then use this:
def find_sub_list(sub_list,this_list):
if sub_list[0] in this_list:
for i,item in enumerate(sub_list[1:]):
if item not in this_list[this_list.index(sub_list[i]):]:
return False
return(this_list.index(sub_list[0]),len(sub_list))
Now, the items have to be in the right order for the function not to return false.
Following is a solution if only the indices of the first and the last entry are to be returned:
def find_sub_list(subl, l):
ind_subl = [i for i in range(len(l)) if l[i] in subl]
return [ind_subl[0], ind_subl[-1]]
print find_sub_list(['my', 'name', 'is'], greeting)
# [1, 3]