Python 的 any 和 all 函数是如何工作的? [英] How do Python's any and all functions work?

查看:62
本文介绍了Python 的 any 和 all 函数是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解 any()all() Python 内置函数是如何工作的.

我正在尝试比较元组,以便如果任何值不同,则返回 True,如果它们都相同,则返回 False.在这种情况下,他们如何工作以返回 [False, False, False]?

d 是一个 defaultdict(list).

print d['Drd2']# [[1, 5, 0], [1, 6, 0]]打印列表(zip(*d['Drd2']))# [(1, 1), (5, 6), (0, 0)]打印 [any(x) 而不是 all(x) for x in zip(*d['Drd2'])]# [假,假,假]

据我所知,这应该输出

# [假,真,假]

因为(1,1)相同,(5,6)不同,(0,0)相同.

为什么所有元组的计算结果都是 False?

解决方案

你可以粗略地把anyall看成一系列符合逻辑的or> 和 运算符.

任何

any 将返回 True至少一个元素 为真时.阅读真值测试.

全部

all 仅在 所有元素 都为真时返回 True.

真值表

+-----------------------------------------+----------+---------+||任何 |所有 |+-----------------------------------------+---------+----------+|所有Truthy值|真|真|+-----------------------------------------+---------+----------+|所有 Falsy 值 |错误 |错误 |+-----------------------------------------+---------+----------+|一个Truthy 值(所有其他值都是Falsy) |真|错误 |+-----------------------------------------+---------+----------+|一个 Falsy 值(所有其他值都是真值) |真|错误 |+-----------------------------------------+---------+----------+|空可迭代 |错误 |真|+-----------------------------------------+---------+----------+

注1:空的迭代案例在官方文档中有解释,像这样

any

<块引用>

如果可迭代对象的任何元素为真,则返回 True.如果iterable为空,返回False

由于没有一个元素为真,在这种情况下它返回 False.

all

<块引用>

如果可迭代对象的所有元素都为真(或者如果可迭代对象为空),则返回 True.

因为没有一个元素是假的,所以在这种情况下它返回 True.

<小时>

注意 2:

关于anyall 的另一件重要的事情是,一旦他们知道结果,它就会使执行短路.优点是,不需要消耗整个迭代.例如,

<预><代码>>>>multiples_of_6 = (not (i % 6) for i in range(1, 10))>>>任何(multiples_of_6)真的>>>列表(multiples_of_6)[假,假,假]

这里,(not (i % 6) for i in range(1, 10)) 是一个生成器表达式,如果当前数字在 1 和9 是 6 的倍数.any 迭代 multiples_of_6,当遇到 6 时,它找到一个 Truthy 值,所以它立即返回 True,其余的 multiples_of_6 不会被迭代.这就是我们在打印 list(multiples_of_6) 时看到的,789 的结果.

这个优秀的东西在这个答案中使用得非常巧妙.

<小时>

有了这个基本的理解,如果我们看你的代码,你就知道了

any(x) 而不是 all(x)

确保至少其中一个值是Truthy,但不是全部.这就是它返回 [False, False, False] 的原因.如果你真的想检查两个数字是否不一样,

print [x[0] != x[1] for x in zip(*d['Drd2'])]

I'm trying to understand how the any() and all() Python built-in functions work.

I'm trying to compare the tuples so that if any value is different then it will return True and if they are all the same it will return False. How are they working in this case to return [False, False, False]?

d is a defaultdict(list).

print d['Drd2']
# [[1, 5, 0], [1, 6, 0]]
print list(zip(*d['Drd2']))
# [(1, 1), (5, 6), (0, 0)]
print [any(x) and not all(x) for x in zip(*d['Drd2'])]
# [False, False, False]

To my knowledge, this should output

# [False, True, False]

since (1,1) are the same, (5,6) are different, and (0,0) are the same.

Why is it evaluating to False for all tuples?

解决方案

You can roughly think of any and all as series of logical or and and operators, respectively.

any

any will return True when at least one of the elements is Truthy. Read about Truth Value Testing.

all

all will return True only when all the elements are Truthy.

Truth table

+-----------------------------------------+---------+---------+
|                                         |   any   |   all   |
+-----------------------------------------+---------+---------+
| All Truthy values                       |  True   |  True   |
+-----------------------------------------+---------+---------+
| All Falsy values                        |  False  |  False  |
+-----------------------------------------+---------+---------+
| One Truthy value (all others are Falsy) |  True   |  False  |
+-----------------------------------------+---------+---------+
| One Falsy value (all others are Truthy) |  True   |  False  |
+-----------------------------------------+---------+---------+
| Empty Iterable                          |  False  |  True   |
+-----------------------------------------+---------+---------+

Note 1: The empty iterable case is explained in the official documentation, like this

any

Return True if any element of the iterable is true. If the iterable is empty, return False

Since none of the elements are true, it returns False in this case.

all

Return True if all elements of the iterable are true (or if the iterable is empty).

Since none of the elements are false, it returns True in this case.


Note 2:

Another important thing to know about any and all is, it will short-circuit the execution, the moment they know the result. The advantage is, entire iterable need not be consumed. For example,

>>> multiples_of_6 = (not (i % 6) for i in range(1, 10))
>>> any(multiples_of_6)
True
>>> list(multiples_of_6)
[False, False, False]

Here, (not (i % 6) for i in range(1, 10)) is a generator expression which returns True if the current number within 1 and 9 is a multiple of 6. any iterates the multiples_of_6 and when it meets 6, it finds a Truthy value, so it immediately returns True, and rest of the multiples_of_6 is not iterated. That is what we see when we print list(multiples_of_6), the result of 7, 8 and 9.

This excellent thing is used very cleverly in this answer.


With this basic understanding, if we look at your code, you do

any(x) and not all(x)

which makes sure that, atleast one of the values is Truthy but not all of them. That is why it is returning [False, False, False]. If you really wanted to check if both the numbers are not the same,

print [x[0] != x[1] for x in zip(*d['Drd2'])]

这篇关于Python 的 any 和 all 函数是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆