¿Cuál es la forma más eficiente de alternar tomando valores de diferentes iteradores en Python, para que, por ejemplo, alternate(xrange(1, 7, 2), xrange(2, 8, 2))
arroje 1, 2, 3, 4, 5, 6. Conozco una forma de implementarlo sería:Alternando entre iteradores en Python
def alternate(*iters):
while True:
for i in iters:
try:
yield i.next()
except StopIteration:
pass
Pero, ¿hay una manera más eficiente o más limpia? (O, mejor aún, una función itertools
me perdí?)
Qué quiere decir 1, 2, 7, 8, 2, 2? –
Pregunta muy similar: http://stackoverflow.com/questions/243865/how-do-i-merge-two-python-iterators – Seth
@Sean Devlin: No, 1 2 3 4 5 6 es correcto. http://docs.python.org/library/functions.html#xrange –