itertools.izip brokeness

David Murmann david.murmann at rwth-aachen.de
Tue Jan 3 06:06:15 EST 2006


rurpy at yahoo.com schrieb:
> [izip() eats one line]

as far as i can see the current implementation cannot be changed
to do the Right Thing in your case. pythons iterators don't allow
to "look ahead", so izip can only get the next element. if this
fails for an iterator, everything up to that point is lost.

maybe the documentation for izip should note that the given
iterators are not necessarily in a sane state afterwards.

for your problem you can do something like:

def izipall(*args):
    iters = [iter(it) for it in args]
    while iters:
        result = []
        for it in iters:
            try:
                x = it.next()
            except StopIteration:
                iters.remove(it)
            else:
                result.append(x)
        yield tuple(result)

note that this does not yield tuples that are always the same
length, so "for x, y in izipall()" won't work. instead, do something
like "for seq in izipall(): print '\t'.join(seq)".

hope i was clear enough, David.



More information about the Python-list mailing list