在现有的 Python 中没有明显的方法可以做到这一点,但是制作自己的支持回溯的迭代器很容易。例如,后面是一个“可搜索”的迭代器。寻求相对 0 重复相同的元素,或相对 -1 回到前一个元素。
注意,因为这种迭代方式不享受“保证前进的进步”,它可以说更容易出现无限循环。
class SeekableIterator(object):
"""An iterator that supports seeking backwards or forwards."""
def __init__(self, iterable):
"""Make a SeekableIterator over an iterable collection."""
self.iterable = iterable
self.index = None
def __iter__(self):
"""Start the iteration."""
self.index = 0
return self
def next(self):
"""Return the next item in the iterator."""
try:
value = self.iterable[self.index]
self.index += 1
return value
except IndexError:
raise StopIteration
def seek(self, n, relative=False):
"""Adjust the loop counter, either relatively or to an absolute index.
Note that seeking 0 replays the current item. Seeking -1 goes to
the previous item. If the adjustment pushes the index outside the
iterable's bounds, raise an index error."""
if relative:
self.index += n - 1
# NB index already advanced one in next(), so subtracting one here
else:
self.index = n
if self.index < 0 or self.index >= len(self.iterable):
raise IndexError
if __name__ == '__main__':
import random
def prob(percent):
"""Return True with roughly the given probability, else False"""
return random.random() <= (percent * 1.0 / 100.0)
seeker = SeekableIterator([1, 2, 3, 4])
for n in seeker:
print "n:", n
if prob(50):
if prob(50):
print "\tREDO - seeking 0"
seeker.seek(0, relative=True)
elif n > 1:
print "\tUNDO - seeking -1"
seeker.seek(-1, relative=True)
这是一个示例输出:
n: 1
n: 2
n: 3
n: 4
REDO - seeking 0
n: 4
REDO - seeking 0
n: 4
UNDO - seeking -1
n: 3
n: 4