-
方式一:
has_key()
,在python2.2
之前已經(jīng)被放棄,所以推薦使用其他方式。dict = { 'name': 'dzm', 'age': '20' } print(dict.has_key('name')) # True print(dict.has_key('id')) # False
-
方式二:
keys()
,需要in
配合使用,也可以使用not in
文章來源:http://www.zghlxwxcb.cn/news/detail-536084.htmldict = { 'name': 'dzm', 'age': '20' } print('name' in dict.keys()) # True print('id' in dict.keys()) # False print('id' not in dict.keys()) # True
-
方式三:
in
或not in
,【推薦使用】
文章來源地址http://www.zghlxwxcb.cn/news/detail-536084.htmldict = { 'name': 'dzm', 'age': '20' } print('name' in dict) # True print('id' in dict) # False print('id' not in dict) # True
到了這里,關(guān)于Python 判斷字典中 key 是否存在(三種方式)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!