a = [1,1,0,0,0,'yes',1,1,0]
b = [1,1,0,0,0,'yes',0,1,1]
pattern = ['yes',1,1]
#主列表a和b应该以相同顺序检查模式
我期望输出如下:
-中的模式应为“是”或“真”
b中的模式-应该给出'否'或False
将列表中的值合并为1个字符串,并检查if-in条件不是我要查找的路径。
答案 0 :(得分:2)
您可以将any
用于生成器理解和列表切片:
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
答案 1 :(得分: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
>>>