简体   繁体   English

Python列表列表中的所有项目为true

[英]Python all items in list of lists true

Made a table of True & False, and all I'd like to do is check if they are all True. 制作一张True&False表,我要做的就是检查它们是否都是True。 Used all() successfully before, but for some reason with the below I fail miserably. 之前成功使用过all(),但是由于以下原因,我惨遭失败。

data = [[False, False, False], 
[False, False, False], 
[True, True, True], 
[True, True, True]]

print(all(data))
>>> True

Why is this happening? 为什么会这样呢?

all does not check the bools in each sublist. all不检查每个子列表中的布尔值。 Each of the non-empty lists are all truthy. 每个非空列表都是真实的。

To check that all the items in all sublists are True , you should do: 要检查所有子列表中的所有项目均为True ,您应该执行以下操作:

all(x for lst in data for x in lst) # -> False

You can pass each sublist to the all function within all using a generator: 您可以将每个子列表传递给all函数内all使用发电机:

print(all(all(i) for i in data))

Output: 输出:

False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM