4.5小节完成
This commit is contained in:
48
cookbook/c04/p05_reverse_iterate.py
Normal file
48
cookbook/c04/p05_reverse_iterate.py
Normal file
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- encoding: utf-8 -*-
|
||||
"""
|
||||
Topic: 方向迭代
|
||||
Desc :
|
||||
"""
|
||||
|
||||
|
||||
def reverse_iterate():
|
||||
a = [1, 2, 3, 4]
|
||||
for x in reversed(a):
|
||||
print(x)
|
||||
|
||||
# 两个条件:大小可知或者实现__reversed__方法,
|
||||
# 如果都不满足,先转换为list
|
||||
# Print a file backwards
|
||||
# f = open('somefile')
|
||||
# for line in reversed(list(f)):
|
||||
# print(line, end='')
|
||||
|
||||
for rr in reversed(Countdown(30)):
|
||||
print(rr)
|
||||
for rr in Countdown(30):
|
||||
print(rr)
|
||||
|
||||
|
||||
class Countdown:
|
||||
def __init__(self, start):
|
||||
self.start = start
|
||||
|
||||
# Forward iterator
|
||||
def __iter__(self):
|
||||
n = self.start
|
||||
while n > 0:
|
||||
yield n
|
||||
n -= 1
|
||||
|
||||
# Reverse iterator
|
||||
def __reversed__(self):
|
||||
n = 1
|
||||
while n <= self.start:
|
||||
yield n
|
||||
n += 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
reverse_iterate()
|
||||
|
||||
@@ -5,14 +5,69 @@
|
||||
----------
|
||||
问题
|
||||
----------
|
||||
todo...
|
||||
你想反方向迭代一个序列
|
||||
|
||||
|
|
||||
|
||||
----------
|
||||
解决方案
|
||||
----------
|
||||
todo...
|
||||
使用内置的reversed()函数,比如:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> a = [1, 2, 3, 4]
|
||||
>>> for x in reversed(a):
|
||||
... print(x)
|
||||
...
|
||||
4
|
||||
3
|
||||
2
|
||||
1
|
||||
|
||||
反向迭代仅仅当对象的大小可预先确定或者对象实现了__reversed__()的特殊方法时才能生效。
|
||||
如果两者都不符合,那你必须先将对象转换为一个列表才行,比如:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# Print a file backwards
|
||||
f = open('somefile')
|
||||
for line in reversed(list(f)):
|
||||
print(line, end='')
|
||||
|
||||
要注意的是如果可迭代对象元素很多的话,将其预先转换为一个列表要消耗大量的内存。
|
||||
|
||||
|
|
||||
|
||||
----------
|
||||
讨论
|
||||
----------
|
||||
todo...
|
||||
很多程序员并不知道可以通过在自定义类上实现__reversed__()方法来实现反向迭代。比如:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
class Countdown:
|
||||
def __init__(self, start):
|
||||
self.start = start
|
||||
|
||||
# Forward iterator
|
||||
def __iter__(self):
|
||||
n = self.start
|
||||
while n > 0:
|
||||
yield n
|
||||
n -= 1
|
||||
|
||||
# Reverse iterator
|
||||
def __reversed__(self):
|
||||
n = 1
|
||||
while n <= self.start:
|
||||
yield n
|
||||
n += 1
|
||||
|
||||
for rr in reversed(Countdown(30)):
|
||||
print(rr)
|
||||
for rr in Countdown(30):
|
||||
print(rr)
|
||||
|
||||
定义一个反向迭代器可以使得代码非常的高效,
|
||||
因为它不再需要将数据填充到一个列表中然后再去反向迭代这个列表。
|
||||
|
||||
Reference in New Issue
Block a user