您可以在此处找到一系列迭代方法:http
:
//docs.python.org/2.7/library/itertools.html#recipes
from itertools import islice, cycledef roundrobin(*iterables): "roundrobin('ABC', 'D', 'EF') --> A D E B F C" # Recipe credited to George Sakkis pending = len(iterables) nexts = cycle(iter(it).next for it in iterables) while pending: try: for next in nexts: yield next() except StopIteration: pending -= 1 nexts = cycle(islice(nexts, pending))print list(roundrobin(range(5), "hello"))编辑 :Python 3
https://docs.python.org/3/library/itertools.html#itertools-
recipes
def roundrobin(*iterables): "roundrobin('ABC', 'D', 'EF') --> A D E B F C" # Recipe credited to George Sakkis num_active = len(iterables) nexts = cycle(iter(it).__next__ for it in iterables) while num_active: try: for next in nexts: yield next() except StopIteration: num_active -= 1 nexts = cycle(islice(nexts, num_active))print list(roundrobin(range(5), "hello"))


