python判断键在不在字典里

641次阅读
没有评论

python判断键在不在字典里

python3 中采用 in 方法

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

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

#判断字典中某个键是否存在
arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"}
#使用自带的has_key()
if(arr.has_key("int")):
    print("存在")
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2019-11-18发表,共计471字。
新手QQ群:570568346,欢迎进群讨论 Python51学习
评论(没有评论)