Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f0a6e2e

Browse filesBrowse files
committed
🎉 学习第3章字典和集合
1 parent 5407a2d commit f0a6e2e
Copy full SHA for f0a6e2e

File tree

Expand file treeCollapse file tree

6 files changed

+860
-0
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+860
-0
lines changed

‎codes/ch03/creator.py

Copy file name to clipboard
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
"""
4+
@author: HuRuiFeng
5+
@file: creator.py
6+
@time: 2023/9/16 14:31
7+
@project: fluent-python
8+
@desc: P62 从出版物记录中提取创作者的名字
9+
"""
10+
11+
12+
def get_creators(record: dict) -> list:
13+
match record:
14+
case {'type': 'book', 'api': 2, 'authors': [*names]}:
15+
return names
16+
case {'type': 'book', 'api': 1, 'author': name}:
17+
return [name]
18+
case {'type': 'book'}:
19+
return ValueError(f"Invalid 'book' record: {record!r}")
20+
case {'type': 'movie', 'director': name}:
21+
return [name]
22+
case _:
23+
return ValueError(f'Invalid record: {record!r}')
24+
25+
26+
if __name__ == '__main__':
27+
b1 = dict(api=1, author='Douglas Hofstadter', type='book', title='Godel, Escher, Bach')
28+
print(get_creators(b1))

‎codes/ch03/index.py

Copy file name to clipboard
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
"""
4+
@author: HuRuiFeng
5+
@file: index.py
6+
@time: 2023/9/16 15:31
7+
@project: fluent-python
8+
@desc: P68 与index0.py相比,使用dict.setdefault
9+
"""
10+
11+
import re
12+
13+
WORD_RE = re.compile(r'\w+')
14+
15+
index = {}
16+
with open('zen.txt', encoding='utf-8') as fp:
17+
for line_no, line in enumerate(fp, 1):
18+
for match in WORD_RE.finditer(line):
19+
word = match.group()
20+
column_no = match.start() + 1
21+
location = (line_no, column_no)
22+
index.setdefault(word, []).append(location)
23+
24+
for word in sorted(index, key=str.upper):
25+
print(word, index[word])

‎codes/ch03/index0.py

Copy file name to clipboard
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
"""
4+
@author: HuRuiFeng
5+
@file: index0.py
6+
@time: 2023/9/16 15:28
7+
@project: fluent-python
8+
@desc: P67 使用dict.get获取并更新词出现的位置列表,编制索引
9+
"""
10+
import re
11+
12+
WORD_RE = re.compile(r'\w+')
13+
14+
index = {}
15+
with open('zen.txt', encoding='utf-8') as fp:
16+
for line_no, line in enumerate(fp, 1):
17+
for match in WORD_RE.finditer(line):
18+
word = match.group()
19+
column_no = match.start() + 1
20+
location = (line_no, column_no)
21+
# 这样写不完美
22+
occurrences = index.get(word, [])
23+
occurrences.append(location)
24+
index[word] = occurrences
25+
26+
for word in sorted(index, key=str.upper):
27+
print(word, index[word])

‎codes/ch03/index_default.py

Copy file name to clipboard
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
"""
4+
@author: HuRuiFeng
5+
@file: index_default.py
6+
@time: 2023/9/16 15:34
7+
@project: fluent-python
8+
@desc: 使用defaultdict代替setdefault方法
9+
"""
10+
import collections
11+
import re
12+
13+
WORD_RE = re.compile(r'\w+')
14+
15+
index = collections.defaultdict(list)
16+
with open('zen.txt', encoding='utf-8') as fp:
17+
for line_no, line in enumerate(fp, 1):
18+
for match in WORD_RE.finditer(line):
19+
word = match.group()
20+
column_no = match.start() + 1
21+
location = (line_no, column_no)
22+
index[word].append(location)
23+
24+
for word in sorted(index, key=str.upper):
25+
print(word, index[word])

‎codes/ch03/zen.txt

Copy file name to clipboard
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The Zen of Python, by Tim Peters
2+
3+
Beautiful is better than ugly.
4+
Explicit is better than implicit.
5+
Simple is better than complex.
6+
Complex is better than complicated.
7+
Flat is better than nested.
8+
Sparse is better than dense.
9+
Readability counts.
10+
Special cases aren't special enough to break the rules.
11+
Although practicality beats purity.
12+
Errors should never pass silently.
13+
Unless explicitly silenced.
14+
In the face of ambiguity, refuse the temptation to guess.
15+
There should be one-- and preferably only one --obvious way to do it.
16+
Although that way may not be obvious at first unless you're Dutch.
17+
Now is better than never.
18+
Although never is often better than *right* now.
19+
If the implementation is hard to explain, it's a bad idea.
20+
If the implementation is easy to explain, it may be a good idea.
21+
Namespaces are one honking great idea -- let's do more of those!

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.