新聞中心
這篇文章給大家分享的是有關(guān)python中如何實(shí)現(xiàn)dict的元素取值的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
如下所示:
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)
感謝各位的閱讀!關(guān)于“python中如何實(shí)現(xiàn)dict的元素取值”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
本文題目:python中如何實(shí)現(xiàn)dict的元素取值-創(chuàng)新互聯(lián)
本文來(lái)源:http://fisionsoft.com.cn/article/dechei.html