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 __'等長鏈。
3 回答

忽然笑
TA貢獻1806條經驗 獲得超5個贊
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
TA貢獻1817條經驗 獲得超6個贊
您可以串聯/合并所有列表并檢查真實性:
while not all(first+second+third+fourth):
這將檢查任何False-y值,如果為0,則返回True。
添加回答
舉報
0/150
提交
取消