NoobyD NoobyD - 7 days ago 5
Python Question

how to match an exact match in a list python

How can I check to see if an exact match of B appears in A in the same consecutive order? In the the below example 99, 3, 2 are in A in that exact way:

A = [0, 3, 123, 0, 99, 3, 2, 1, 2, 33, 1, 76]

B = [99, 3, 2]

An example of A which would fail is:

A = [0, 321, 99, 0, 3, 0, 2, 0]

As 99, 3, 2 elements do not appear consecutively.

I have tried doing

if B in A:
print("yes")
else:
print("NO")


This fails.

Thank you all,
Jemma

Answer
def consecutive_in(B,A):
    return B in (A[i:i+len(B)] for i in range(len(A)))
Comments