简体   繁体   English

Python中的“全部”功能如何工作?

[英]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 return True 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM