如下所示:
dict.get(key, default=None)
key – 字典中要查找的鍵。
default – 如果指定鍵的值不存在時(shí),返回該默認(rèn)值值。
{'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'
補(bǔ)充:Python字典鍵的取值和字典值的取值方法
Python字典,因?yàn)樽值涫强勺冾愋蛿?shù)據(jù),允許對(duì)字典進(jìn)行取值。
對(duì)鍵的取值方法,使用keys()函數(shù)。
程序?qū)嵗?:
使用keys()函數(shù)取鍵名,并轉(zhuǎn)換為列表。
dict_val = {'及時(shí)雨':"宋江",'花和尚':'魯智深','母夜叉':'孫二娘'}
key = dict_val.keys()
print(key)
print(list(key))
print(list(key)[1])

對(duì)字典的值進(jìn)行取值操作,用values()函數(shù)。
程序?qū)嵗?:
用values()函數(shù)對(duì)字典的值進(jìn)行取值操作,并轉(zhuǎn)化為列表。
dict_val = {'及時(shí)雨':"宋江",'花和尚':'魯智深','母夜叉':'孫二娘'}
value = dict_val.values()
print(value)
print(list(value))
print(list(value)[1])

對(duì)字典的元素進(jìn)行取值,包括鍵名及其對(duì)應(yīng)的值,使用items()函數(shù)。
程序?qū)嵗?:
使用items()函數(shù)對(duì)字典的元素進(jìn)行取值操作。
dict_val = {'及時(shí)雨':"宋江",'花和尚':'魯智深','母夜叉':'孫二娘'}
item = dict_val.items()
print(item)
print(list(item))
print(list(item)[1])
key,value = list(item)[1]
print(key)
print(value)

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