Skip to content

Files

Latest commit

48a2448 · Jun 26, 2020

History

History
61 lines (42 loc) · 881 Bytes

54.md

File metadata and controls

61 lines (42 loc) · 881 Bytes

Python all()函数

原文: https://thepythonguru.com/python-builtin-functions/all/


于 2020 年 1 月 7 日更新


all()函数测试可迭代项中的所有项目是否都等于True。 如果所有项目都为true,它将接受一个可迭代对象并返回True,否则返回False

其语法如下:

all(iterable) -> boolean

这是一个例子:

>>>
>>> all(['alpha', 'beta', ''])
False
>>>
>>> 
>>> all(['one', 'two', 'three'])
True
>>> 
>>> 
>>> all([])
True
>>>
>>>
>>> gen = (i for i in ['0', (), {}, 51, 89]) # generator
>>> 
>>> 
>>> all(gen)
False
>>>

试试看:

print(all(['alpha', 'beta', '']))

print(all(['one', 'two', 'three']))

print(all([]))

gen = (i for i in ['0', (), {}, 51, 89]) # generator

print(all(gen))