python内置函数每日一学 -- any()
any(iterable)
官方文档解释:
Return True
if any element of the iterable is true. If the iterable is empty, return False
. Equivalent to:
1 def any(iterable): 2 for element in iterable: 3 if element: 4 return True 5 return False
详解:
如果iterable中存在一个元素不为0、''、False,any(iterable)返回True。如果iterable为空,返回False。
实例:
1 列表: 2 3 print(any([1,2,3,4])) # True 4 5 print(any([0,1,2,3,4])) # True 6 7 print(any([])) # False 8 9 print(any(['',1,2,3,4])) # True 10 11 print(any([False,1,2,3,4])) # True 12 13 print(any(['',False,0])) # False 14 15 元组: 16 17 print(any((1,2,3,4))) # True 18 19 print(any((0,1,2,3,4))) # True 20 21 print(any(())) # False 22 23 print(any(('',1,2,3,4))) # True 24 25 print(any((False,1,2,3,4))) # True 26 27 print(any(('',False,0))) # False