Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match an exact match in a list python

Tags:

python

list

How can I check to see if an exact match of B appears in A in the same consecutive order? In 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

like image 542
NoobyD Avatar asked Dec 04 '25 11:12

NoobyD


1 Answers

def consecutive_in(B,A):
    return B in (A[i:i+len(B)] for i in range(len(A)))
like image 171
exprosic Avatar answered Dec 07 '25 01:12

exprosic