itertools.izip brokeness
bonono at gmail.com
bonono at gmail.com
Tue Jan 3 06:31:45 EST 2006
Paul Rubin wrote:
> > I think you need to use map(None,...) which would not drop anything,
> > just None filled. Though you don't have a relatively lazy version as
> > imap(None,...) doesn't behave like map but a bit like zip.
>
> I don't understand what you mean by this? None is not callable.
zip([1,2,3],[4,5]) gives [(1,4),(2,5)]
map(None,[1,2,3],[4,5]) gives [(1,4),(2,5),(3,None)]
So the result of map() can be filtered out for special processing. Of
course, your empty/sentinel filled version is doing more or less the
same thing.
>
> How about this (untested):
>
> def myzip(iterlist):
> """return zip of smaller and smaller list of iterables as the
> individual iterators run out"""
> sentinel = object() # unique sentinel
> def sentinel_append(iterable):
> return itertools.chain(iterable, itertools.repeat(sentinel))
> for i in itertools.izip(map(sentinel_append, iterlist)):
> r = [x for x in i.next() if x is not sentinel]
> if r: yield r
> else: break
More information about the Python-list
mailing list