字典
字典及其使用
在Python中,字典是一系列鍵—值對(duì)。每個(gè)鍵都與一個(gè)值相關(guān)聯(lián),你可以使用鍵來(lái)訪(fǎng)問(wèn)與之 相關(guān)聯(lián)的值。與鍵相關(guān)聯(lián)的值可以是數(shù)字、字符串、列表乃至字典。事實(shí)上,可將任何Python對(duì)象用作字典中的值。
在Python中,字典用放在花括號(hào){}中的一系列鍵—值對(duì)表示。
鍵—值對(duì)是兩個(gè)相關(guān)聯(lián)的值。指定鍵時(shí),Python將返回與之相關(guān)聯(lián)的值。鍵和值之間用冒號(hào)(:)分隔,而鍵—值對(duì)之間用逗號(hào)(,)分隔。在字典中,你想存儲(chǔ)多少個(gè)鍵—值對(duì)都可以。最簡(jiǎn)單的字典只有一個(gè)鍵—值對(duì)。
alien_0 = {'color': 'green', 'points': 5}
print(alien_0['color'])
print(alien_0['points'])
訪(fǎng)問(wèn)字典的值
要獲取與鍵相關(guān)聯(lián)的值,可依次指定字典名和放在方括號(hào)內(nèi)的鍵。
alien_0 = {'color': 'green'}
print(alien_0['color'])
字典中可包含任意數(shù)量的鍵—值對(duì)。
alien_0 = {'color': 'green', 'points': 5}
添加鍵-值對(duì)
字典是一種動(dòng)態(tài)結(jié)構(gòu),可隨時(shí)在其中添加鍵—值對(duì)。要添加鍵—值對(duì),可依次指定字典名、用 方括號(hào)括起的鍵和相關(guān)聯(lián)的值。
alien_0 = {'color': 'green', 'points': 5}
print(alien_0)
alien_0['y_position'] = 25
print(alien_0)
運(yùn)行結(jié)果:
{'color': 'green', 'points': 5} 13
{'color': 'green', 'points': 5, 'y_position': 25}
注意,鍵—值對(duì)的排列順序與添加順序不同。Python不關(guān)心鍵—值對(duì)的添加順序, 而只關(guān)心鍵和值之間的關(guān)聯(lián)關(guān)系。
創(chuàng)建一個(gè)空字典
方法:用一對(duì)空的花括號(hào){}定義一個(gè)字典,再分行添加各個(gè)鍵—值對(duì)。使用字典來(lái)存儲(chǔ)用戶(hù)提供的數(shù)據(jù)或在編寫(xiě)能自動(dòng)生成大量鍵—值對(duì)的代碼時(shí),通常都需要先 定義一個(gè)空字典。
代碼:
>>> num={}
>>> num['one']=1
>>> num['two']=2
>>> num['three']=3
>>> print(num)
結(jié)果:
{'one': 1, 'two': 2, 'three': 3}
修改字典中的值
要修改字典中的值,可依次指定字典名、用方括號(hào)括起的鍵以及與該鍵相關(guān)聯(lián)的新值。
代碼示例:
>>> num={'one': 1, 'two': 2, 'three': 3}
>>> num['one']=0
>>> print(num)
運(yùn)行結(jié)果:
{'one': 0, 'two': 2, 'three': 3}
>>> num={'one':"1",'two':'2','three':'3'}
>>> num['one']="0"
>>> print("one 的值是:" + num['one'] + '!')
運(yùn)行結(jié)果:
one 的值是:0!
注意:再修該鍵值對(duì)的值時(shí),必須用
引號(hào)把值包起來(lái),否則加修飾語(yǔ)打印時(shí)會(huì)報(bào)錯(cuò),正常打印則不會(huì)
會(huì)報(bào)錯(cuò)的情況
刪除鍵—值對(duì)
對(duì)于字典中不再需要的信息,可使用del語(yǔ)句將相應(yīng)的鍵—值對(duì)徹底刪除。使用del語(yǔ)句時(shí), 必須指定字典名和要?jiǎng)h除的鍵。
{'one': 1, 'two': 2, 'three': 3}
>>> num={'one':"1",'two':'2','three':'3'}
>>> del num['one']
>>> print(num)
運(yùn)行結(jié)果:
{'two': '2', 'three': '3'}
字典使用
遍歷字典
一個(gè)Python字典可能只包含幾個(gè)鍵—值對(duì),也可能包含數(shù)百萬(wàn)個(gè)鍵—值對(duì)。鑒于字典可能包含大量的數(shù)據(jù),Python支持對(duì)字典遍歷。字典可用于以各種方式存儲(chǔ)信息,因此有多種遍歷字典的方式:可遍歷字典的所有鍵—值對(duì)、鍵或值。
遍歷所有的鍵-值對(duì)
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}
for key, value in user_0.items():
print("\nKey: " + key)
print("Value: " + value)
運(yùn)行結(jié)果:
Key:username
Value:efermi
Key:first
Value:enrico
Key:last
Value:fermi
for語(yǔ)句的第二部分包含字典名和方法items(),它返回一個(gè)鍵—值對(duì)列表。
遍歷字典中的所有鍵
keys()獲取字典的所有值。
代碼示例:
>>> list={
"color":'red',
"width":'100',
"height":'50',
}
>>> for name in list.keys():
print(name)```
運(yùn)行結(jié)果:
color
width
height
遍歷字典中的所有值
values()獲取字典的所有值。set()可以去除重復(fù)的值。
代碼示例:
>>> list={
"color":'red',
"width":'100',
"height":'50',
"background":'red',
}
>>> for name in list.values():
print(name)
#去重復(fù)的值
>>> for name in set(list.values()):
print(name)
運(yùn)行結(jié)果:
red
100
50
red
#去重復(fù)的值
red
100
50
嵌套
有時(shí)候,需要將一系列字典存儲(chǔ)在列表中,或?qū)⒘斜碜鳛橹荡鎯?chǔ)在字典中,這稱(chēng)為嵌套??梢栽诹斜碇星短鬃值?、在字典中嵌套列表甚至在字典中嵌套字典。
字典列表
代碼示例:
alien_0 = {'color': 'green', 'points': 5}
alien_1 = {'color': 'yellow', 'points': 10}
alien_2 = {'color': 'red', 'points': 15}
aliens = [alien_0, alien_1, alien_2]
for alien in aliens:
print(alien)
運(yùn)行結(jié)果:
{'color': 'green', 'points': 5}
{'color': 'yellow', 'points': 10}
{'color': 'red', 'points': 15}
在字典中存儲(chǔ)列表
每當(dāng)需要在字典中將一個(gè)鍵關(guān)聯(lián)到多個(gè)值時(shí),都可以在字典中嵌套一個(gè)列表。
代碼示例:
pizza = {
'crust': 'thick',
'toppings': ['mushrooms', 'extra cheese'],
}
列表和字典的嵌套層級(jí)不應(yīng)太多。如果嵌套層級(jí)比前面的示例多得多,很可能有更簡(jiǎn)單的解決問(wèn)題的方案。
在字典中存儲(chǔ)字典
代碼示例:
users = { 'aeinstein': {
'first': 'albert',
'last': 'einstein',
'location': 'princeton',
},
'mcurie': {
'first': 'marie',
'last': 'curie',
'location': 'paris',
},
}
print(user)
運(yùn)行結(jié)果:
{'aeinstein':
{'first': 'albert', 'last': 'einstein', 'location': 'princeton'},
'mcurie': {'first': 'marie', 'last': 'curie', 'location': 'paris'}
}
首先定義了一個(gè)名為users的字典,其中包含兩個(gè)鍵:用戶(hù)名'aeinstein'和'mcurie'; 與每個(gè)鍵相關(guān)聯(lián)的值都是一個(gè)字典,其中包含用戶(hù)的名、姓和居住地。
