python any() 函数究竟是如何工作的? [英] How exactly does the python any() function work?
问题描述
在 any
的 python 文档页面中,等效的any()
函数的代码如下:
In the python docs page for any
, the equivalent code for the any()
function is given as:
def any(iterable):
for element in iterable:
if element:
return True
return False
如果以这种形式调用它,该函数如何知道我要测试的元素?
How does this function know what element I wanna test if call it in this form?
any(x > 0 for x in list)
从函数定义中,我只能看到我正在传递一个可迭代对象.for
循环如何知道我正在寻找的东西 >0
?
From the function definition, all I can see is that I'm passing an iterable object. How does the for
loop know I am looking for something > 0
?
推荐答案
如果你使用 any(lst)
你会看到 lst
是可迭代的,它是一个列表的一些项目.如果它包含 [0, False, '', 0.0, [], {}, None]
(它们都具有 False
的布尔值),则 any(lst)
将是 False
.如果 lst
还包含以下任何一个 [-1, True, "X", 0.00001]
(所有这些都评估为 True
)然后any(lst)
将是 True
.
If you use any(lst)
you see that lst
is the iterable, which is a list of some items. If it contained [0, False, '', 0.0, [], {}, None]
(which all have boolean values of False
) then any(lst)
would be False
. If lst
also contained any of the following [-1, True, "X", 0.00001]
(all of which evaluate to True
) then any(lst)
would be True
.
在您发布的代码中,x >0 for x in lst
,这是另一种可迭代的,称为生成器表达式.在将生成器表达式添加到 Python 之前,您将创建一个 列表推导式,它看起来非常相似,但是包含了 []
的:[x >0 表示 lst 中的 x]
.从包含 [-1, -2, 10, -4, 20]
的 lst
中,你会得到这个理解列表:[假,假,真,假,真]
.然后,这个内部值将传递给 any
函数,该函数将返回 True
,因为至少有一个 True
值.
In the code you posted, x > 0 for x in lst
, this is a different kind of iterable, called a generator expression. Before generator expressions were added to Python, you would have created a list comprehension, which looks very similar, but with surrounding []
's: [x > 0 for x in lst]
. From the lst
containing [-1, -2, 10, -4, 20]
, you would get this comprehended list: [False, False, True, False, True]
. This internal value would then get passed to the any
function, which would return True
, since there is at least one True
value.
但是使用生成器表达式,Python 不再需要创建 True(s)
和 False(s)
的内部列表,值将在 any
函数迭代生成器表达式一次生成一个值时生成.并且,由于any
短路,它会在看到第一个True
值时立即停止迭代.如果您使用诸如 lst = range(-1,int(1e9))
(或 xrange
,如果您正在使用 Python2.x).即使这个表达式将生成超过 10 亿个条目,any
只需要在到达 1
时到达第三个条目,它计算 True
为 x>0
,所以 any
可以返回 True
.
But with generator expressions, Python no longer has to create that internal list of True(s)
and False(s)
, the values will be generated as the any
function iterates through the values generated one at a time by the generator expression. And, since any
short-circuits, it will stop iterating as soon as it sees the first True
value. This would be especially handy if you created lst
using something like lst = range(-1,int(1e9))
(or xrange
if you are using Python2.x). Even though this expression will generate over a billion entries, any
only has to go as far as the third entry when it gets to 1
, which evaluates True
for x>0
, and so any
can return True
.
如果您创建了一个列表推导式,Python 将首先必须在内存中创建十亿个元素的列表,然后将其传递给 any
.但是通过使用生成器表达式,你可以让 Python 的内置函数,如 any
和 all
尽早爆发,只要 True
或 False
值被看到.
If you had created a list comprehension, Python would first have had to create the billion-element list in memory, and then pass that to any
. But by using a generator expression, you can have Python's builtin functions like any
and all
break out early, as soon as a True
or False
value is seen.
这篇关于python any() 函数究竟是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!