Skip to content

Files

Latest commit

8244caa · Jun 26, 2020

History

History
61 lines (42 loc) · 840 Bytes

53.md

File metadata and controls

61 lines (42 loc) · 840 Bytes

Python any()函数

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


于 2020 年 1 月 7 日更新


any()函数测试可迭代项中的任何项目是否求值为True。 它接受一个可迭代对象并返回True,如果可迭代对象中的至少一项为true,则返回False

其语法如下:

any(iterable) -> boolean

这是一个例子:

>>> 
>>> any([10, "", "one"])
True
>>> 
>>> 
>>> any(("", {}))
False
>>> 
>>> 
>>> 
>>> any([])
False
>>> 
>>>
>>> gen = (i for i in [5, 0, 0.0, 4]) # generator
>>> 
>>> any(gen)
True
>>>

试试看:

print(any([10, "", "one"]))

print(any(("", {})))

print(any([]))

gen = (i for i in [5, 0, 0.0, 4]) # generator

print(any(gen))