[Tutor] print as columns
Michael P. Reilly
arcege@speakeasy.net
Tue, 17 Jul 2001 15:09:00 -0400 (EDT)
Mike Serpa wrote
> > 1) you can find the longest in a list of lists by using the
> > reduce function which keeps applying the function to
> > successive pairs of elements from a list:
> >
> > n=len(reduce(lambda a,b:max(a,b),l))
> > --- ------------------- -
> > ^ ^ ^->the list of lists (called Lists
> > in
> > your program)
> > | |
> > | |_ the function which returns the longer of two lists
> > |
> > |_ when the reduce is done we will know the longest list, len
> > gives the
> > length of it
> >
> > Example:
> >
> > a=[1,2,3,4]
> > b=[5,6,7]
> > c=[8,9,10,11,12]
> >
> > n=len( reduce( lambda a,b:max(a,b), [a,b,c] ))
> >
> > n is 5
>
> Couldn't we shorten this to:
> n=len(max(lists))
>
> It seems that everything I figure out how to do is already in a module
> somewhere. Question for expert Python coders (or any lang.) Do you
> ever get to the stage where you don't have to constantly look things
> up in the lang docs?
Actually, none of these work. The max of a list of lists is to find out
which list is greater, by component, not the longer.
>>> a = [1, 2, 3]
>>> b = [3, 4, 5, 6]
>>> max(a, b)
[3, 4, 5, 6]
>>> c = [7, 8, 9]
>>> max(b, c)
[7, 8, 9]
You probably want to use: reduce(max, map(len, seq_o_lists))
>>> reduce(max, map(len, (a, b, c)))
4
About the only thing I look to the docs for now are some of modules I
use less frequently, or when trying to verify something for the list.
-Arcege
--
+----------------------------------+-----------------------------------+
| Michael P. Reilly | arcege@speakeasy.net |