# 如何进行反向迭代以及如何实现反向迭代 l = [1, 2, 3, 4, 5] # 这种方式改变了原列表 # l.reverse() # print(l) lNew = l[::-1] print(lNew) print(l) for x in reversed(l): print(x) class FloatRange: def __init__(self, start, end, step=0.1): self.start = start self.end = end self.step = step def __iter__(self): t = self.start while t <= self.end: yield t t += self.step def __reversed__(self): t = self.end while t >= self.start: yield t t -= self.step # 正向迭代 for x in FloatRange(1.0, 4.0, 0.5): print(x) # 反向迭代 for x in reversed(FloatRange(1.0, 4.0, 0.5)): print(x)