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

 

转载于:https://www.cnblogs.com/BlameKidd/p/9292029.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值