[英]How does the “all” function in Python work?
I searched for understanding about the all
function in Python, and I found this , according to here: 我搜索了有关Python中
all
函数的理解,然后根据这里找到了它 :
all
will returnTrue
only when all the elements are Truthy.只有当所有元素都为真时,
all
才会返回True
。
But when I work with this function it's acting differently: 但是当我使用此功能时,它的行为有所不同:
'?' == True # False
'!' == True # False
all(['?','!']) # True
Why is it that when all elements in input are False
it returns True
? 为什么当输入中的所有元素均为
False
它返回True
? Did I misunderstand its functionality or is there an explanation? 我误解了它的功能还是有解释?
only when all the elements are Truthy .
只有当所有要素都是真实的时候 。
Truthy != True
. 真!=
True
。
all
essentially checks whether bool(something)
is True
(for all something
s in the iterable). 基本上
all
检查bool(something)
是否为True
(对于迭代器中的所有something
)。
>>> "?" == True
False
>>> "?" == False # it's not False either
False
>>> bool("?")
True
'?' '?' and '!'
和'!' are both truthy since they are non-empty Strings.
都是真实的,因为它们是非空字符串。
There's a difference between True
and "truthy". True
和“真实”之间是有区别的。 Truthy means that when coerced, it can evaluate to True
. Truthy表示被强制时可以求值为
True
。 That's different from it being ==
to True
though. 但这与
==
到True
不同。
all() function is used when we want to check that if all the items in a list are iterable or not. 当我们要检查列表中的所有项目是否可迭代时,使用all()函数。 For ex :
x=[1,2,3,4,5] all(x)
It will return True. 例如:
x=[1,2,3,4,5] all(x)
它将返回True。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.