一、字典的基本概念
下面是一個簡單的字典:
alien_0={'color':'green','points':5}
print(alien_0['color'])
print(alien_0['points'])
輸出如下:
green
5
二、字典的使用
1.訪問字典中的值
可參考一中的示例代碼
2.添加鍵-值對
字典是一種動態結構,可隨時在其中添加鍵-值對。可依次指定字典名、用方括號括起的鍵和相關聯的值。
以下代碼將在一中的代碼添加x與y座標。
alien_0={'color':'green','points':5}
print(alien_0['color'])
print(alien_0['points'])
alien_0['x_position']=0
alien_0['y_position']=25
print(alien_0)
則添加成功,輸出如下:
green
5
{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}
3.刪改字典的值
修改可通過直接賦值,刪除可通過del
動手試一試6-1-6-3(相同類型在此只列出6-1)
#6-1
people={'first_name':'xiaoming','last_name':'wang','age':20,'city':'Beijing'}
print(people)
輸出如下:
{'first_name': 'xiaoming', 'last_name': 'wang', 'age': 20, 'city': 'Beijing'}
三、遍歷字典
1.遍歷所有鍵-對
參考以下代碼:
people={'first_name':'xiaoming','last_name':'wang','age':'20','city':'Beijing'}
for key,value in people.items():
print("\nKey:"+key)
print("Value:"+value)
輸出如下:
Key:first_name
Value:xiaoming
Key:last_name
Value:wang
Key:age
Value:20
Key:city
Value:Beijing
2.遍歷所有鍵——方法keys()
參考如下代碼:
people={'first_name':'xiaoming','last_name':'wang','age':'20','city':'Beijing'}
for key in people.keys():
print(key)
輸出:
first_name
last_name
age
city
3.遍歷所有值——方法values()
參考如下代碼:
people={'first_name':'xiaoming','last_name':'wang','age':'20','city':'Beijing'}
for value in people.values():
print(value)
輸出如下:
xiaoming
wang
20
Beijing
4.按順序遍歷——函數sorted()
參考如下代碼:
people={'first_name':'d','last_name':'a','age':'c','city':'f'}
for value in sorted(people.values()):
print(value)
輸出如下:
a
c
d
f
動手試一試6-4 - 6-6
#6-4
#略
#6-5
messages={'Nile':'Egypt','Changjiang':'China','Amazon':'Brazil'}
for river,country in messages.items():
print("The "+river+" runs through "+country)
for river in messages.keys():
print(river)
for country in messages.values():
print(country)
#6-6
favourite_language={'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python',
}
names=['jen','sarah','liming']
for name in names:
if name in favourite_language.keys():
print("Thank you,"+name.title())
else:
print(name.title()+",would you want to participate in an invistigation?")
輸出如下:
The Nile runs through Egypt
The Changjiang runs through China
The Amazon runs through Brazil
Nile
Changjiang
Amazon
Egypt
China
Brazil
Thank you,Jen
Thank you,Sarah
Liming,would you want to participate in an invistigation?
四、嵌套
將字典存儲在列表中,或將列表作爲值存儲在字典中稱爲嵌套。
- 字典列表
- 列表字典
- 字典中存儲字典
動手試一試6-7 - 6-11
#6-7
person_1={'first_name':'xiaoming','last_name':'wang','age':20,'city':'Beijing'}
person_2={'first_name':'xiaohua','last_name':'zhang','age':21,'city':'Shanghai'}
person_3={'first_name':'xiaolong','last_name':'li','age':24,'city':'Shenzhen'}
people=[person_1,person_2,person_3]
for person in people:
print(person)
#6-8
milk={'type':'cat','master':'John'}
cake={'type':'dog','master':'Jenny'}
cookie={'type':'fish','master':'Tom'}
pets=[milk,cake,cookie]
for pet in pets:
print(pet)
#6-9
favourit_places={'John':['Beijing''Shanghai'],'Jenny':['Shenzhen','Guangzhou'],'Tom':['Tianjin','Pairs']}
for name,places in favourit_places.items():
print('\n'+name+"'s favourite places are:")
for place in places:
print(place)
#6-10
#同6-9 略
#6-11
print("\n")
cities={
'Beijing':{
'country':'China',
'population':'21540000',
'fact':'capital city',
},
'Pairs':{
'country':'Franch',
'population':'2240000',
'fact':'capital city',
},
'New York':{
'country':'America',
'population':'8510000',
'fact':'Big Apple'
},
}
for city,message in cities.items():
print('\n'+city+':')
for message_1,message_2 in message.items():
print('\t'+message_1+":"+message_2)
輸出如下:
{'first_name': 'xiaoming', 'last_name': 'wang', 'age': 20, 'city': 'Beijing'}
{'first_name': 'xiaohua', 'last_name': 'zhang', 'age': 21, 'city': 'Shanghai'}
{'first_name': 'xiaolong', 'last_name': 'li', 'age': 24, 'city': 'Shenzhen'}
{'type': 'cat', 'master': 'John'}
{'type': 'dog', 'master': 'Jenny'}
{'type': 'fish', 'master': 'Tom'}
John's favourite places are:
BeijingShanghai
Jenny's favourite places are:
Shenzhen
Guangzhou
Tom's favourite places are:
Tianjin
Pairs
Beijing:
country:China
population:21540000
fact:capital city
Pairs:
country:Franch
population:2240000
fact:capital city
New York:
country:America
population:8510000
fact:Big Apple