python判断元素是否存在的实例方法
作者:FXL 时间:2021-02-24 23:20:08
可以利用in运算符来进行判断,如果在指定的序列中找到值返回True,否则返回False。
运算符not in表示如果在指定的序列中没有找到值返回 True,否则返回 False。
举例:
#-*- coding:utf-8 -*- python 3.6.2
lista=[1,'5','s','cf']
if 1 in lista:
print('1 在列表lista中')
if '1' in lista:
print('"11" 在列表lista中')
if 'cf' in lista:
print('cf 在列表lista中')
if 'ss' in lista:
print('ss 在列表lista中')
运算结果:
1 在列表lista中
cf 在列表lista中
实例扩展:
实例 1
test_list = [ 1, 6, 3, 5, 3, 4 ]
print("查看 4 是否在列表中 ( 使用循环 ) : ")
for i in test_list:
if(i == 4) :
print ("存在")
print("查看 4 是否在列表中 ( 使用 in 关键字 ) : ")
if (4 in test_list):
print ("存在")
以上实例输出结果为:
查看 4 是否在列表中 ( 使用循环 ) :
存在
查看 4 是否在列表中 ( 使用 in 关键字 ) :
存在
来源:https://www.py.cn/jishu/jichu/20232.html
标签:python,判断元素
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ASP程序中使用断开的数据记录集
2007-10-17 18:51:00
python 数据提取及拆分的实现代码
2023-11-13 09:13:12
![](https://img.aspxhome.com/file/2023/1/63581_0s.png)
Python部署web开发程序的几种方法
2023-08-24 06:30:32
Python matplotlib生成图片背景透明的示例代码
2022-07-04 06:22:57
![](https://img.aspxhome.com/file/2023/2/81472_0s.png)
对TensorFlow中的variables_to_restore函数详解
2022-09-11 00:49:19
Python实现快速傅里叶变换的方法(FFT)
2022-09-18 07:21:47
![](https://img.aspxhome.com/file/2023/3/77873_0s.png)
浅谈解除装饰器作用(python3新增)
2022-05-30 03:31:52
FrontPage XP设计教程5——表单的设计
2008-10-11 12:35:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/2008101712380183s.jpg)
用Python实现命令行闹钟脚本实例
2023-01-19 12:57:09
Python Opencv 通过轨迹(跟踪)栏实现更改整张图像的背景颜色
2021-12-03 04:04:53
![](https://img.aspxhome.com/file/2023/7/92457_0s.png)
现代 javscript 编程
2011-04-21 16:24:00
![](https://img.aspxhome.com/file/uploadpic/20075/2007511165112755.gif)
python 实现手机自动拨打电话的方法(通话压力测试)
2021-03-19 08:10:34
pandas使用fillna函数填充NaN值的代码实例
2023-09-29 05:51:48
Python中字符串类型代码的执行函数——eval()、exec()和compile()详解
2022-07-08 09:14:12
python获取指定字符串中重复模式最高的字符串方法
2021-03-25 14:59:25
php返回相对时间(如:20分钟前,3天前)的方法
2023-10-26 11:20:38
Python PaddleNLP开源实现快递单信息抽取
2023-01-21 04:35:11
![](https://img.aspxhome.com/file/2023/3/80603_0s.jpg)
Python数据分析之Numpy库的使用详解
2021-06-14 02:54:53
![](https://img.aspxhome.com/file/2023/7/103627_0s.png)
编写Python脚本批量下载DesktopNexus壁纸的教程
2022-05-27 01:17:38
Python中的lambda和apply用法及说明
2023-08-12 14:56:46