any(iterable)
如果參數(shù) iterable 存在一個元素為 true(即元素的值不為0、''、False),函數(shù)返回 True,否則返回 False(參數(shù) iterable 為空時也返回 False)。
該函數(shù)等價于:
def any(iterable):
for element in iterable:
if element:
return True
return False
說明
參數(shù) iterable 是可迭代對象。
示例
下面的代碼演示了列表/元組具有不同元素時函數(shù) 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