广度优先版本的itertools.chain()

Mic*_*hel 1 python python-itertools

itertools那里chain,它将多个生成器组合在一起,实质上是对它们进行深度优先迭代,即chain.from_iterable(['ABC', '123'])产生A,B,C,1,2,3.但是,没有广度优先版本,或者是我错过了什么?当然izip_longest,但是对于大量的发生器来说,这感觉很尴尬,因为元组会非常长并且可能非常稀疏.

我想出了以下内容:

def chain_bfs(*generators):
    generators = list(generators)
    while generators:
        g = generators.pop(0)
        try:
            yield g.next()
        except StopIteration:
            pass
        else:
            generators.append(g)
Run Code Online (Sandbox Code Playgroud)

对我来说感觉有点冗长,是否有更多的Pythonic方法我错过了?这个功能是否适合加入itertools

Mar*_*ers 7

您可以使用collections.deque()旋转迭代器; 旋转双端队列效率更高.我也把它称为链式拉链,而不是'呼吸第一链',因此:

from collections import deque

def chained_zip(*iterables):
    iterables = deque(map(iter, iterables))
    while iterables:
        try:
            yield next(iterables[0])
        except StopIteration:
            iterables.popleft()
        else:
            iterables.rotate(-1)
Run Code Online (Sandbox Code Playgroud)

演示:

>>> list(chained_zip('ABC', '123'))
['A', '1', 'B', '2', 'C', '3']
>>> list(chained_zip('AB', '1234'))
['A', '1', 'B', '2', '3', '4']
Run Code Online (Sandbox Code Playgroud)

使用以下功能,roundrobin()文档中还有一个相同的配方:itertools.cycle()

def 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))
Run Code Online (Sandbox Code Playgroud)