Real-world use cases for map's None fill-in feature?
rurpy at yahoo.com
rurpy at yahoo.com
Thu Jan 12 18:47:09 EST 2006
"Raymond Hettinger" <python at rcn.com> wrote:
> [rurpy at yahoo.com]
> > > > How well correlated in the use of map()-with-fill with the
> > > > (need for) the use of zip/izip-with-fill?
>
> [raymond]
> > > Close to 100%. A non-iterator version of izip_longest() is exactly
> > > equivalent to map(None, it1, it2, ...).
>
> [rurpy at yahoo.com]
> > If I use map()
> > I can trivially determine the arguments lengths and deal with
> > unequal length before map(). With iterators that is more
> > difficult. So I can imagine many cases where izip might
> > be applicable but map not, and a lack of map use cases
> > not representative of izip use cases.
>
> You don't seem to understand what map() does. There is no need to
> deal with unequal argument lengths before map(); it does the work for
> you. It handles iterator inputs the same way. Meditate on this:
>
> def izip_longest(*args):
> return iter(map(None, *args))
>
> Modulo arbitrary fill values and lazily evaluated inputs, the semantics
> are exactly what is being requested. Ergo, lack of use cases for
> map(None,it1,it2) means that izip_longest(it1,it2) isn't needed.
"lazily evaluated inputs" is exactly what I was pointing
out and what make your izip_longest() above not the
same as map(None,...), and hence, your conclusion
invalid. Specifically....
def izip_longest(*args):
return iter(map(None, *args))
f1 = file ("test.dat")
f2 = file ("test.dat")
it = izip2 (f1, f2)
while 1:
h1, h2 = it.next ()
print h1.strip(), h2
izip2() in the above code is a "real" izip_longest
based on a version posted in this thread.
> test.py
3347 3347
-3487 -3487
2011 2011
239 239
...
Replace izip2 in the above code with your izip_longest
> test.py
[wait, wait, wait,... after a few minutes type ^c, nothing
happens, close window].
I don't think your izip_longest is at all equivalent to
the proposed izip, and thus there may well be uses
cases for izip that aren't represented by imap(None,...)
use cases, which is what I said. That is, I might have
a use case for izip which I would never even consider
map() for.
More information about the Python-list
mailing list