新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python判斷鍵在不在字典里
python3 中采用 in 方法

#判斷字典中某個鍵是否存在
arr = {"int":"整數(shù)","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":"整數(shù)","float":"浮點","str":"字符串","list":"列表","tuple":"元組","dict":"字典","set":"集合"}
#使用自帶的has_key()
if(arr.has_key("int")):
print("存在") 本文名稱:創(chuàng)新互聯(lián)Python教程:python判斷鍵在不在字典里
文章鏈接:http://fisionsoft.com.cn/article/cosoeps.html


咨詢
建站咨詢
