itertools.izip brokeness

Antoon Pardon apardon at forel.vub.ac.be
Mon Jan 9 09:19:21 CET 2006


Op 2006-01-05, Bengt Richter schreef <bokr at oz.net>:
> On 5 Jan 2006 15:48:26 GMT, Antoon Pardon <apardon at forel.vub.ac.be> wrote:
>
>>On 2006-01-04, rurpy at yahoo.com <rurpy at yahoo.com> wrote:
>>><rurpy at yahoo.com> wrote:
>>>> But here is my real question...
>>>> Why isn't something like this in itertools, or why shouldn't
>>>> it go into itertools?
>>>
>>>
>>>   4) If a need does arise, it can be met by __builtins__.map() or by
>>>      writing:  chain(iterable, repeat(None)).
>>>
>>> Yes, if youre a python guru.  I don't even understand the
>>> code presented in this thread that uses chain/repeat,
>>
>>And it wouldn't work in this case. chain(iterable, repeat(None))
>>changes your iterable into an iterator that first gives you
>>all elements in the iterator and when these are exhausted
>>will continue giving the repeat parameter. e.g.
>>
>>  chain([3,5,8],repeat("Bye")
>>
>>Will produce  3, 5 and 8 followed by an endless stream
>>of "Bye".
>>
>>But if you do this with all iterables, and you have to
>>because you don't know which one is the smaller, all
>>iterators will be infinite and izip will never stop.
>
> But you can fix that (only test is what you see ;-) :

Maybe, but not with this version.

> >>> from itertools import repeat, chain, izip
> >>> it = iter(lambda z=izip(chain([3,5,8],repeat("Bye")), chain([11,22],repeat("Bye"))):z.next(), ("Bye","Bye"))
> >>> for t in it: print t
>  ...
>  (3, 11)
>  (5, 22)
>  (8, 'Bye')
>
> (Feel free to generalize ;-)

The problem with this version is that it will stop if for some reason
each iterable contains a 'Bye' at the same place. Now this may seem
far fetched at first. But consider that if data is collected from
experiments certain values may be missing. This can be indicated
by a special "Missing Data" value in an iterable. But this "Missing
Data" value would also be the prime canidate for a fill parameter
when an iterable is exhausted.

-- 
Antoon Pardon



More information about the Python-list mailing list