Python之dict詳解

Python字典是另一種可變?nèi)萜髂P?無(wú)序),且可存儲(chǔ)任意類型對(duì)象,如字符串、數(shù)字、元組等其他容器模型。

本次主要介紹Python中字典(Dict)的詳解操作方法,包含創(chuàng)建、訪問、刪除、其它操作等,需要的朋友可以參考下。

字典由鍵和對(duì)應(yīng)值成對(duì)組成。字典也被稱作關(guān)聯(lián)數(shù)組或哈希表?;菊Z(yǔ)法如下:

1.創(chuàng)建字典


>>> dict = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'}

技巧:

字典中包含列表:dict={'yangrong':['23','IT'],"xiaohei":['22','dota']}

字典中包含字典:dict={'yangrong':{"age":"23","job":"IT"},"xiaohei":{"'age':'22','job':'dota'"}}

注意:

每個(gè)鍵與值用冒號(hào)隔開(:),每對(duì)用逗號(hào),每對(duì)用逗號(hào)分割,整體放在花括號(hào)中({})。

鍵必須獨(dú)一無(wú)二,但值則不必。

2.訪問字典里的值


>>> dict={'ob1':'computer',

? 'ob2':'mouse', 'ob3':'printer'}

>>> print(dict['ob1'])

computer

如果用字典里沒有的鍵訪問數(shù)據(jù),會(huì)輸出錯(cuò)誤如下:

>>> print(dict['ob4'])

Traceback (most recent call last):

?File"", line 1, in

??print(dict['ob4'])

訪問所有值

>>> dict1 ={'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'}

>>> forkey indict1:

??print(key,dict1[key])?

ob3 printer

ob2 mouse

ob1 computer

3.修改字典


>>> dict={'ob1':'computer',

? 'ob2':'mouse', 'ob3':'printer'}

>>> dict['ob1']='book'

>>> print(dict)

{'ob3': 'printer', 'ob2': 'mouse', 'ob1': 'book'}

4.刪除字典


能刪單一的元素

>>> dict={'ob1':'computer',

? 'ob2':'mouse', 'ob3':'printer'}

>>> deldict['ob1']

>>> print(dict)

{'ob3': 'printer', 'ob2': 'mouse'}

刪除字典中所有元素

>>> dict1={'ob1':'computer','ob2':'mouse','ob1':'printer'}

>>> dict1.clear()

>>> print(dict1)

{}

刪除整個(gè)字典,刪除后訪問字典會(huì)拋出異常。

>>> dict1 ={'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'}

>>> deldict1

>>> print(dict1)

Traceback (most recent call last):

?File"", line 1, in

??print(dict1)

NameError: name 'dict1'isnotdefined

5.更新字典


update()方法可以用來(lái)將一個(gè)字典的內(nèi)容添加到另外一個(gè)字典中:

>>> dict1 ={'ob1':'computer', 'ob2':'mouse'}

>>> dict2={'ob3':'printer'}

>>> dict1.update(dict2)

>>> print(dict1)

{'ob3': 'printer', 'ob2': 'mouse', 'ob1': 'computer'}

6.映射類型相關(guān)的函數(shù)


>>> dict(x=1, y=2)

{'y': 2, 'x': 1}

>>> dict8 =dict(x=1, y=2)

>>> dict8

{'y': 2, 'x': 1}

>>> dict9 =dict(**dict8)

>>> dict9

{'y': 2, 'x': 1}


dict9 =dict8.copy()

7.字典鍵的特性


字典值可以沒有限制地取任何python對(duì)象,既可以是標(biāo)準(zhǔn)的對(duì)象,也可以是用戶定義的,但鍵不行。

兩個(gè)重要的點(diǎn)需要記住:

1)不允許同一個(gè)鍵出現(xiàn)兩次。創(chuàng)建時(shí)如果同一個(gè)鍵被賦值兩次,后一個(gè)值會(huì)被記住?

>>> dict1={'ob1':'computer','ob2':'mouse','ob1':'printer'}

>>> print(dict1)

{'ob2': 'mouse', 'ob1': 'printer'}


2)鍵必須不可變,所以可以用數(shù),字符串或元組充當(dāng),用列表就不行

>>> dict1 ={['ob1']:'computer', 'ob2':'mouse', 'ob3':'printer'}

Traceback (most recent call last):

?File"", line 1, in

??dict1 ={['ob1']:'computer', 'ob2':'mouse', 'ob3':'printer'}

TypeError: unhashable type: 'list'

8.字典內(nèi)置函數(shù)&方法


Python字典包含了以下內(nèi)置函數(shù):

1、cmp(dict1, dict2):比較兩個(gè)字典元素。(python3后不可用)

2、len(dict):計(jì)算字典元素個(gè)數(shù),即鍵的總數(shù)。

3、str(dict):輸出字典可打印的字符串。

4、type(variable):返回輸入的變量類型,如果變量是字典就返回字典類型。

Python字典包含了以下內(nèi)置方法:

1、radiansdict.clear():刪除字典內(nèi)所有元素

2、radiansdict.copy():返回一個(gè)字典的淺復(fù)制

3、radiansdict.fromkeys():創(chuàng)建一個(gè)新字典,以序列seq中元素做字典的鍵,val為字典所有鍵對(duì)應(yīng)的初始值

4、radiansdict.get(key, default=None):返回指定鍵的值,如果值不在字典中返回default值

5、radiansdict.has_key(key):如果鍵在字典dict里返回true,否則返回false

6、radiansdict.items():以列表返回可遍歷的(鍵, 值) 元組數(shù)組

7、radiansdict.keys():以列表返回一個(gè)字典所有的鍵

8、radiansdict.setdefault(key, default=None):和get()類似, 但如果鍵不已經(jīng)存在于字典中,將會(huì)添加鍵并將值設(shè)為default

9、radiansdict.update(dict2):把字典dict2的鍵/值對(duì)更新到dict里

10、radiansdict.values():以列表返回字典中的所有值

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容