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:
a = [2,3,5,2,5,6,7,2]
b = [2,5,6]
I would like to find when all the indexes of the second list (b
) are in the first list (a
), so that I get something like this:
indexes of b in a: 3, 4, 5
or b = a[3:6]
With a list comprehension:
Or with a for-loop:
This should do what you are asking:
Of course you should format the print statement to your own liking. Or don't print and save result in, say, another list.
Good luck!
Also, for efficiency, you can use KMP algorithm that is used in string matching (from here):
This find the first index of the
b
ina
. Hence, the range is the result of the search and its plus to the length ofb
.