python字典dict中常用內(nèi)置函數(shù)的使用
前言
字典是 Python 中很重要的數(shù)據(jù)類型,有很多內(nèi)置函數(shù)需要了解。
1.dict.clear
清除字典中所有鍵值對(duì)。
dict = {'a':10, 'b':20, 'c':30} dict.clear() print(dict) # {}
2.dict.get
如果鍵存在于字典中,則返回該鍵的值。
如果未找到,則返回 None。
指定可選參數(shù)之后,未找到返回默認(rèn)值。
dict = {'a':10, 'b':20, 'c':30} print(dict.get('c')) # 30 print(dict.get('g')) # None print(dict.get('g', -1)) # -1 指定可選參數(shù)
3.dict.items
返回字典中的鍵值對(duì)列表。
items() 返回包含鍵值對(duì)的元組列表。
每個(gè)元組中的第一項(xiàng)是鍵,第二項(xiàng)是鍵的值。
dict = {'a':10, 'b':20, 'c':30} dict.items() # dict_items([('a', 10), ('b', 20), ('c', 30)]) list(dict.items()) # [('a', 10), ('b', 20), ('c', 30)] list(dict.items())[1] # ('b', 20) list(dict.items())[1][0] # 'b'
4.dict.keys
返回字典中的鍵列表。
dict = {'a':10, 'b':20, 'c':30}
dict.keys() # dict_keys(['a', 'b', 'c'])
list(dict.keys()) # ['a', 'b', 'c']
5.dict.values
返回字典中的值列表。
dict = {'a':10, 'b':20, 'c':30} dict.values() # dict_values([10, 20, 30]) list(dict.values()) # [10, 20, 30] # 即使值重復(fù),也會(huì)被多次返回 dict2 = {'a':10, 'b':10, 'c':30} list(dict2.values()) # [10, 10, 30]
6.dict.pop
從字典中刪除一個(gè)鍵,如果它存在,并返回它的值。
如果不存在,則引發(fā)異常 KeyError。
指定可選參數(shù),不存在時(shí)返回默認(rèn)值,不引發(fā)異常。
dict = {'a':10, 'b':20, 'c':30} dict.pop('b') # 20 print(dict) # {'a': 10, 'c': 30} dict.pop('g') ''' Traceback (most recent call last): ? File "<ipython-input-20-a81e983a7be0>", line 1, in <module> ? ? dict.pop('g') KeyError: 'g' ''' dict.pop('g', -1) # -1
7.dict.popitem
從字典中刪除最后面的鍵值對(duì),并返回。
直到字典被刪除至空,則引發(fā)異常 KeyError。
dict = {'a':10, 'b':20, 'c':30} dict.popitem() # ('c', 30) print(dict) # {'a': 10, 'b': 20} dict.popitem() # ('b', 20) print(dict) # {'a': 10} dict.popitem() # ('a', 10) print(dict) # {} dict.popitem() ''' Traceback (most recent call last): ? File "<ipython-input-28-7e744445e3d2>", line 1, in <module> ? ? dict.popitem() KeyError: 'popitem(): dictionary is empty' '''
**注意:**在低于 3.6 的 Python 版本中,popitem( ) 將返回任意(隨機(jī))鍵值對(duì),因?yàn)?Python 字典在 3.6 版本之前是無(wú)序的。
8.dict.update
將字典與另一個(gè)字典或可迭代的鍵值對(duì)合并。
dict = {'a':10, 'b':20, 'c':30} dict2 = {'b':200, 'd':400} dict.update(dict2) print(dict) # {'a': 10, 'b': 200, 'c': 30, 'd': 400}
所有的值都被更新。
尾語(yǔ) ??
到此這篇關(guān)于python字典dict中常用內(nèi)置函數(shù)的使用的文章就介紹到這了,更多相關(guān)python字典dict內(nèi)置函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 圖像插值 最近鄰、雙線性、雙三次實(shí)例
這篇文章主要介紹了python 圖像插值 最近鄰、雙線性、雙三次實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07python版本的仿windows計(jì)劃任務(wù)工具
這篇文章主要介紹了python版本的仿windows計(jì)劃任務(wù)工具,計(jì)劃任務(wù)工具根據(jù)自己設(shè)定的具體時(shí)間,頻率,命令等屬性來(lái)規(guī)定所要執(zhí)行的計(jì)劃,當(dāng)然功能不是很全大家可以補(bǔ)充2018-04-04Python語(yǔ)法之精妙的十個(gè)知識(shí)點(diǎn)(裝B語(yǔ)法)
本文精心篩選了最能展現(xiàn) Python 語(yǔ)法之精妙的十個(gè)知識(shí)點(diǎn),并附上詳細(xì)的實(shí)例代碼,需要的朋友可以參考下2020-01-01opencv 實(shí)現(xiàn)特定顏色線條提取與定位操作
這篇文章主要介紹了opencv 實(shí)現(xiàn)特定顏色線條提取與定位操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06解決vscode python print 輸出窗口中文亂碼的問(wèn)題
今天小編就為大家分享一篇解決vscode python print 輸出窗口中文亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12Python實(shí)現(xiàn)PS圖像抽象畫風(fēng)效果的方法
這篇文章主要介紹了Python實(shí)現(xiàn)PS圖像抽象畫風(fēng)效果的方法,涉及Python基于skimage模塊進(jìn)行圖像處理的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01