python - Python 中的 "all"函数是如何工作的?

标签 python python-3.x

我搜索了关于 all 的理解在 Python 中的函数,我找到了 this ,根据这里:

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

但是当我使用这个函数时,它的行为有所不同:

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

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

最佳答案

only when all the elements are Truthy.

Truthy != True

all 本质上检查 bool(something) 是否为 True(对于可迭代对象中的所有 something) .

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

关于python - Python 中的 "all"函数是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35685768/

相关文章:

python - 获取 lxml 中的项目符号列表

python - DSX 中的 tensorflow : Consuming prediction results fails due to list()

python - 根据条件创建 Pandas Dataframe 的更好方法

python - 使用python打开.txt文件

django templates Unclosed tag on line 10 : 'if' . 寻找其中之一:endif

python-3.x - 为什么使用 sklearn 库随机生成的数据精度较低

python - 有没有办法遍历列表并将所有内容转换为整数?

Python socket.error : [Errno 111] Connection refused

python - 导入图形Python时出错

python - 在 flask 应用程序中将用户 ip 和用户 id 添加到日志文件