码迷,mamicode.com
首页 > 编程语言 > 详细

python中判断字典中是否存在某个键

时间:2018-07-19 13:49:37      阅读:381      评论:0      收藏:0      [点我收藏+]

标签:字符   python3   nbsp   tuple   存在   style   lis   key   自带   

python3 中采用 in 方法

 1 #判断字典中某个键是否存在
 2 arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"}
 3 #使用 in 方法
 4 if "int" in arr:
 5     print("存在")
 6 if "float" in arr.keys():
 7     print("存在")
 8 #判断键不存在
 9 if "floats" not in arr:
10     print("不存在")
11 if "floats" not in arr:
12     print("不存在")
13     

python 3中不支持 has_key(),python2 中支持

#判断字典中某个键是否存在
arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"}
#使用自带的has_key()
if(arr.has_key("int")):
    print("存在")

 

python中判断字典中是否存在某个键

标签:字符   python3   nbsp   tuple   存在   style   lis   key   自带   

原文地址:https://www.cnblogs.com/xmnote/p/9334880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!