天天看點

Python内置函數 — all,any

1、all

源碼注釋:

def all(*args, **kwargs): # real signature unknown
    """
    Return True if bool(x) is True for all values x in the iterable.
    
    If the iterable is empty, return True.
    """
    pass      

文法格式:

all(iterable)

如果 iterable 的所有元素均為真值(或可疊代對象為空)則傳回 True 。 

用法執行個體:

(1)所有元素為真,傳回 True

print(all([1, 2, 3]))
---------------------------------------------------------------------------
True
           

(2)有的元素為假,傳回 False

print(all([0, 1, 2]))
--------------------------------------------------------------------------
False
           

(3)對象為空,傳回 True

print(all([]))
-----------------------------------------------------------------------
True
           

2、any

源碼注釋:

def any(*args, **kwargs): # real signature unknown
    """
    Return True if bool(x) is True for any x in the iterable.
    
    If the iterable is empty, return False.
    """
    pass      

文法格式:

any(iterable)

如果 iterable 的任一進制素為真值則傳回 True。 如果可疊代對象為空,傳回 False。

用法執行個體:

(1)任一進制素為真,傳回 True

print(any([0, None, 3]))
-----------------------------------------------------------------------
True
           

(2)所有元素為假,傳回 False

print(any([0, None, 0.00]))
--------------------------------------------------------------------------------
False
           

(3)對象為空,傳回 False

print(any([]))
---------------------------------------------------------------------------
False
           

注:

關于邏輯值真假的判斷可以閱讀:Python基礎 — 邏輯值真假判斷_笃行之.kiss的部落格-CSDN部落格

reference:

内置函數 — Python 3.8.16 文檔