any(iterable)
如果参数 iterable 存在一个元素为 true(即元素的值不为0、''、False),函数返回 True,否则返回 False(参数 iterable 为空时也返回 False)。
该函数等价于:
def any(iterable):
for element in iterable:
if element:
return True
return False
说明
参数 iterable 是可迭代对象。
示例
下面的代码演示了列表/元组具有不同元素时函数 any(iterable) 的返回值。
>>> any([]) # 空列表
False
>>> any(()) # 空元组
False
>>> any([0, '', False]) # 列表不存在值为 true 的元素
False
>>> any([0, 'iujk', '', False]) # 列表存在一个值为 true 的元素
True
>>> any((0, "", False)) # 元组不存在值为 true 的元素
False
>>> any((0, "", False, 98)) # 元组存在一个值为 true 的元素
True