Skip to content

Commit c6fe046

Browse files
committed
更新了第7天的文档
1 parent 2dc9890 commit c6fe046

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

Day01-15/07.字符串和常用数据结构.md

+15-5
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,8 @@ set2 = set(range(1, 10))
331331
set3 = set((1, 2, 3, 3, 2, 1))
332332
print(set2, set3)
333333
# 创建集合的推导式语法(推导式也可以用于推导集合)
334-
set4 = {num for num in range(100) if num % 3 == 0 or num % 5 == 0}
334+
set4 = {num for num in range(1, 100) if num % 3 == 0 or num % 5 == 0}
335+
print(set4)
335336
```
336337

337338
向集合添加元素和从集合删除元素。
@@ -375,16 +376,25 @@ print(set1 >= set3)
375376
376377
### 使用字典
377378

378-
字典是另一种可变容器模型,类似于我们生活中使用的字典,它可以存储任意类型对象,与列表、集合不同的是,字典的每个元素都是由一个键和一个值组成的“键值对”,键和值通过冒号分开。下面的代码演示了如何定义和使用字典。
379+
字典是另一种可变容器模型,Python中的字典跟我们生活中使用的字典是一样一样的,它可以存储任意类型对象,与列表、集合不同的是,字典的每个元素都是由一个键和一个值组成的“键值对”,键和值通过冒号分开。下面的代码演示了如何定义和使用字典。
379380

380381
```Python
382+
# 创建字典的字面量语法
381383
scores = {'骆昊': 95, '白元芳': 78, '狄仁杰': 82}
384+
print(scores)
385+
# 创建字典的构造器语法
386+
items1 = dict(one=1, two=2, three=3, four=4)
387+
# 通过zip函数将两个序列压成字典
388+
items2 = dict(zip(['a', 'b', 'c'], '123'))
389+
# 创建字典的推导式语法
390+
items3 = {num: num ** 2 for num in range(1, 10)}
391+
print(items1, items2, items3)
382392
# 通过键可以获取字典中对应的值
383393
print(scores['骆昊'])
384394
print(scores['狄仁杰'])
385-
# 对字典进行遍历(遍历的其实是键再通过键取对应的值)
386-
for elem in scores:
387-
print('%s\t--->\t%d' % (elem, scores[elem]))
395+
# 对字典中所有键值对进行遍历
396+
for key in scores:
397+
print(f'{key}: {scores[key]}')
388398
# 更新字典中的元素
389399
scores['白元芳'] = 65
390400
scores['诸葛王朗'] = 71

0 commit comments

Comments
 (0)