Python如何使用Any和All?代码示例和解析
Any和All是python中提供的两个内置插件, 用于连续的And/Or。
Any
如果任何一项为True, 则返回true。如果为空或全部为假, 则返回False。可以将任何内容视为对提供的可迭代对象进行的一系列OR操作。
它将执行短路, 即一旦知道结果就立即停止执行。
语法:any(迭代列表)
# Since all are false, false is returned
print ( any ([ False , False , False , False ]))# Here the method will short-circuit at the
# second item (True) and will return True.
print ( any ([ False , True , False , False ]))# Here the method will short-circuit at the
# first (True) and will return True.
print ( any ([ True , False , False , False ]))
输出:
False
True
True
All
如果所有项目均为True(或iterable为空), 则返回true。可以将所有内容视为对提供的可迭代对象的一系列AND操作。这也使执行短路, 即一旦知道结果就立即停止执行。
语法:all(迭代列表)
# Here all the iterables are True so all
# will return True and the same will be printed
print ( all ([ True , True , True , True ]))# Here the method will short-circuit at the
# first item (False) and will return False.
print ( all ([ False , True , True , False ]))# This statement will return False, as no
# True is found in the iterables
print ( all ([ False , False , False ]))
输出:
True
False
False
实际例子
# This code explains how can we
# use 'any' function on list
list1 = []
list2 = []# Index ranges from 1 to 10 to multiply
for i in range ( 1 , 11 ):list1.append( 4 * i) # Index to access the list2 is from 0 to 9
for i in range ( 0 , 10 ):list2.append(list1[i] % 5 = = 0 )print ( 'See whether at least one number is divisible by 5 in list 1=>' )
print ( any (list2))
输出如下:
See whether at least one number is divisible by 5 in list 1=>
True
# Illustration of 'all' function in python 3# Take two lists
list1 = []
list2 = []# All numbers in list1 are in form: 4*i-3
for i in range ( 1 , 21 ):list1.append( 4 * i - 3 )# list2 stores info of odd numbers in list1
for i in range ( 0 , 20 ):list2.append(list1[i] % 2 = = 1 )print ( 'See whether all numbers in list1 are odd =>' )
print ( all (list2))
输出如下:
See whether all numbers in list1 are odd =>
True
真值表:-
如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
更多Python开发技术相关内容请参考:lsbin - IT开发技术:https://www.lsbin.com/
查看以下更多Python相关的内容:
- Python进程同步和池化:https://www.lsbin.com/3326.html
- Python如 Kivy的AnchorLayout布局:https://www.lsbin.com/3036.html
- Python下载视频:https://www.lsbin.com/3013.html
Python如何使用Any和All?代码示例和解析相关推荐
- python turtle画彩虹-Python利用turtle库绘制彩虹代码示例_天津SEO
天津SEO RGB模型:光的三原色,共同决定色相 HSB/HSV模型:H色彩,S深浅,B饱和度,H决定色相 需要将HSB模型转换为RGB模型 代码示例: #-*- coding:utf-8 –*- f ...
- python画图代码彩虹-Python利用turtle库绘制彩虹代码示例
语言:Python IDE:Python.IDE 需求 做出彩虹效果 颜色空间 RGB模型:光的三原色,共同决定色相 HSB/HSV模型:H色彩,S深浅,B饱和度,H决定色相 需要将HSB模型转换为R ...
- python查天气预报_一个用Python编写抓取天气预报的代码示例
Python代码抓取获取天气预报信息源码讲解.这是一个用Python编写抓取天气预报的代码示例,用python写天气查询软件程序很简单.这段代码可以获取当地的天气和.任意城市的天气预报,原理是根据ur ...
- python短信接口_基于 python 的短信接口调用代码示例模板
阅信短信验证码平台最近会从新梳理基于不同语言的短信接口调用代码示例,是为了迎合市面上现在流行的各个语言代码实现,也是为了能够更好的服务满足客户的不同层次的需求. 下面的代码是基于 python 的短信 ...
- python把图片转化为素描画_python如何将图片转换素描画 python将图片转换素描画代码示例...
python如何将图片转换素描画?本篇文章小编给大家分享一下python将图片转换素描画代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看. 代码如 ...
- doc python 颜色_Python wordcloud.ImageColorGenerator方法代码示例
本文整理汇总了Python中wordcloud.ImageColorGenerator方法的典型用法代码示例.如果您正苦于以下问题:Python wordcloud.ImageColorGenerat ...
- python asyncio future_Python asyncio.isfuture方法代码示例
本文整理汇总了Python中asyncio.isfuture方法的典型用法代码示例.如果您正苦于以下问题:Python asyncio.isfuture方法的具体用法?Python asyncio.i ...
- python operator __gt___Python operator.gt方法代码示例
本文整理汇总了Python中operator.gt方法的典型用法代码示例.如果您正苦于以下问题:Python operator.gt方法的具体用法?Python operator.gt怎么用?Pyth ...
- python爬虫之多线程、多进程+代码示例
python爬虫之多线程.多进程 使用多进程.多线程编写爬虫的代码能有效的提高爬虫爬取目标网站的效率. 很多人学习python,不知道从何学起. 很多人学习python,掌握了基本语法过后,不知道在哪 ...
- python socket connection_Python socket.create_connection方法代码示例
本文整理汇总了Python中gevent.socket.create_connection方法的典型用法代码示例.如果您正苦于以下问题:Python socket.create_connection方 ...
最新文章
- Pixel 3的超分辨变焦技术
- python中weekday_[转载] Python日历模块| 使用示例的weekday()方法
- 将connection存放在Threadlocal里和数据库连接池的区别
- 爬虫之 单线程+多任务异步协程
- 2022年浙江省电子设计大赛C题小车跟随系统
- 内容安全策略(CSP)详解
- 多功能雨伞项目计划书_雨伞策划书范文
- linux char大小,Linux基本数据类型大小——int,char,long int,long long int
- 三、生成树协议(Spanning Tree Protocol,STP)
- p5.js 和 Processing 的恩怨情仇
- 微信小程序创建项目后无法显示内容
- s:iterator各种遍历用法
- 嵌入式系统的应用领域
- 异步复位同步释放原理
- 计算机网络实验:华为交换机的常用命令
- C++一本通1086(角谷猜想)
- 利用七参数进行CGCS2000坐标系到西安80坐标系的转换
- Metasploit渗透框架介绍及永恒之蓝复现
- 专家呼吁禁止杀手机器人
- 长春师范大学计算机学院宿舍,长春师范大学宿舍条件,宿舍环境图片(10篇)