码迷,mamicode.com
首页 > 编程语言 > 详细

Python 之 any与all 方法

时间:2015-08-09 12:37:18      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:python   any   all   pop   built-in functions   

any()
doc: Return True if any element of the iterable is true. If the iterable is empty, return False.
只要迭代器中有一个元素为真就为真。

In [4]: a = [True, False]  
In [5]: any(a)  
Out[5]: True  

也就是说,整个迭代中返回所有的真假判断中有一个真就是真,就好像说一箱子鸡蛋中只要有一个坏了,我们就认定这厢鸡蛋坏了。


In [6]: b = ['good','bad','good','good']  
In [7]: any(i=='bad' for i in b)  
Out[7]: True  
把坏的鸡蛋扔了

In [9]: b.pop(1)  
Out[9]: 'bad'    
In [10]: any(i=='bad' for i in b)  
Out[10]: False  
结果就想法了,如果对于单个元素的判断,有点像 ‘bad‘

all()
doc:Return True if all elements of the iterable are true (or if the iterable is empty)
也就是说,迭代器中所有的判断项返回都是真,结果才为真

In [13]: a  
Out[13]: [True, False]    
In [14]: all(a)  
Out[14]: False  
如果一箱子鸡蛋全都好,才算好。


In [15]: b  
Out[15]: ['good', 'good', 'good', 'bad']  
In [16]: all('good'== i for i in b)  
Out[16]: False  

有一个坏的,返回false

In [17]: b.pop()  
Out[17]: 'bad'  
In [18]: all('good'== i for i in b)  
Out[18]: True  
剔除不好的,全都为good,结果为True


文档地址

版权声明:本文为博主原创文章,未经博主允许不得转载。

Python 之 any与all 方法

标签:python   any   all   pop   built-in functions   

原文地址:http://blog.csdn.net/u013630349/article/details/47374333

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!