[英]How exactly does the python any() function work?
In the python docs page for any
, the equivalent code for the any()
function is given as: 在
any
python文档页面中, any()
函数的等效代码如下:
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)
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
? for
循环如何知道我正在寻找> 0
东西?
If you use any(lst)
you see that lst
is the iterable, which is a list of some items. 如果您使用
any(lst)
您会看到lst
是可迭代的,这是一些项目的列表。 If it contained [0, False, '', 0.0, [], {}, None]
(which all have boolean values of False
) then any(lst)
would be False
. 如果它包含
[0, False, '', 0.0, [], {}, None]
(它们都具有False
布尔值)那么any(lst)
都将为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
. 如果
lst
还包含以下任何一个[-1, True, "X", 0.00001]
(所有值都为True
),那么any(lst)
都将为True
。
In the code you posted, x > 0 for x in lst
, this is a different kind of iterable, called a generator expression . 在您发布的代码中,
x > 0 for x in lst
, x > 0 for x in lst
,这是一种不同的可迭代类型,称为生成器表达式 。 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]
. 在将生成器表达式添加到Python之前,您将创建一个列表推导 ,它看起来非常相似,但周围的
[]
: [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]
. 从包含
[-1, -2, 10, -4, 20]
lst
中,你会得到这个被理解的列表 : [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. 然后,此内部值将传递给
any
函数,该函数将返回True
,因为至少有一个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. 但是使用生成器表达式 ,Python不再需要创建
True(s)
和False(s)
内部列表,当any
函数迭代生成器表达式一次生成的值时,将生成这些值。 And , since any
short-circuits, it will stop iterating as soon as it sees the first True
value. 并且 ,由于
any
短路,一旦看到第一个True
值,它就会停止迭代。 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 ). 如果你使用类似
lst = range(-1,int(1e9))
(或xrange
如果你使用Python2.x )创建lst
这将特别方便。 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
. 即使这个表达式将生成超过十亿个条目,
any
只有当它达到1
时才必须到第三个条目,它会为x>0
计算True
,因此any
都可以返回True
。
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
. 如果您已经创建了列表解析 ,那么Python首先必须在内存中创建十亿元素列表,然后将其传递给
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
和all
只要一个早期的爆发, True
或False
的值为止。
>>> names = ['King', 'Queen', 'Joker']
>>> any(n in 'King and john' for n in names)
True
>>> all(n in 'King and Queen' for n in names)
False
It just reduce several line of code into one. 它只是将几行代码减少为一行。 You don't have to write lengthy code like:
您不必编写冗长的代码,例如:
for n in names:
if n in 'King and john':
print True
else:
print False
(x > 0 for x in list)
in that function call creates a generator expression eg. (x > 0 for x in list)
中的(x > 0 for x in list)
在该函数调用中创建一个生成器表达式,例如。
>>> nums = [1, 2, -1, 9, -5]
>>> genexp = (x > 0 for x in nums)
>>> for x in genexp:
print x
True
True
False
True
False
Which any
uses, and shortcircuits on encountering the first object that evaluates True
遇到第一个评估
True
对象的any
用途和短路
It's because the iterable is 这是因为可迭代的
(x > 0 for x in list)
Note that x > 0
returns either True
or False
and thus you have an iterable of booleans. 请注意,
x > 0
返回True
或False
,因此您有一个可迭代的布尔值。
Simply saying, any() does this work : according to the condition even if it encounters one fulfilling value in the list, it returns true, else it returns false. 简单地说,any()可以做到这一点:根据条件,即使它在列表中遇到一个满足的值,它返回true,否则返回false。
list = [2,-3,-4,5,6]
a = any(x>0 for x in lst)
print a:
True
list = [2,3,4,5,6,7]
a = any(x<0 for x in lst)
print a:
False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.