Python中的“all”函数是如何工作的?

2024-05-21 07:47:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中搜索关于^{}函数的理解,发现了this,如下所示:

all will return True only when all the elements are Truthy.

但是当我使用这个函数时,它的作用就不同了:

'?' == True   # False
'!' == True   # False
all(['?','!']) # True

为什么当输入中的所有元素都是False时,它返回True?是我误解了它的功能还是有解释?在


Tags: the函数功能falsetrue元素onlyreturn
3条回答

'?'还有“!”它们都是真实的,因为它们不是空弦。在

True和“真理”是有区别的。Truthy的意思是,当被强制时,它可以计算为True。这与==到{}不同。在

当我们要检查列表中的所有项是否可编辑时,使用all()函数。 例如: x=[1,2,3,4,5] all(x) 它将返回真值。在

only when all the elements are Truthy.

真话!=True。在

all本质上检查bool(something)是否是True(对于iterable中的所有something)。在

>>> "?" == True
False
>>> "?" == False # it's not False either
False
>>> bool("?")
True

相关问题 更多 >