Find indexes of sequence in list in python

后端 未结 2 1119
眼角桃花
眼角桃花 2020-12-06 07:46

I am quite new and I hope it\'s not too obvious, but I just can\'t seem to find a short and precise answer to the following problem.

I have two lists:



        
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 08:20

    With a list comprehension:

    >>> [(i, i+len(b)) for i in range(len(a)) if a[i:i+len(b)] == b]
    [(3, 6)]
    

    Or with a for-loop:

    >>> indexes = []
    >>> for i in range(len(a)):
    ...    if a[i:i+len(b)] == b:
    ...        indexes.append((i, i+len(b)))
    ... 
    >>> indexes
    [(3, 6)]
    

提交回复
热议问题