inverse of izip

Satchidanand Haridas sharidas at zeomega.com
Thu Aug 19 06:01:33 EDT 2004


Steven Bethard wrote:

>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:
>
>  
>
Could you expand on what you mean by exhaust the iterators too early?

The reason I ask is that the * operator is applied to 
((1,1),(2,2),....(9,9)). The operation of the 
itertools.izip(range10(),range10()) is completed before the * operation 
is applied. And the iter() simply converts the result of the inverse 
izip operation into an iterator.  I hope the above was not too 
confusing. :-)

I am trying to understand a little about the izip myself. Thanks.

Satchit


>>>>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
>  
>

Could you expand on what you mean by exhaust the iterators too early?

The reason I ask is that the * operator is applied to the tuple 
((1,1),(2,2),...(9,9)). Actually to the iterator which is called 10 
times, each time returning (i,i) for 0<=0<10. When the iterator is 
called the 11th time, it prints "exhausted".

So the operation of the itertools.izip(range10(),range10()) is completed 
and "exhausted" is printed before the * operation is applied. The iter() 
simply converts the result of the inverse izip operation which into an 
iterator.  I hope the above was not too confusing. :-)

I am trying to understand what goes on inside izip myself. Thanks.

Regards,
Satchit







More information about the Python-list mailing list