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

python any() all()

时间:2018-08-05 16:50:29      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:empty   keyword   bad   返回   style   elements   size   ble   str   

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

 

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

 

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

  1. In [6]: b = [‘good‘,‘bad‘,‘good‘,‘good‘]
  2. In [7]: any(i==‘bad‘ for i in b)
  3. Out[7]: True

把坏的鸡蛋扔了

  1. In [9]: b.pop(1)
  2. Out[9]: ‘bad‘
  3. In [10]: any(i==‘bad‘ for i in b)
  4. Out[10]: False

结果就想法了,如果对于单个元素的判断,有点像 ‘bad‘

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

1     In [13]: a
2     Out[13]: [True, False]
3     In [14]: all(a)
4     Out[14]: False

 

 

 

如果一箱子鸡蛋全都好,才算好。


  1. In [15]: b
  2. Out[15]: [‘good‘, ‘good‘, ‘good‘, ‘bad‘]
  3. In [16]: all(‘good‘== i for i in b)
  4. Out[16]: False

有一个坏的,返回false

  1. In [17]: b.pop()
  2. Out[17]: ‘bad‘
  3. In [18]: all(‘good‘== i for i in b)
  4. Out[18]: True

剔除不好的,全都为good,结果为True


return [i for i in range(left,right+1) if not any([m == ‘0‘ or i % int(m) != 0 for m in str(i)])]

例如10 或者13

如果包含0或者无法被 13%3=0,都会从列表中去除

python any() all()

标签:empty   keyword   bad   返回   style   elements   size   ble   str   

原文地址:https://www.cnblogs.com/xiaojinniu425/p/9425987.html

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