[Tutor] print as columns [using apply()]
Mike Serpa
onav@yahoo.com
Tue, 17 Jul 2001 16:12:02 -0700 (PDT)
>
> Danny Yoo wrote
> > The guilt is not really on apply(), but max() --- max() can't
> handle a
> > single argument: it always assumes that it works with at least two:
> >
> [snipped]
> >
> > On the other hand, reduce() should work on this pathological
> example:
> >
> > ###
> > >>> reduce(max, map(len, ([1],)))
> > 1
> > ###
> >
> >
> > I must remind myself to test my programs with small "boundary"
> cases, just
> > to make sure nothing leaks out... *grin*
>
> Actually, on this, I'm almost surprised. I'd think that you would
> have
> to add the trailing initial value to reduce.
>
> >>> reduce(max, map(len, ([1],)), 0)
>
> Interestingly, max((1,)) returns 1 (yikes).
>
> -Arcege
>
Okay, I think I've got it now. You still don't need reduce or apply.
a = [1,2,3]
b = [4,5,6,7]
c = [8,9]
max(map(len, (a, b, c)))
# or max(map(len, (a,)))
still works even if there is only one list.
the map function returns a list e.g. [3,4,2] # or [3]
max([3]) works fine.
If it's really preferable to use reduce or apply here please explain it
to me because I don't get it.
Mike
__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/