python - compare a subset of a list and return where it exists inside the list -
i have 2 lists , b, b small subset somewhere in a.
b = ['apple','banana','carrot'] = list of length 100 somewhere contains b @ indices 12,13,14.
i search b in , return indices 12,13,14.
my current idea 2 nested loops pattern hoping there cleaner/easier solution.
here simple possibility:
b = ['a', 'b', 'c'] = ['t', 'z', 'd', 'a', 'b', 'c', 't', 's', 'a', 'b'] [i in range(len(a)) if a[i:i+len(b)] == b]
output: [3]
returns index of first element of list b
in list a
. notice if b
repeated more once inside a
, approach return 2 indexes b
repeated in a
.
Comments
Post a Comment