Python内置函数(3)——any
生活随笔
收集整理的這篇文章主要介紹了
Python内置函数(3)——any
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
英文文檔:
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
說(shuō)明:
1. 接受一個(gè)可迭代器對(duì)象為參數(shù),當(dāng)參數(shù)為空或者不為可迭代器對(duì)象是報(bào)錯(cuò)
>>> any(2) #傳入數(shù)值報(bào)錯(cuò)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
any(2)
TypeError: 'int' object is not iterable
2. 如果可迭代對(duì)象中其中一個(gè)元素的邏輯值為T(mén)rue時(shí),返回True,全部值均為False時(shí)返回False
>>> any([0,1,2]) #列表元素有一個(gè)為T(mén)rue,則返回True
True
>>> any([0,0]) #列表元素全部為False,則返回False
False
3. 如果可迭代對(duì)象為空(元素個(gè)數(shù)為0),返回False
>>> any([]) #空列表
False
>>> any({}) #空字典
False
>>>
總結(jié)
以上是生活随笔為你收集整理的Python内置函数(3)——any的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 全自动器皿清洗机的操作界面设计,你知道多
- 下一篇: JIRA中的核心概念