4.5 反向迭代
问题
解决方案
>>> a = [1, 2, 3, 4]
>>> for x in reversed(a):
... print(x)# Print a file backwards
f = open('somefile')
for line in reversed(list(f)):
print(line, end='')讨论
Last updated
>>> a = [1, 2, 3, 4]
>>> for x in reversed(a):
... print(x)# Print a file backwards
f = open('somefile')
for line in reversed(list(f)):
print(line, end='')Last updated
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)