show | version | enable_checker |
---|---|---|
step |
1.0 |
true |
- 这次研究了字典推导式
- 字典推导式本质上是集合推导式
- 因为字典本质上是 key-value 的集合
- 可以像列表推导式一样生成集合推导式
- 可以用冒号(:)生成 key-value 对的集合推导式就是字典推导式
- 还有什么可玩的么?🤔
any
help(any)
- 新函数any
- 遍历 可迭代对象
- 得到 元素对应的 布尔值
- 如果 非零 则 返回True
- 如果 为零 则继续迭代
- 如果 迭代完毕
- 返回 False
- 得到 元素对应的 布尔值
l = [False,0,0.0,0j,"",b"",[],(),{}]
any(l)
- l中对象布尔值
- 都为False
l = [False,0,0.0,0j,"",b"",[],(),{},"0"]
any(l)
- 如果任意一个为True
- 直接返回True
- any有什么应用场景吗?
test_str = "m0rning"
l1 = list(char for char in test_str)
print(l1)
l2 = list(char.isdigit() for char in test_str)
print(l2)
any(char.isdigit() for char in test_str)
- 某个字符串中是否有数字?
test_str = "123j"
any(char.isalpha() for char in test_str)
- 判断字符串中是否有字母
- 除了字符串之外
- 还有什么可迭代的?
enemies = [
{"id": 1, "hp":0},
{"id": 2, "hp":0},
{"id": 3, "hp":0}
]
any(enemy["hp"] > 0 for enemy in enemies)
- 任何血量大于0的敌人
- any的本质是什么?
b1 = False
b2 = True
b3 = False
any([b1, b2, b3])
b1 or b2 or b3
- any 本质上是
- 对布尔变量的或运算
- 这次我们研究了any函数
- 任何一次迭代出True
- 就返回True
- 都迭代完了 也没有返回
- 就返回False
- 任何一次迭代出True
- 有真为真 全假为假
- any本质上是 一堆条件的 或运算
- 那有没有与运算 对应的函数呢?🤔
- 下次再说 👋