inverse of izip
Steven Bethard
steven.bethard at gmail.com
Thu Aug 19 04:07:31 EDT 2004
Satchidanand Haridas <sharidas <at> zeomega.com> writes:
> How about using iter() to get another solution like the following:
>
> >>> starzip2 = lambda it: tuple([iter(x) for x in itertools.izip(*it)])
>
> >>> l,m = starzip2(itertools.izip(range(10),range(10)))
>
> >>> l
> <tupleiterator object at 0x4016802c>
> >>> m
> <tupleiterator object at 0x4016896c>
>
> >>> list(l)
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> >>> list(m)
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Unfortunately, I think this exhausts the iterators too early because it
applies * to the iterator:
>>> def range10():
... for i in range(10):
... yield i
... print "exhausted"
...
>>> l,m = starzip2(itertools.izip(range10(),range10()))
exhausted
I believe we only get one "exhausted" because as soon as one iterator is used
up with izip, the next iterator is discarded. But we are hitting "exhausted"
before we ever ask for an element from the starzip2 iterators, so it looks to
me like all the pairs from the first iterator are read into memory before the
second iterators are ever accessed...
Steve
More information about the Python-list
mailing list