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

Sign up
Appearance settings

Commit 7e1e2a0

Browse files
committed
add 04-generator-and-iterator README.md 💥
1 parent fb94366 commit 7e1e2a0

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

‎04-generator-and-iterator/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1. `demo.py`:支持迭代的类,正向迭代与反向迭代
2+
2. `genitor.py`:生成器的类,用于迭代
3+
3. `iterator_visit.py`:迭代器的访问:如`islice`访问生成器指定起始和终止点,跳过指定位置的元素,元素的排列组合,迭代添加索引,同时迭代多个序列,`zip`合并序列的访问,`chain`不同数据在一起迭代访问,将列表展平

‎04-generator-and-iterator/genitor.py‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,24 @@
1818
class linehistory:
1919
def __init__(self, lines, histlen=3):
2020
self.lines = lines
21-
self.history = deque(maxlen=histlen)
2221

2322
def __iter__(self):
2423
# 索引从 1 开始
2524
for lineno, line in enumerate(self.lines, 1):
26-
self.history.append((lineno, line))
27-
yield line
28-
29-
def clear(self):
30-
self.history.clear()
25+
yield lineno, line
3126

3227

3328
# 创建一个实例对象,可以访问内部属性值
3429
# 如果你在迭代操作时不使用for循环语句,那么你得先调用 iter() 函数
35-
with open('demo.py') as f:
30+
with open('demo.py', encoding='utf-8') as f:
3631
lines = linehistory(f)
37-
for line in lines:
38-
for lineno, hline in lines.history:
39-
print('{}:{}'.format(lineno, hline), end='')
32+
for lineno, line in lines:
33+
print(lineno, ': ', line, end='')
34+
# for lineno, hline in lines.history:
35+
# print('{}:{}'.format(lineno, hline), end='')
4036

4137

42-
f = open('demo.py')
38+
f = open('demo.py', encoding='utf-8')
4339
lines = linehistory(f)
4440
t = iter(lines)
4541
print(next(t))

‎04-generator-and-iterator/iterator_visit.py‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ def count(n):
3535

3636

3737
# 跳过可迭代对象的开始部分
38-
with open('/etc/passwd') as f:
39-
# 去掉文件头部的行
40-
for line in dropwhile(lambda line: not line.startswith('#'), f):
41-
# 如下的写法会去掉注释行
42-
# lines = (line for line in f if not line.startswith('#'))
43-
print(line, end='')
38+
# with open('/etc/passwd') as f:
39+
# # 去掉文件头部的行
40+
# for line in dropwhile(lambda line: not line.startswith('#'), f):
41+
# # 如下的写法会去掉注释行
42+
# # lines = (line for line in f if not line.startswith('#'))
43+
# print(line, end='')
4444

4545

4646
# 跳过指定位置的元素
47-
items = ['a', 'b', 'c', 1, 4, 10, 15]
47+
items = ['a', 'b', 'c', 1, 2, 4, 10, 15]
4848
# [3:] 和 [:3]
4949
for x in islice(items, 3, None):
5050
print(x)
@@ -57,16 +57,20 @@ def count(n):
5757

5858
# 全部的排列组合
5959
for p in permutations(items):
60-
print(p)
60+
print(p,)
61+
print('--------')
6162
# 指定数量的排列组合
6263
for p in permutations(items, 2):
6364
print(p)
65+
print('--------')
6466
# 去除元素相同的排列组合(不考虑顺序)
6567
for i in combinations(items, 2):
6668
print(i)
69+
print('--------')
6770
# 元素被选取就会从候选中剔除掉, 就不会再考虑它
6871
for c in combinations_with_replacement(items, 3):
6972
print(c)
73+
print('--------')
7074
# 负责的迭代可以参考 itertools
7175

7276

@@ -112,7 +116,8 @@ def flatten(items, ignore_types=(str, bytes)):
112116
items = [1, 2, [3, 4, [5, 6], 7], 8]
113117
# Produces 1 2 3 4 5 6 7 8
114118
for x in flatten(items):
115-
print(x)
119+
# sep=':' 不行,只有一个输出
120+
print(x, end=', ')
116121
items = ['Dave', 'Paula', ['Thomas', 'Lewis']]
117122
for x in flatten(items):
118123
print(x)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /