[TOC]
*****
# 6.1 一個簡單的字典
像json對象,鍵值對
```
alien_0 = {'color': 'green', 'points': 5}
print(alien_0['color'])
```
# 6.2 使用字典
字典是一系列鍵—值對。每個鍵都與一個值相關聯,你可以使用鍵來訪問與之
相關聯的值。與鍵相關聯的值可以是數字、字符串、列表乃至字典。事實上,可將任何Python對象用作字典中的值。
```
alien_0 = {'color': 'green', 'points': 5}
```
## 6.2.1 訪問字典中的值
```
alien_0 = {'color': 'green'}
print(alien_0['color'])
# green
```
## 6.2.2 添加鍵—值對
字典是一種動態結構,可隨時在其中添加鍵—值對。要添加鍵—值對,可依次指定字典名、用方括號括起的鍵和相關聯的值。
```
alien_0 = {'color': 'green', 'points': 5}
print(alien_0)
alien_0['x_position'] = 0
alien_0['y_position'] = 25
#{'color': 'green', 'points': 5}
#{'color': 'green', 'points': 5, 'y\_position': 25, 'x\_position': 0}
```
## 6.2.3 先創建一個空字典
```
alien_0 = {}
alien_0['color'] = 'green'
alien_0['points'] = 5
```
## 6.2.4 修改字典中的值
```
alien_0 = {'color': 'green'}
print("The alien is " + alien_0['color'] + ".")
alien_0\['color'] = 'yellow'
```
## 6.2.5 刪除鍵— 值對
```
alien_0 = {'color': 'green', 'points': 5}
print(alien_0)
del alien_0['points']
```
## 6.2.6 由類似對象組成的字典
要調查很多人,詢問他們最喜歡的編程語言,可使用一個字典來存儲這種簡單調查的結果,如下所示:
```
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
```
# 6.3 遍歷字典
## 6.3.1 遍歷所有的鍵— 值對
```
user_0 = {'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}
for key, value in user_0.items():
print("\nKey: " + key)
print("Value: " + value)
```
## 6.3.2 遍歷字典中的所有鍵
```
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
for name in favorite_languages.keys():
```
## 6.3.3 按順序遍歷字典中的所有鍵
```
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
for name in sorted(favorite_languages.keys()):
print(name.title() + ", thank you for taking the poll.")
```
## 6.3.4 遍歷字典中的所有值
```
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
for language in favorite_languages.values():
print(language.title())
```
**將字典中不重復的值都取出來,使用集合set**
```
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
for language in set(favorite_languages.values()):
print(language.title())
```
# 6.4 嵌套
將一系列字典存儲在列表中,或將列表作為值存儲在字典中,這稱為嵌套。你
可以在列表中嵌套字典、在字典中嵌套列表甚至在字典中嵌套字典。
## 6.4.1 字典列表
將字典嵌套到列表中
創建一個外星人列表,其中每個外星人都是一個字典,包含有關該外星人的各種信息。
```
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)
```
創建30個外星人
```
aliens = []
# 創建30個綠色的外星人
# 循環30次
for alien_number in range(30):
new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
aliens.append(new_alien)
```
## 6.4.2 在字典中存儲列表
```
pizza = {
'crust': 'thick',
'toppings': ['mushrooms', 'extra cheese'],
}
for topping in pizza['toppings']:
print("\t" + topping)
```
## 6.4.3 在字典中存儲字典
可在字典中嵌套字典。例如,如果有多個網站用戶, 每個都有獨特的用戶名,可在字典中將用戶名作為鍵,然后將每位用戶的信息存儲在一個字典中,并將該字典作為與用戶名相關聯的值。在下面的程序中,對于每位用戶,我們都存儲了其三項信息:名、姓和居住地
```
#在字典中存儲字典
users = {'aeinstein': {'first': 'albert',
'last': 'einstein',
'location': 'princeton'},
'mcurie': {'first': 'marie',
'last': 'curie',
'location': 'paris'},
}
#對字典進行循環
for username, user_info in users.items():
print("\nUsername: " + username)
full_name = user_info['first'] + " " + user_info['last']
location = user_info['location']
print("\tFull name: " + full_name.title())
print("\tLocation: " + location.title())
```