有什么办法可以使这小段代码更短?(Python)

first  = [0, 0, 0, 0]

second = [0, 0, 0, 0]

third  = [0, 0, 0, 0]

fourth = [0, 0, 0, 0]


while 0 in first and 0 in second and 0 in third and 0 in fourth:

顶部的列表以每个值保持为0开头。随着程序的进行,我计划将列表中的值从0更改为其他数字。有效地,我想知道是否有方法可以重写'while'语句以检查0是否在任何列表中,而没有'while not in __ and not in __'等长链。


DIEA
浏览 152回答 3
3回答

忽然笑

while all(0 in i for i in [first,second,third,fourth]):    ...如果要检查列表中是否包含0,请执行以下操作:while any(0 in i for i in [first,second,third,fourth]):    ...

慕的地6264312

您可以串联/合并所有列表并检查真实性:while not all(first+second+third+fourth):这将检查任何False-y值,如果为0,则返回True。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python