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

Python内置函数(33)——any

时间:2017-12-29 16:21:12      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:err   error   迭代器   iterable   doc   对象   ror   元素   desc   

英文文档:

any(iterable)

    Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

  判断可迭代对象的元素是否有 True值的元素

说明:

    1. 接受一个可迭代器对象为参数,当参数为空或者不为可迭代器对象是报错

>>> any(2) #传入数值报错
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    any(2)
TypeError: ‘int‘ object is not iterable

 

    2. 如果可迭代对象中其中一个元素的逻辑值为True时,返回True,全部值均为False时返回False

>>> any([0,1,2]) #列表元素有一个为True,则返回True
True
>>> any([0,0]) #列表元素全部为False,则返回False
False

    3. 如果可迭代对象为空(元素个数为0),返回False

>>> any([]) #空列表
False
>>> any({}) #空字典
False
>>> 

Python内置函数(33)——any

标签:err   error   迭代器   iterable   doc   对象   ror   元素   desc   

原文地址:https://www.cnblogs.com/lincappu/p/8144909.html

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