我想检查“ check”内的所有数字是否都在给定的数组“ lists”内,诸如此类:
import sys
lists = [1,2,8,8,8,8,8,8,8,8,8,8]
check = [1,2,3]
for check in lists:
print True
sys.exit()
print False
但是,除非我使列表= [],否则此命令始终打印True。有谁知道这将如何工作?预先感谢!
答案 0 :(得分:1)
更新后的工作
s = [True if lists[idx: idx+3] == check else False for idx, item in enumerate(lists[:-2])]
if any(s):
print(True)
else:
print(False)
答案 1 :(得分:0)
它一直在打印True
,因为您正在循环内打印True
,如果列表为[]
,则由于其为空而无法进行迭代,因此请尝试: / p>
print(all(i in lists for i in check))
所以整个代码是:
lists = [1,2,8,8,8,8,8,8,8,8,8,8]
check=[1,2,3]
print(all(i in lists for i in check))
更新:
print(all(i in lists for i in check) and [lists.index(i) for i in check]==range([lists.index(i) for i in check][0],[lists.index(i) for i in check][-1]+1))