Python内置函数(32)——all

英文文档:

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

  判断可迭代对象的每个元素都是 True 值

说明:

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

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

2. 如果可迭代对象中每个元素的逻辑值均为True时,返回True,否则返回False

>>> all([1,2]) #列表中每个元素逻辑值均为True,返回True
True
>>> all([0,1,2]) #列表中0的逻辑值为False,返回False
False

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

>>> all(()) #空元组
True
>>> all({}) #空字典
True

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

时间: 2024-08-06 18:28:51

Python内置函数(32)——all的相关文章

Python内置函数(32)——input

英文文档: input([prompt]) If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is rea

Python内置函数_数学运算类

本文和大家分享的主要是python内置函数数据运算类相关内容,一起来看看吧,希望对大家学习python 有所帮助. abs abs(x) 求绝对值 · X可以是整型,也可以是复数 · 若X是复数,则返回复数的模 >>> abs(-1)1>>> abs(-3+4j)5.0>>> bin bin(x) 将整数x转换为二进制字符串 >>> bin(2)'0b10'>>> bin(3)'0b11' bool bool([x]

Python内置函数进制转换的用法

使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns a

Python补充--Python内置函数清单

Python内置函数 Python内置(built-in)函数随着python解释器的运行而创建.在Python的程序中,你可以随时调用这些函数,不需要定义.最常见的内置函数是: print("Hello World!") 在Python教程中,我们已经提到下面一些内置函数:基本数据类型 type()反过头来看看 dir()   help()    len()词典 len()文本文件的输入输出 open()循环设计 range()   enumerate()    zip()循环对象

python基础9 -----python内置函数

python内置函数1 一.python内所有的内置函数: 二.python内常用的内置函数: 三.python内内置函数详解: 1.数学运算函数: 2.集合类函数: 3.逻辑类函数: 4.映射类函数: 5.IO操作:

Python 内置函数sorted()有哪些高级用法?

本文和大家分享的主要是python内置函数sorted()的相关内容,一起来看看吧,希望对大家学习python http://www.maiziedu.com/land/python/有所帮助. 1.对于 Python 内置函数 sorted() ,先拿来跟list(列表)中的成员函数 list.sort() 进行下对比.在本质上,list的排序和内建函数sorted的排序是差不多的,连参数都基本上是一样的. 2.主要的区别在于, list.sort() 是对已经存在的列表进行操作,进而可以改变

[python基础知识]python内置函数map/reduce/filter

python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法)来迭代遍历每个序列中的元素. 返回bool_func结果为true的元素的序列(注意弄清楚序列是什么意思)http://blog.csdn.net/bolike/article/details/19997465序列参考</a> 如果filter参数值为None,list参数中所有为假的元 素都将被

lambda 表达式+python内置函数

#函数 def f1(a,b): retrun  a+b #lambda方式,形参(a,b):返回值(a+b) f2=lambda a,b : a+b 在一些比较简单的过程计算就可以用lambda python内置函数 abc 获取绝对值 all 循环对象,都为真就返回真否则为假 >>> li[1, 2, 3]>>> all(li)True #0为假 >>> li=[0,1,2,3]>>> all(li)False bool 真假判断

python内置函数(三)

python内置函数目前需要知道的如下: 代码: #!/usr/bin/env python# -*- coding:utf-8 -*-# author by lh #map(函数,可迭代的对象)def f1(x): return x+100ret=map(f1,[1,2,3,4,5])for i in ret: print iprint '---------------------------------------'#hash转换成哈希值,节约内存dic={ 'dvfhsuicbfhascj