Skip to content

Commit e2e03b9

Browse files
author
Archie
committed
add 08_dictionaries Chinese support
1 parent 11023b3 commit e2e03b9

File tree

1 file changed

+343
-0
lines changed

1 file changed

+343
-0
lines changed

Chinese/08_dictionaries.md

Lines changed: 343 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,343 @@
1+
<div align="center">
2+
<h1> 30 天 Python 学习:第 8 天 - 字典</h1>
3+
<a class="header-badge" target="_blank" href="https://www.linkedin.com/in/asabeneh/">
4+
<img src="https://img.shields.io/badge/style--5eba00.svg?label=LinkedIn&logo=linkedin&style=social">
5+
</a>
6+
<a class="header-badge" target="_blank" href="https://twitter.com/Asabeneh">
7+
<img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/asabeneh?style=social">
8+
</a>
9+
10+
<sub>作者:
11+
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
12+
<small> 第二版:2021 年 7 月</small>
13+
</sub>
14+
15+
</div>
16+
17+
[<< 第 7 天 ](../07_Day_Sets/07_sets.md) | [第 9 天 >>](../09_Day_Conditionals/09_conditionals.md)
18+
19+
![30 天 Python 学习](../images/[email protected])
20+
21+
- [📘 第 8 天](#-第-8-天)
22+
- [字典](#字典)
23+
- [创建字典](#创建字典)
24+
- [字典长度](#字典长度)
25+
- [访问字典项](#访问字典项)
26+
- [向字典添加项](#向字典添加项)
27+
- [修改字典中的项](#修改字典中的项)
28+
- [检查字典中的键](#检查字典中的键)
29+
- [从字典中移除键值对](#从字典中移除键值对)
30+
- [将字典转换为项目列表](#将字典转换为项目列表)
31+
- [清空字典](#清空字典)
32+
- [删除字典](#删除字典)
33+
- [复制字典](#复制字典)
34+
- [将字典键转换为列表](#将字典键转换为列表)
35+
- [将字典值转换为列表](#将字典值转换为列表)
36+
- [💻 练习:第 8 天](#-练习-第-8-天)
37+
38+
# 📘 第 8 天
39+
40+
## 字典
41+
42+
字典是一种由无序、可修改(可变)的键值对组成的数据类型。
43+
44+
### 创建字典
45+
46+
为了创建字典,我们使用大括号 {} 或内置函数 _dict()_
47+
48+
```py
49+
# 语法
50+
empty_dict = {}
51+
# 带数据值的字典
52+
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
53+
```
54+
55+
**示例:**
56+
57+
```py
58+
person = {
59+
'first_name':'Asabeneh',
60+
'last_name':'Yetayeh',
61+
'age':250,
62+
'country':'Finland',
63+
'is_marred':True,
64+
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
65+
'address':{
66+
'street':'Space street',
67+
'zipcode':'02210'
68+
}
69+
}
70+
```
71+
72+
上面的字典显示,值可以是任何数据类型:字符串、布尔值、列表、元组、集合或字典。
73+
74+
### 字典长度
75+
76+
它检查字典中的键值对的数量。
77+
78+
```py
79+
# 语法
80+
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
81+
print(len(dct)) # 4
82+
```
83+
84+
**示例:**
85+
86+
```py
87+
person = {
88+
'first_name':'Asabeneh',
89+
'last_name':'Yetayeh',
90+
'age':250,
91+
'country':'Finland',
92+
'is_marred':True,
93+
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
94+
'address':{
95+
'street':'Space street',
96+
'zipcode':'02210'
97+
}
98+
}
99+
print(len(person)) # 7
100+
101+
```
102+
103+
### 访问字典项
104+
105+
我们可以通过参考其键名来访问字典项。
106+
107+
```py
108+
# 语法
109+
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
110+
print(dct['key1']) # value1
111+
print(dct['key4']) # value4
112+
```
113+
114+
**示例:**
115+
116+
```py
117+
person = {
118+
'first_name':'Asabeneh',
119+
'last_name':'Yetayeh',
120+
'age':250,
121+
'country':'Finland',
122+
'is_marred':True,
123+
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
124+
'address':{
125+
'street':'Space street',
126+
'zipcode':'02210'
127+
}
128+
}
129+
print(person['first_name']) # Asabeneh
130+
print(person['country']) # Finland
131+
print(person['skills']) # ['JavaScript', 'React', 'Node', 'MongoDB', 'Python']
132+
print(person['skills'][0]) # JavaScript
133+
print(person['address']['street']) # Space street
134+
print(person['city']) # 错误
135+
```
136+
137+
通过键名访问项时,如果键不存在会引发错误。为了避免这个错误,我们首先要检查键是否存在,或者使用 _get_ 方法。get 方法在键不存在时返回 None(这是 NoneType 对象数据类型)。
138+
139+
```py
140+
person = {
141+
'first_name':'Asabeneh',
142+
'last_name':'Yetayeh',
143+
'age':250,
144+
'country':'Finland',
145+
'is_marred':True,
146+
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
147+
'address':{
148+
'street':'Space street',
149+
'zipcode':'02210'
150+
}
151+
}
152+
print(person.get('first_name')) # Asabeneh
153+
print(person.get('country')) # Finland
154+
print(person.get('skills')) #['HTML','CSS','JavaScript', 'React', 'Node', 'MongoDB', 'Python']
155+
print(person.get('city')) # None
156+
```
157+
158+
### 向字典添加项
159+
160+
我们可以向字典中添加新的键值对
161+
162+
```py
163+
# 语法
164+
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
165+
dct['key5'] = 'value5'
166+
```
167+
168+
**示例:**
169+
170+
```py
171+
person = {
172+
'first_name':'Asabeneh',
173+
'last_name':'Yetayeh',
174+
'age':250,
175+
'country':'Finland',
176+
'is_marred':True,
177+
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
178+
'address':{
179+
'street':'Space street',
180+
'zipcode':'02210'
181+
}
182+
}
183+
person['job_title'] = 'Instructor'
184+
person['skills'].append('HTML')
185+
print(person)
186+
```
187+
188+
### 修改字典中的项目
189+
190+
我们可以修改字典中的项目
191+
192+
```py
193+
# 语法
194+
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
195+
dct['key1'] = 'value-one'
196+
```
197+
198+
**示例:**
199+
200+
```py
201+
person = {
202+
'first_name':'Asabeneh',
203+
'last_name':'Yetayeh',
204+
'age':250,
205+
'country':'Finland',
206+
'is_married':True,
207+
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
208+
'address':{
209+
'street':'Space street',
210+
'zipcode':'02210'
211+
}
212+
}
213+
person['first_name'] = 'Eyob'
214+
person['age'] = 252
215+
```
216+
217+
### 检查字典中的键
218+
219+
我们使用 _in_ 运算符来检查字典中是否存在某个键
220+
221+
```py
222+
# 语法
223+
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
224+
print('key2' in dct) # True
225+
print('key5' in dct) # False
226+
```
227+
228+
### 从字典中删除键值对
229+
230+
- _pop(key)_: 删除具有指定键名的项目
231+
- _popitem()_: 删除最后一个项目
232+
- _del_: 删除具有指定键名的项目
233+
234+
```py
235+
# 语法
236+
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
237+
dct.pop('key1') # 删除 key1 项目
238+
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
239+
dct.popitem() # 删除最后一项
240+
del dct['key2'] # 删除 key2 项目
241+
```
242+
243+
**示例:**
244+
245+
```py
246+
person = {
247+
'first_name':'Asabeneh',
248+
'last_name':'Yetayeh',
249+
'age':250,
250+
'country':'Finland',
251+
'is_married':True,
252+
'skills':['JavaScript', 'React', 'Node', 'MongoDB', 'Python'],
253+
'address':{
254+
'street':'Space street',
255+
'zipcode':'02210'
256+
}
257+
}
258+
person.pop('first_name') # 删除 firstname 项目
259+
person.popitem() # 删除 address 项目
260+
del person['is_married'] # 删除 is_married 项目
261+
```
262+
263+
### 将字典改变为项目列表
264+
265+
_items()_ 方法将字典变成由元组组成的列表。
266+
267+
```py
268+
# 语法
269+
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
270+
print(dct.items()) # dict_items([('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3'), ('key4', 'value4')])
271+
```
272+
273+
### 清空字典
274+
275+
如果我们不需要字典中的项目,我们可以使用 _clear()_ 方法来清空它们
276+
277+
```py
278+
# 语法
279+
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
280+
print(dct.clear()) # None
281+
```
282+
283+
### 删除字典
284+
285+
如果我们不再使用字典,我们可以完全删除它
286+
287+
```py
288+
# 语法
289+
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
290+
del dct
291+
```
292+
293+
### 复制字典
294+
295+
我们可以使用 _copy()_ 方法复制一个字典。使用 copy 方法可以避免原始字典被修改。
296+
297+
```py
298+
# 语法
299+
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
300+
dct_copy = dct.copy() # {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
301+
```
302+
303+
### 获取字典的键列表
304+
305+
keys() 方法给我们一个包含所有字典键的列表。
306+
307+
```py
308+
# 语法
309+
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
310+
keys = dct.keys()
311+
print(keys) # dict_keys(['key1', 'key2', 'key3', 'key4'])
312+
```
313+
314+
### 获取字典的值列表
315+
316+
values 方法给我们一个包含所有字典值的列表。
317+
318+
```py
319+
# 语法
320+
dct = {'key1':'value1', 'key2':'value2', 'key3':'value3', 'key4':'value4'}
321+
values = dct.values()
322+
print(values) # dict_values(['value1', 'value2', 'value3', 'value4'])
323+
```
324+
325+
🌕 你很了不起。现在,你已经掌握了字典的强大功能。你已经完成了第 8 天的挑战,离成功又近了一步。现在为你的大脑和肌肉做一些练习。
326+
327+
## 💻 练习:第 8 天
328+
329+
1. 创建一个名为 dog 的空字典
330+
2. 向 dog 字典添加 name、color、breed、legs、age 键
331+
3. 创建一个学生字典,添加 first_name、last_name、gender、age、marital status、skills、country、city 和 address 作为字典的键
332+
4. 获取学生字典的长度
333+
5. 获取 skills 的值并检查数据类型,应该是列表
334+
6. 修改 skills 值,添加一到两个技能
335+
7. 获取字典的键列表
336+
8. 获取字典的值列表
337+
9. 使用 _items()_ 方法将字典变为由元组组成的列表
338+
10. 删除字典中的一项
339+
11. 删除其中一个字典
340+
341+
🎉 恭喜你! 🎉
342+
343+
[<< 第 7 天 ](../07_Day_Sets/07_sets.md) | [第 9 天 >>](../09_Day_Conditionals/09_conditionals.md)

0 commit comments

Comments
 (0)