Python3 all() 函数 - 指示列表中的所有元素是否都为真

Python 内置函数 Python 内置函数


描述

all() ​函数用于判断一个列表中的所有元素是否为真(True)。

如果列表中的所有元素都为真(True或者可以转换为True),则返回True,否则返回False

注:元素除了 0、空、None、False 外都算 True。

 为了便于理解,上述文本中使用了列表作为可迭代对象的替换。实际上这是片面的,​all()​函数接受的参数为可迭代的对象(iterable ),也就是说,以下这些类型都可以作为all()的参数:

1.list,tuple,dict、set、str
2.generator,带yield的generator function,统称为iterator。生成器可以使用for可以被next函数调用并返回下一个值。

函数等价于:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True
它与​any()
​函数是一对功能相反的函数,可以简单的记为:all()
​全真才真,​any()
​全假才假(刚好与与运算和或运算类似)

语法

以下是 ​all()​ 方法的语法:

all(iterable)

参数

  • iterable -- 可迭代的参数,包括.list,tuple,dict、set、str以及生成器生成的iterable对象。

返回值

如果iterable的所有元素不为​0​、​''​、​False​或者iterable为空,all(iterable)返回True,否则返回False;

注意:空元组、空列表返回值为True,这里要特别注意。


实例

以下展示了使用 all() 方法的实例:

print(all(['a', 'b', 'c', 'd']))  # 列表list,元素都不为空或0
# 返回 True
print(all(['a', 'b', '', 'd']))   # 列表list,存在一个为空的元素
# 返回False
print(all([0, 1 , 2, 3]))         # 列表list,存在一个为0的元素
# 返回False
print(all(('a', 'b', 'c', 'd')))  # 元组tuple,元素都不为空或0
# 返回 True
print(all(('a', 'b', '', 'd')))   # 元组tuple,存在一个为空的元素
#返回 False
print(all((0, 1, 2, 3)))          # 元组tuple,存在一个为0的元素
# 返回 False
print(all(''))                    # 字符串也是可迭代对象,可以作为all()的参数
# 返回 True
print(all("w3cschool001"))        # 但是字符串中的数字0,空字符串并不算         
# 返回 True
print(all([]))             # 空列表
# 返回 True
print(all(()))            # 空元组
# 返回 True

运行结果为:

True
False
False
True
False
False
True
True
True
True

Python 内置函数 Python 内置函数


目录

Python3 教程

Python3 教程
Python3 简介
Python3 安装
Python3 使用上手
Python3 解释器
Python3 VSCode 配置
Python3 AI 通义灵码 VSCode插件安装与功能详解
Python3 基础语法
Python3 编程第一步
Python3 基本数据类型
Python3 注释
Python3 运算符
Python3 数字
Python3 序列
Python3 字符串
Python3 列表
Python3 元组
Python3 字典
Python3 集合
Python3 条件控制
Python3 循环
Python3 迭代器与生成器
Python3 函数
Python3 数据结构
Python3 模块
Python3 输入和输出
Python3 File 方法
Python3 OS 文件/目录方法
Python3 错误和异常
Python3 面向对象
Python3 标准库概览
Python3 实例

Python3 历代版本更新

Python3.11更新
Python3.10更新
python3.9更新

Python3 高级教程

Python3 正则表达式
Python3 CGI 编程
Python3 MySQL 数据库连接
Python3 网络编程
Python3 SMTP发送邮件
Python3 多线程
Python3 XML解析
Python3 JSON 数据解析
Python3 日期和时间
Python3 内置函数
Python3 MongoDB
Python3 urllib模块
Python3 uWSGI 安装配置
Python3 pip
Python3 operator 模块
Python3 math 模块
Python3 requests 模块
Python3 random 模块
Python3 装饰器
Python3速查表:语法、数据结构与函数全解析

Python 技术专题

python 解决pip下载缓慢的方法
python 怎么使用pip进行包管理
python type()函数专题——动态创建类

Python 更多内容

Python 备忘单
Python有什么岗位?
Python 性能分析器入门教程

python第三方库

Python3 操作excel库总结

数据分析相关

Python3 xlwt库基本教程

未入册文章

Python3 randrange() 函数
Python3 ceil() 函数
Python3 log() 函数
Python3 floor() 函数
Python3 choice() 函数
Python3 log10() 函数
Python3 fabs() 函数
Python3 sqrt()方法 - 求平方根
Python3 exp() 函数
Python3 modf() 函数
PyCharm安装与配置

python 内置函数

Python3 round() 函数 -返回浮点数x的四舍五入值
Python3 abs() 函数-求一个数的绝对值
Python3 pow() 函数 -求幂
Python3 max() 函数 -求最大值
Python3 min() 函数 -求最小值
Python3 len()方法 - 返回对象长度
Python3 zip() 函数
Python3 all() 函数 - 指示列表中的所有元素是否都为真
Python3 iter() 函数 -生成迭代器
Python3 dir() 函数 -返回指定对象的所有属性和方法
Python3 eval() 函数 -执行一句python表达式
Python3 any() 函数 - 指示列表中的所有元素是否都为假
Python3 type() 函数 - 判断对象的类型
Python3 isinstance() 函数 -判断一个对象是否是一个已知的类型
Python3 print()函数 -打印输出内容
Python3 help() 函数 -查看函数或模块用途
Python3 id()函数 -返回对象的唯一标识符

python3 math模块

Python3 math.e常量 - 自然常数e
Python3 math.inf常量 - 无穷大常量
Python3 math.nan常量 - 非法数字
Python3 math.pi常量 - 圆周率π
Python3 math.tau常量 -两倍圆周率τ
Python3 math.acos()方法 -求反余弦值
Python3 math.acosh()方法 - 求反双曲余弦值
Python3 math.asin()方法 -求反正弦值
Python3 math.asinh()方法-求反双曲正弦值
Python3 math.atan()方法 -求反正切值
Python3 math.atan2()方法 -求y/x的反正切值
Python3 math.atanh()方法 - 求反双曲正切值
Python3 math.ceil()方法 - 向上取整
Python3 math.floor() 方法 -向下取整
Python3 math.comb()方法 -计算组合数
Python3 math.copysign()方法 -返回固定符号的值
Python3 math.cos()方法 - 求余弦值
Python3 math.cosh() 方法 -求双曲余弦值
Python3 math.degrees()方法 - 弧度转化为角度
Python3 math.radians() 方法 - 角度转化为弧度
Python3 math.dist()方法 - 求欧几里得距离
Python3 math.erf() 方法 - 求一个数的误差函数值
Python3 math.erfc()方法 - 求一个数的互补误差函数
Python3 math.exp()方法 -求e 的 x 次幂
Python3 math.expm1()方法 -求e 的 x 次幂(次方)减 1
Python3 math.fabs()方法 - 求绝对值
Python3 math.factorial()方法 -求阶乘
Python3 math.fmod() 方法 -求余数
Python3 math.frexp()方法 -求尾数和指数
Python3 math.fsum() 方法 - 求所有元素的总和
Python3 math.gamma() 方法 -求伽马函数值
Python3 math.gcd() 方法 - 求最大公约数
Python3 math.hypot() 方法 - 求欧几里得范数
Python3 math.isclose()方法 - 判断两个值是否接近
Python3 math.isfinite() 方法 - 判断一个数是否有限
Python3 math.isinf() 方法 -判断是否为无穷大
Python3 math.isnan() 方法 -判断一个数是否是NAN
Python3 math.isqrt() 方法 -求平方根的整数值
Python3 math.ldexp() 方法 - 求x乘2的i次方
Python3 math.lgamma() 方法 - 求自然对数伽玛值
Python3 math.log() 方法 - 求一个数的对数
Python3 math.log10() 方法-求10 为底的对数
Python3 math.log1p() 方法 - 求1+x 的自然对数
Python3 math.log2() 方法 -求x 以 2 为底的对数
Python3 math.perm() 方法 -计算排列数
Python3 math.pow() 方法 - 求幂
Python3 math.prod() 方法 - 求积
Python3 math.remainder() 方法 -求余数
Python3 math.sin() 方法 -求正弦值
Python3 math.sinh() 方法 - 求双曲正弦值
Python3 math.sqrt() 方法 -求平方根
Python3 math.tan() 方法 -求正切值
Python3 math.tanh() 方法 - 求双曲正切值
Python3 math.trunc() 方法 -求浮点数的整数部分
Python3 math.modf()方法 -求浮点数的小数部分
Python3 math.ulp()方法 - 求浮点数的最小有效比特位
Python3 math.lcm()方法 -求最小公倍数

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }