python中判斷字典中是否存在某個鍵


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("存在")

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM