Python学习之路(6)字典

茴香豆 Lv5

本章中,你将学习如何访问和修改字典的信息。并且演示如何遍历字典中的数据。另外,还将学习存储字典的列表、存储列表的字典和存储字典的字典。

1.一个简单的字典

在Python中,字典是一系列键值对。每个键都与一个值相关联,你可以使用键来访问与之相关联的值。事实上,可将任何Python对象用作字典中中的值。

字典用放在花括号 { } 中的一系列键值表示。

我们来设置一些外星人,设置一个简单的字典,存储特定外星人的信息:

1
2
3
alien_0 = {'color': 'green', 'points': 5}
print(alien_0['color']) # green
print(alien_0['points']) # 5

2.使用字典

2.1添加键值对:

1
2
3
4
5
6
# add key-calue 
alien_0 = {'color': 'green', 'points': 5}
print(alien_0) # {'color': 'green', 'points': 5}
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0) # {'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}

2.2修改字典中的值

1
2
3
4
5
# change value
alien_0 = {'color': 'green', 'points': 5}
print(alien_0) # {'color': 'green', 'points': 5}
alien_0['color'] = 'yellow'
print(alien_0) # {'color': 'yellow', 'points': 5}

2.3删除键值对:

1
2
3
4
5
# delete key-value
alien_0 = {'color': 'green', 'points': 5}
print(alien_0) # {'color': 'green', 'points': 5}
del alien_0['points']
print(alien_0) # {'color': 'green'}

3.遍历字典

3.1遍历所有键值对

1
2
3
4
5
6
7
8
9
10
11
12
# for
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi'
}
for key, value in user_0.items():
print("key: " + key + " | value: " + value)
#output:
key: username | value: efermi
key: first | value: enrico
key: last | value: fermi

3.2遍历所有键

1
2
3
4
5
6
7
8
9
10
11
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi'
}
for key in user_0.keys():
print(key.title())
#output:
Username
First
Last

3.3按顺序遍历所有键

1
2
3
4
5
6
7
8
9
10
11
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi'
}
for key in sorted(user_0.keys()):
print(key.title())
#output:
First
Last
Username

3.4遍历所有值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi'
}
for value in user_0.values():
print(value.title())
#output:
Efermi
Enrico
Fermi
#use set drop same value 删除重复元素
user_0['middle'] = 'fermi'
for value in set(user_0.values()):
print(value.title())
#output:
Efermi
Enrico
Fermi

4.嵌套

4.1字典列表

1
2
3
4
5
6
7
8
9
# dictionary list
alien_0 = {'color': 'green', 'point': 5}
alien_1 = {'color': 'yellow', 'point': 10}
alien_2 = {'color': 'red', 'point': 15}

aliens = [alien_0, alien_1, alien_2]
print(aliens)
#output:
[{'color': 'green', 'point': 5}, {'color': 'yellow', 'point': 10}, {'color': 'red', 'point': 15}]

4.2在字典中存储列表

1
2
3
4
5
6
7
8
# list in dictionary
pizza = {
'crust': 'thick',
'toppings': ['mushrooms', 'extra cheese']
}
print(pizza)
#output:
{'crust': 'thick', 'toppings': ['mushrooms', 'extra cheese']}

4.3在字典中存储字典

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# dictionary in dictionary
users = {
'aeinstein': {
'first': 'albert',
'last': 'einstein',
'location': 'princeton'
},
'mcurie': {
'first': 'marie',
'last': 'curie',
'location': 'paris'
}
}
for username, user_info in users.items():
print("username: " + username)
print("\tfullname: " + user_info['first'] + " " + user_info['last'])
print("\tlocation: " + user_info['location'])
#output:
username: aeinstein
fullname: albert einstein
location: princeton
username: mcurie
fullname: marie curie
location: paris
  • Title: Python学习之路(6)字典
  • Author: 茴香豆
  • Created at : 2022-07-01 15:03:05
  • Updated at : 2022-07-01 21:56:57
  • Link: https://hxiangdou.github.io/2022/07/01/Python_6/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments