英文文檔: any(iterable) Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to: 說明: 1. 接受一個可迭代器對象為參數,當參 ...
英文文檔:
any
(iterable)
Return True
if any element of the iterable is true. If the iterable is empty, return False
. Equivalent to:
def any(iterable): for element in iterable: if element: return True return False
說明:
1. 接受一個可迭代器對象為參數,當參數為空或者不為可迭代器對象是報錯
>>> any(2) #傳入數值報錯 Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> any(2) TypeError: 'int' object is not iterable
2. 如果可迭代對象中其中一個元素的邏輯值為True時,返回True,全部值均為False時返回False
>>> any([0,1,2]) #列表元素有一個為True,則返回True True >>> any([0,0]) #列表元素全部為False,則返回False False
3. 如果可迭代對象為空(元素個數為0),返回False
>>> any([]) #空列表 False >>> any({}) #空字典 False >>>