Find starting and ending indices of sublist in list

和自甴很熟 提交于 2019-11-27 23:07:05

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]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!