[[x,f(x)] for x in list that maximizes f(x)] <--newbie help

bonono at gmail.com bonono at gmail.com
Thu Dec 1 11:31:27 EST 2005


Alex Martelli wrote:
> <bonono at gmail.com> wrote:
>    ...
> > thanks. I don't know what max can or cannot compare.
>
> Just the same things that you can compare with, say, < .
>
> I believe in 2.5 max and min will also accept a key= argument (like
> sorted etc) to tweak what to compare, so max(thelist, key=f) should then
> work (but in 2.4 you do need to more explicitly use some kind of
> decorate-sort-undecorate idiom, as explained in previous posts).
>
Thanks. In that case, would it be easier to understand(beside the
original iterative loop) if I use reduce and lambda ?

reduce(lambda (mv,mx), (v,x): mv > v and (mv,mx) or (v,x), ((f(x), x)
for x in mylist)))

As while DSU is a very smart way to guard the max compare thing, it is
still being introduced as a way that is not related to the original
problem, i.e. I just want to compare f(x)




More information about the Python-list mailing list