简体   繁体   English

确定列表列表中的所有值是真还是假

[英]Determining if all values in a list of lists are true or false

So basically I have a function that creates a list of lists, lets say I define 3 values to be in my list and each of those 3 values has a list of their own that contains 2 values, their number, and their availability, like so:所以基本上我有一个创建列表列表的函数,假设我定义了 3 个值在我的列表中,这 3 个值中的每一个都有一个自己的列表,其中包含 2 个值、它们的数量和它们的可用性,就像这样:

[['Product 0', False, ], ['Product 1', False, ], ['Product 2', False,]]

Basically, I want to determine whether all values for availability are True or False , and I cannot seem to get it to work with all() as it apparently does not have to capability to check for values of lists inside of a list.基本上,我想确定可用性的所有值是True还是False ,而且我似乎无法让它与all()因为它显然没有能力检查列表中列表的值。

You can use list comprehension for this.您可以为此使用列表理解。 Iterate the inner lists and extract the second value (True\\False).迭代内部列表并提取第二个值 (True\\False)。 Then use all to check all the values.然后使用all检查所有值。

x = [['Product 0', False, ], ['Product 1', False, ], ['Product 2', False,]]

AllTrue  = all([e[1] for e in x])      # False
AllFalse = all([not e[1] for e in x])  # True

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

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