python - 关于 all() 和 any() 函数的困惑
问题描述
我正在学习 python 并且在理解all()
和any()
功能方面遇到了一些困惑:
1 in [0,2] #False. Correct.
all([0,1]) in [0,2] #True. Why? 1 is not in [0,2]
any([0,1]) in [0,2] #False. Why? 0 is in [0,2]
解决方案
如果可迭代对象中的所有项目都是并且如果有的话,函数all()
返回布尔值:True
True
False
False
all([0, 1]) # return True if all items are True
return False
because0
被认为是False
当False in [0, 2]
返回时True
,因为0 == False
。
为了any([0,1]) in [0,2] #False. Why? 0 is in [0,2]
any([0,1]) # return True if any item is True
返回True
,因为1 = True
.
然后它检查True in [0, 2]
并没有True
or 1
in[0, 2]
并返回False
。
In [1]: 1 == True
Out[1]: True
In [2]: 0 == False
Out[2]: True
In [3]: 2 == True
Out[3]: False
推荐阅读
- header - Powershell - 如何将字典添加到 splat = @{}
- r - 根据R中的三个不同组确定坐标
- java - IntelliJ IDEA 远程开发代码“找不到要前往的声明”
- html - 使用 R 将数据框转换为嵌套的 html 文件
- c# - 使用 C# 将文件上传到 CRM 文件字段
- sql - Oracle while exists(select ...) 插入
- swift - 从上传的图像中获取下载 URL - Firebase 和 Swift
- odata - 如何使用 Power Query 从 ProjectServer 访问资源?(我得到一个 DataSource.Error。)
- swiftui - SwiftUI 多张纸:关闭动画损坏
- swift - 从图像中删除像素,然后将它们恢复