问题描述
在itertools
中有一个chain
,它将多个生成器组合到一个生成器中,并且实质上对它们进行了深度优先迭代,即chain.from_iterable(['ABC', '123'])
生成A,B,C,1、2、3但是,没有广度优先版本,还是我错过了什么?当然有izip_longest
,但是对于大量的生成器来说,这感觉很尴尬,因为元组会很长,甚至很稀疏.
In itertools
there's chain
, which combines multiple generators in a single one, and in essence does a depth-first iteration over them, i.e., chain.from_iterable(['ABC', '123'])
yields A, B, C, 1, 2, 3. But, there's no breadth-first version, or am I missing something? There's of course izip_longest
, but for large numbers of generators this feels awkward, as the tuples will be very long and possibly very sparse.
我想出了以下几点:
def chain_bfs(*generators):
generators = list(generators)
while generators:
g = generators.pop(0)
try:
yield g.next()
except StopIteration:
pass
else:
generators.append(g)
对我来说,这有点冗长,我还想念一种更Python化的方法吗?并且此函数是否可以很好地包含在itertools
中?
It feels a bit verbose to me, is there a more Pythonic approach I'm missing? And would this function be a good candidate for inclusion in itertools
?
推荐答案
您可以使用 遍历迭代器;旋转双端队列效率更高.我也将其称为链式拉链,而不是呼吸优先链",例如:
You could use collections.deque()
to rotate through your iterators; rotating a deque is much more efficient. I'd also call it a chained zip, not a 'breath first chain', as such:
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)
演示:
>>> list(chained_zip('ABC', '123'))
['A', '1', 'B', '2', 'C', '3']
>>> list(chained_zip('AB', '1234'))
['A', '1', 'B', '2', '3', '4']
文档中还有一个 roundrobin()
配方 ,使用 itertools.cycle()
函数:
There is also a roundrobin()
recipe in the documentation that does the same, using the itertools.cycle()
function:
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))
这篇关于itertools.chain()的广度优先版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!