A
Arnaud Delobelle
Instead of counting the exceptions, we can limit the padding iterables
by using an iterator that returns len(iterables) - 1 padding
generators, use a sort of lazy chain, and then just izip.
from itertools import izip, repeat
def chain_next(xs, yg):
for x in xs: yield x
for y in yg.next(): yield y
def izippad(*xs, **kw):
padder = repeat(kw.get('padding', None))
padder_gen = repeat(padder, len(xs) - 1)
return izip(*[chain_next(x, padder_gen) for x in xs])
I have had the need for such a 'padded zip' before and my
implementation was eerily similar:
from itertools import repeat, chain, izip
def repeatnext(iterator):
val = iterator.next()
while True: yield val
def longzip(default, *iterables):
defaultgen = repeat(default, len(iterables) - 1)
return izip(*[chain(it, repeatnext(defaultgen)) for it in
iterables])