python any() 函数究竟是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16505456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How exactly does the python any() function work?
提问by pythoniku
In the python docs page for any, the equivalent code for the any()function is given as:
在 python 文档页面中any,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 forloop know I am looking for something > 0?
从函数定义中,我只能看到我正在传递一个可迭代对象。for循环如何知道我在寻找什么> 0?
采纳答案by PaulMcG
If you use any(lst)you see that lstis 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 lstalso contained any of the following [-1, True, "X", 0.00001](all of which evaluate to True) then any(lst)would be True.
如果您使用,any(lst)您会看到它lst是可迭代的,它是一些项目的列表。如果它包含[0, False, '', 0.0, [], {}, None](所有的布尔值都是False),那么any(lst)将是False。如果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. 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 lstcontaining [-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 anyfunction, which would return True, since there is at least one Truevalue.
在您发布的代码中x > 0 for x in lst,这是一种不同类型的可迭代对象,称为生成器表达式。在将生成器表达式添加到 Python 之前,您会创建一个列表理解,它看起来非常相似,但周围有[]'s: [x > 0 for x in lst]。从lst包含[-1, -2, 10, -4, 20],你会得到这个理解列表:[False, False, True, False, True]。此内部值随后将传递给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 anyfunction iterates through the values generated one at a time by the generator expression. And, since anyshort-circuits, it will stop iterating as soon as it sees the first Truevalue. This would be especially handy if you created lstusing something like lst = range(-1,int(1e9))(or xrangeif you are using Python2.x). Even though this expression will generate over a billion entries, anyonly has to go as far as the third entry when it gets to 1, which evaluates Truefor x>0, and so anycan return True.
但是对于生成器表达式,Python 不再需要创建True(s)and 的内部列表False(s),这些值将在any函数迭代生成器表达式一次生成一个值时生成。并且,由于any短路,它会在看到第一个True值后立即停止迭代。如果您lst使用类似lst = range(-1,int(1e9))(或者xrange如果您正在使用Python2.x)创建,这将特别方便。即使这个表达式将生成超过 10 亿个条目,any当它到达 时只需要到达第三个条目1,它的计算结果True为x>0,因此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. But by using a generator expression, you can have Python's builtin functions like anyand allbreak out early, as soon as a Trueor Falsevalue is seen.
如果您创建了一个列表推导式,Python 将首先必须在内存中创建十亿个元素的列表,然后将其传递给any. 但是通过使用生成器表达式,你可以让 Python 的内置函数像anyandall那样在看到a TrueorFalse值时尽早中断。
回答by freakish
It's because the iterable is
这是因为可迭代的是
(x > 0 for x in list)
Note that x > 0returns either Trueor Falseand thus you have an iterable of booleans.
请注意,x > 0返回Trueor False,因此您有一个可迭代的布尔值。
回答by jamylak
(x > 0 for x in list)in that function call creates a generator expression eg.
(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 anyuses, and shortcircuits on encountering the first object that evaluates True
其any用途,并在遇到的第一个对象,评估地短路True
回答by Alisha
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
回答by Pranjay Kaparuwan
>>> 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

