Python3内置函数——all与any

先祭出英文文档:

all(iterable)

Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True
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

再上函数信息表格


函数原型


all(iterable)


参数解释


iterable


可迭代对象,参数不可为空,但iterable可空。


返回值


<class ‘bool‘> True 或 False。


函数说明


当 iterable 中所有元素都为 True 时(或者 iterable 为空),返回 True 。


函数原型


any(iterable)


参数解释


iterable


可迭代对象,参数不可为空,但iterable可空。


返回值


<class ‘bool‘> True 或 False。


函数说明


当 iterable 中有元素为 True 时,则返回 True 。如果当 iterable 为空时,返回 False 。

从官方文档中,我们可以得知,all(iterable)完全与下面这段代码等价:

1 def all(iterable):
2     for element in iterable:
3         if not element:
4             return False
5     return True

any(iterable)则与这段代码等价:

1 def any(iterable):
2     for element in iterable:
3         if element:
4             return True
5     return False

也就是说,当给定一个不为空的可迭代对象之后:

  对于all函数,如果元素全为真则返回True,否则返回。即只要存在的元素就返回

  对于any函数,只要存在一个为的元素就返回True。不存在为的元素返回假。

另外,当可迭代对象为时,all函数返回Trueany函数返回False。这是因为iterable为空时遍历不执行,直接跳到 line 5 执行 return。

范例1:all函数各种参数的各种结果

1 >>> all([‘a‘,(2,4),1,True])     #list都为真
2 True
3 >>> all([‘a‘,(),1,True])        #list元素中有空tuple
4 False
5 >>> all([‘a‘,(2,4),0,True])     #有0
6 False
7 >>> all([‘a‘,(2,4),3,False])    #有False
8 False

范例2:any函数各种参数的各种结果

 1 >>> any([‘a‘,(2,4),3,True])
 2 True
 3 >>> any([‘a‘,(2,4),3,False])
 4 True
 5 >>> any([‘a‘,(),3,False])
 6 True
 7 >>> any([‘‘,(),0,False])
 8 False
 9 >>> any((‘a‘,(),3,False))
10 True
11 >>> any((‘‘,(),0,False))
12 False

范例3:all函数参数为空与iterable为空时

 1 >>> all()
 2 Traceback (most recent call last):
 3   File "<pyshell#20>", line 1, in <module>
 4     all()
 5 TypeError: all() takes exactly one argument (0 given)
 6 >>>all([])
 7 True
 8 >>> all(())
 9 True
10 >>> all({})
11 True
12 >>> all(‘‘)                    #空list、truple、dict、str皆为True
13 True

范例4:any函数参数为空与iterable为空时

 1 >>> any()
 2 Traceback (most recent call last):
 3   File "<pyshell#37>", line 1, in <module>
 4     any()
 5 TypeError: any() takes exactly one argument (0 given)
 6 >>> any([])
 7 False
 8 >>> any(())
 9 False
10 >>> any({})
11 False
12 >>> any("")
13 False

范例5:iterable为空时不遍历

1 >>> p=1
2 >>> i=[]
3 >>> for item in i:            #i为空不遍历
4     print (p)
5
6
7 >>>

如果您喜欢这篇文章就请点个赞再走吧。如果有不完善的地方还请在评论区中指出。有疑问欢迎在评论区中一起探讨。

时间: 2024-08-19 10:07:39

Python3内置函数——all与any的相关文章

python3内置函数详解

一. 简介 python内置了一系列的常用函数,以便于我们使用,python英文官方文档详细说明:点击查看, 为了方便查看,将内置函数的总结记录下来. 二. 使用说明 以下是Python3版本所有的内置函数: 1. abs() 获取绝对值 >>> abs(-10) 10 >>> abs(10) 10 >>> abs(0) 0 >>> a = -10 >>> a.__abs__() 10 2. all() 接受一个迭代

python3 内置函数大全

一. 简介 python内置了一系列的常用函数,以便于我们使用,python英文官方文档详细说明:点击查看, 为了方便查看,将内置函数的总结记录下来. 二. 使用说明 以下是Python3版本所有的内置函数: 1. abs() 获取绝对值 1 >>> abs(-10) 2 10 3 >>> abs(10) 4 10 5 >>> abs(0) 6 0 7 >>> a = -10 8 >>> a.__abs__() 9

python3 内置函数

内置函数 1. abs() 获取绝对值 >>> abs(10) 10 >>> abs(-10) 10.. >>> abs(0) 0 >>> a = -10 >>> a -10 >>> a.__abs__() 10 2. all() 接受一个迭代器,如果迭代器的所有元素都为真,那么返回True,否则返回False >>> all([]) True >>> >&

Built-in Functions(python3内置函数)

在python中有很多的内置函数,这些内置函数让我们更高效,在此将部分相关知识总结一下. 官网地址:https://docs.python.org/3.6/library/functions.html Built-in Functions abs() dict() help() min() setattr() all() dir() hex() next() slice() any() divmod() id() object() sorted() ascii() enumerate() inp

python3内置函数练习

1. abs() 获取绝对值 2. all() 接受一个迭代器,如果迭代器的所有元素都为真,那么返回True,否则返回False 3. any() 接受一个迭代器,如果迭代器里有一个元素为真,那么返回True,否则返回False 4. ascii() 调用对象的__repr__()方法,获得该方法的返回值. 5. bin(), 6. oct(),  7. hex()   三个函数功能为:将十进制数分别转换为2/8/16进制. 8. bool() 测试一个对象是True还是False. 9. by

Python3内置函数——bin

先上英文文档: bin(x) Convert an integer number to a binary string prefixed with "0b". The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer. Some examples: >>>

Python内置函数.md

Python3 内置函数 abs(x) 返回一个数的绝对值.参数可以是一个整数或者一个浮点数.如果参数是一个复数,那么将返回它的模. >>> abs(-123456) 123456 >>> abs(123456) 123456 all(iterable) 当 iterable 中所有元素都为 True 时(或者 iterable 为空),返回 True .相当于: def all(iterable): for element in iterable: if not el

python内置函数和魔法函数

内置方法:Python中声明每一个类系统都会加上一些默认内置方法,提供给系统调用该类的对象时使用.比如需要实例化一个对象时,需要调用该类的init方法:使用print去打印一个类时,其实调用的是str方法等等. init(self, …):初始化对象class,在创建新对象时调用.在方法里,可以初始化该对象的属性,否则调用其他时可能出“现has no attribute”错误: del(self):释放对象,在对象被虚拟机删除之前调用: new(cls,*args,**kwd):实例的生成操作,

python基础7之python3的内置函数

官方介绍: python3:https://docs.python.org/3/library/functions.html?highlight=built#ascii python2:https://docs.python.org/2/library/functions.html?highlight=built#ascii 下面介绍python3中具体每个内置函数的使用方法及含义: abs()