1. 程式人生 > >Python 中的 all() 和 any()

Python 中的 all() 和 any()

Python 中的 all() 和 any()

轉載請註明出處:https://blog.csdn.net/jpch89/article/details/85119026


文章目錄


1. all()

互動模式下使用 help(all) 檢視幫助文件:

>>> help(all)
Help on built-in function all in module builtins:

all(iterable, /)
    Return True if bool(x) is True for all values x in the iterable.
    
    If the iterable is empty, return True.
  • all() 函式接收一個可迭代物件作為引數,不能不傳引數呼叫。
  • 對可迭代物件中的每個元素 x 都進行布林型別轉換 bool(x),如果它們都為 True,那麼返回 True,只要有一個為 False,則返回 False
  • 假如可迭代物件為空,返回 True

相當於 x1 and x2 and x3 ... and xn 得到的結果


2. any()

互動模式下使用 help(any) 檢視幫助文件:

>>> help(any)
Help on built-in function any in module builtins:

any(iterable, /)
    Return True if bool(x) is True for any x in the iterable.
    
    If the iterable is empty, return False.

  • any() 函式接收一個可迭代物件作為引數,不能不傳引數呼叫。
  • 對可迭代物件中的每個元素 x 都進行布林型別轉換 bool(x),如果它們都為 False,那麼返回 False,只要有一個為 True,則返回 True
  • 假如可迭代物件為空,返回 False

相當於 x1 or x2 or x3 ... or xn 得到的結果


完成於 2018.12.27