python3中采用in方法
#判斷字典中某個鍵是否存在
arr={"int":"整數","float":"浮點","str":"字符串","list":"列表","tuple":"元組","dict":"字典","set":"集合"}
#使用in方法
if"int"inarr:
print("存在")
if"float"inarr.keys():
print("存在")
#判斷鍵不存在
if"floats"notinarr:
print("不存在")
if"floats"notinarr:
print("不存在")
python3中不支持has_key(),python2中支持
#判斷字典中某個鍵是否存在
arr={"int":"整數","float":"浮點","str":"字符串","list":"列表","tuple":"元組","dict":"字典","set":"集合"}
#使用自帶的has_key()
if(arr.has_key("int")):
print("存在")
以上內容為大家介紹了python培訓之判斷鍵在不在字典里,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。