如下所示:
dict.get(key, default=None)
key – 字典中要查找的鍵。
default – 如果指定鍵的值不存在時,返回該默認值值。
{'1*': 9, '2*': 6, '**': 15}.values()
Out[377]: dict_values([9, 6, 15])
{'1*': 9, '2*': 6, '**': 15}.keys()
Out[378]: dict_keys(['1*', '2*', '**'])
{'1*': 9, '2*': 6, '**': 15}.items()
Out[379]: dict_items([('1*', 9), ('2*', 6), ('**', 15)])
{'1*': 9, '2*': 6, '**': 15}.get('1*')
Out[380]: 9
{'1*': 9, '2*': 6, '**': 15}.get('00','whatever')
Out[381]: 'whatever'
補充:Python字典鍵的取值和字典值的取值方法
Python字典,因為字典是可變類型數據,允許對字典進行取值。
對鍵的取值方法,使用keys()函數。
程序實例1:
使用keys()函數取鍵名,并轉換為列表。
dict_val = {'及時雨':"宋江",'花和尚':'魯智深','母夜叉':'孫二娘'}
key = dict_val.keys()
print(key)
print(list(key))
print(list(key)[1])

對字典的值進行取值操作,用values()函數。
程序實例2:
用values()函數對字典的值進行取值操作,并轉化為列表。
dict_val = {'及時雨':"宋江",'花和尚':'魯智深','母夜叉':'孫二娘'}
value = dict_val.values()
print(value)
print(list(value))
print(list(value)[1])

對字典的元素進行取值,包括鍵名及其對應的值,使用items()函數。
程序實例3:
使用items()函數對字典的元素進行取值操作。
dict_val = {'及時雨':"宋江",'花和尚':'魯智深','母夜叉':'孫二娘'}
item = dict_val.items()
print(item)
print(list(item))
print(list(item)[1])
key,value = list(item)[1]
print(key)
print(value)

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- Python 實現list,tuple,str和dict之間的相互轉換
- 解決python中set與dict的無序問題
- 詳解Python 中的 defaultdict 數據類型
- python的dict判斷key是否存在的方法
- Python字典dict常用方法函數實例
- python Yaml、Json、Dict之間的轉化
- Python xmltodict模塊安裝及代碼實例