简体   繁体   English

什么是逻辑上结合布尔列表的最“pythonic”方式?

[英]What is the most 'pythonic' way to logically combine a list of booleans?

I have a list of booleans I'd like to logically combine using and/or. 我有一个布尔列表我想用逻辑组合使用和/或。 The expanded operations would be: 扩展的业务将是:

vals = [True, False, True, True, True, False]

# And-ing them together
result = True
for item in vals:
    result = result and item

# Or-ing them together
result = False
for item in vals:
    result = result or item

Are there nifty one-liners for each of the above? 上面的每一个都有漂亮的单行吗?

See all(iterable) : 查看all(iterable)

Return True if all elements of the iterable are true (or if the iterable is empty). 如果iterable的所有元素都为true(或者iterable为空),则返回True

And any(iterable) : any(iterable)

Return True if any element of the iterable is true. 如果iterable的任何元素为True则返回True If the iterable is empty, return False . 如果iterable为空,则返回False

The best way to do it is with the any() and all() functions. 最好的方法是使用any()all()函数。

vals = [True, False, True, True, True]
if any(vals):
   print "any() reckons there's something true in the list."
if all(vals):
   print "all() reckons there's no non-True values in the list."
if any(x % 4 for x in range(100)):
   print "One of the numbers between 0 and 99 is divisible by 4."

Demonstrating NullUserException's answer. 演示NullUserException的答案。

In [120]: a
Out[120]: 
array([[ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True]], dtype=bool)

In [121]: a.all()
Out[121]: True

In [122]: b
Out[122]: 
array([[False,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True]], dtype=bool)

In [123]: b.all()
Out[123]: False

In [124]: b.any()
Out[124]: True

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

相关问题 列表边界-最Python的方式是什么? - List boundaries - what is the most Pythonic way? 是否有更 Pythonic 的方式将布尔值列表转换为 integer? - Is there a more pythonic way to convert a list of booleans to an integer? 在列表中循环直到特定值的最pythonic方式是什么? - What is the most pythonic way of looping in a list until a particular value? 忽略带有括号的列表中的单词的最有效(pythonic)方法是什么? - What is the most efficient (pythonic) way to ignore words in a list that has parantheses? 重构此列表构建代码的最Pythonic方法是什么? - What's the most Pythonic way to refactor this list building code? 从 OrderedDict 列表中查找项目的最pythonic 方法是什么? - what would be the most pythonic way of finding items from a list of OrderedDict? 从列表中弹出随机元素的最pythonic方法是什么? - What is the most pythonic way to pop a random element from a list? 在列表中找到与其他元素不同的元素的最pythonic方法是什么? - what is most pythonic way to find a element in a list that is different with other elements? 编写此筛选列表理解的最pythonic方法是什么? - What's the most pythonic way to write this filtered list comprehension? 识别列表中连续重复项的最 Pythonic 方法是什么? - What's the most Pythonic way to identify consecutive duplicates in a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM