[Tutor] print as columns [using apply()]

Michael P. Reilly arcege@speakeasy.net
Tue, 17 Jul 2001 20:15:18 -0400 (EDT)


Mike Serpa wrote
> If it's really preferable to use reduce or apply here please explain it
> to me because I don't get it.

Most times it will be preferable because most functions won't take the
arguments as you might want them.  The apply, filter, map and reduce
functions, along with the lambda construct (generally called "functional
programming"), are all to help process things more easily.

Also, as you can see, some of us veterans got confused about the max
function.  Guido made an effort in 2.0 to kind of standardize the single
value sequence vs. multiple value arguments.  So if Guido continues that
effort, max/min may no longer take a single sequence to process.

Also, remember that in a somewhat un-python-like fashion (*wink*),
it this can be done in other ways.

Standard loops:
>>> maxval = 0
>>> for l in [a, b, c]:
...   maxval = max( len(l), maxval )
...
>>> maxval = 0
>>> list_o_lists = (a, b, c)
>>> while list_o_lists:
...   l, list_o_lists = list_o_lists[0], list_o_lists[1:]
...   maxval = max( len(l), maxval)
...

or even the yucky list comprehensions added in Python 2.0:
>>> max( [len(l) for l in (a, b, c)] )

But IMO, list comprehensions are little more than perl-like syntax sugar
and reduce readability.

Basically to answer your question, it is often more helpful to use the
above functions (apply, filter, etc.) together to get the desired results.
But, use the tools available to you as seems natural: loops, map, etc.

  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |