Python內建all函數詳細介紹

高洛峰
發布: 2017-03-21 11:42:14
原創
1814 人瀏覽過

英文文件:

all(iterable#)

    Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True
登入後複製

說明:

    1. 接受一個可迭代器物件為參數,當參數為空或不為可迭代器物件是報錯

>>> all(2) #传入数值报错
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    all(2)
TypeError: &#39;int&#39; object is not iterable
登入後複製

 2. 如果可迭代物件中每個元素的邏輯值皆為True時,傳回True,否則傳回False

>>> all([1,2]) #列表中每个元素逻辑值均为True,返回True
True
>>> all([0,1,2]) #列表中0的逻辑值为False,返回False
False
登入後複製

  3. 若可迭代物件為空(元素個數為0),傳回True

>>> all(()) #空元组
True
>>> all({}) #空字典
True
登入後複製


以上是Python內建all函數詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!