python中的函數any()和all()

python中又很多好玩的函數,下面說一下內建函數any()和all():


any(...)

    any(iterable) -> bool

    

    Return True if bool(x) is True for any x in the iterable.

    If the iterable is empty, return False.

以上是python doc中得說明,意思就是當傳入空可迭代對象時返回False,當可迭代對象中有任意一個不爲False,則返回True


all(...)

    all(iterable) -> bool

    

    Return True if bool(x) is True for all values x in the iterable.

    If the iterable is empty, return True.

以上是python doc中得說明,意思就是當傳入空可迭代對象時返回True,當可迭代對象中有任意一個不爲True,則返回False


示例:

>>> any('123')

True

>>> any([0, 1])

True

>>> any([0, ''])

False

>>> all('123')

True

>>> all([0, 1])

False

>>> all([1, 2])

True



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章