Files
python/20-24/14anyheall.py
T
2026-05-06 11:21:42 +08:00

29 lines
767 B
Python

# 在Python中,any()和all()是两个重要的内置函数,用于对可迭代对象(如列表、元组、集合、字典、字符串等)中的元素执行布尔运算。这两个函数都采用短路求值(short-circuit evaluation)策略,能够高效地处理大量数据。
truthy_list = [1, 2, 3]
# 使用all()检查是否所有值都为真
print(f"all([1, 2, 3]): {all(truthy_list)}") # 输出: True
alsy_list = [0, False, None]
# 使用any()检查是否有真值
print(f"any([0, False, None]): {any(alsy_list)}") # 输出: False
abc = []
if not abc:
print("1")
else:
print("2")
# # 应用
# def test_fuc(x):
# return bool(x)
# tset_list = [0, 1, 2, 3, 4]
# result = all(map(lambda x: test_fuc(x), tset_list))
# print(result)