-2
a = [1,1,0,0,0,'yes',1,1,0]

b = [1,1,0,0,0,'yes',0,1,1]

pattern = ['yes',1,1] #The main lists a and b should check for the pattern in the same order

I'm expecting an output like:

pattern in a - should give 'Yes' or True

pattern in b - should give 'No' or False

Merging the values in the list to form 1 string and checking with an if - in condition is not the path I'm looking for.

jpp
  • 159,742
  • 34
  • 281
  • 339
nisha21_m
  • 11
  • 2

2 Answers2

2

You can use any with a generator comprehension and list slicing:

a = [1,1,0,0,0,'yes',1,1,0]
b = [1,1,0,0,0,'yes',0,1,1]
pattern = ['yes',1,1]

def comparer(L, p):
    n = len(p)
    return any(L[i:i+n] == p for i in range(len(L)-n))

comparer(a, pattern)  # True
comparer(b, pattern)  # False
jpp
  • 159,742
  • 34
  • 281
  • 339
0
>>> a = [1,1,0,0,0,'yes',1,1,0]
>>> b = [1,1,0,0,0,'yes',0,1,1]
>>> pattern = ['yes',1,1]
>>> 
>>> tuple(pattern) in zip(*[a[i:] for i in range(len(pattern))])
True
>>> 
>>> tuple(pattern) in zip(*[b[i:] for i in range(len(pattern))])
False
>>> 
Sunitha
  • 11,777
  • 2
  • 20
  • 23
  • No need for the intermediary list creation: `tuple(pattern) in zip(*(a[i:] for i in range(len(pattern))))`. – jpp Jul 19 '18 at 18:17