python3中采用in方法
#判斷字典中某個(gè)鍵是否存在
arr={"int":"整數(shù)","float":"浮點(diǎn)","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中支持
#判斷字典中某個(gè)鍵是否存在
arr={"int":"整數(shù)","float":"浮點(diǎn)","str":"字符串","list":"列表","tuple":"元組","dict":"字典","set":"集合"}
#使用自帶的has_key()
if(arr.has_key("int")):
print("存在")
以上內(nèi)容為大家介紹了python培訓(xùn)之判斷鍵在不在字典里,希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。