Re: [[x,f(x)] for x in list that maximizes f(x)] --newbie help
On 1 Dec 2005 05:45:54 -0800, Niels L Ellegaard [EMAIL PROTECTED] wrote: I just started learning python and I have been wondering. Is there a short pythonic way to find the element, x, of a list, mylist, that maximizes an expression f(x). In other words I am looking for a short version of the following: pair=[mylist[0],f(mylist[0])] for x in mylist[1:]: if f(x) pair[1]: pair=[x,f(x)] Reversing the order of the pair: mylist = range(10) def f(x): return 10-(x-5)**2 ... [f(x) for x in mylist] [-15, -6, 1, 6, 9, 10, 9, 6, 1, -6] Then one line should do it: max((f(x),x) for x in mylist) (10, 5) Ok, be picky ;-) pair = list(reversed(max((f(x),x) for x in mylist))) pair [5, 10] Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list
Re: [[x,f(x)] for x in list that maximizes f(x)] --newbie help
[EMAIL PROTECTED] wrote: Thanks. In that case, would it be easier to understand(beside the original iterative loop) if I use reduce and lambda ? You could try putting them side by side and seeing which is easiest for someone to understand: reduce(lambda (mv,mx), (v,x): mv v and (mv,mx) or (v,x), ((f(x), x) for x in mylist))) max((f(x),i,x) for i,x in enumerate(mylist))[2] max_x, max_f = mylist[0], f(mylist[0]) for x in mylist[1:]: new_f = f(x) if new_f max_f: max_x, max_f = x, new_f IMO, the last is clearest (but needs a list rather than an iterator), the middle is most concise and not too hard to understand and the first is a complete mess. YMMV. -- http://mail.python.org/mailman/listinfo/python-list
Re: [[x,f(x)] for x in list that maximizes f(x)] --newbie help
[EMAIL PROTECTED] wrote: ... 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) And that's why in 2.5 you'll just code max(mylist, key=f) to express this intent exactly -- that's precisely what 'key=' means. Alex -- http://mail.python.org/mailman/listinfo/python-list
[[x,f(x)] for x in list that maximizes f(x)] --newbie help
I just started learning python and I have been wondering. Is there a short pythonic way to find the element, x, of a list, mylist, that maximizes an expression f(x). In other words I am looking for a short version of the following: pair=[mylist[0],f(mylist[0])] for x in mylist[1:]: if f(x) pair[1]: pair=[x,f(x)] -- http://mail.python.org/mailman/listinfo/python-list
Re: [[x,f(x)] for x in list that maximizes f(x)] --newbie help
Niels L Ellegaard wrote: I just started learning python and I have been wondering. Is there a short pythonic way to find the element, x, of a list, mylist, that maximizes an expression f(x). In other words I am looking for a short version of the following: pair=[mylist[0],f(mylist[0])] for x in mylist[1:]: if f(x) pair[1]: pair=[x,f(x)] this is already very short, what else you want? May be this : max(((f(x), x) for x in mylist)) That is first generate the (f(x),x) pairs then find the max one(first compare f(x) then x) -- http://mail.python.org/mailman/listinfo/python-list
Re: [[x,f(x)] for x in list that maximizes f(x)] --newbie help
wrote: In other words I am looking for a short version of the following: pair=[mylist[0],f(mylist[0])] for x in mylist[1:]: if f(x) pair[1]: pair=[x,f(x)] this is already very short, what else you want? May be this : max(((f(x), x) for x in mylist)) That is first generate the (f(x),x) pairs then find the max one(first compare f(x) then x) It might be better to do: max((f(x),i,x) for i,x in enumerate(mylist))[2] as that will handle the case where x is not comparable but f(x) is. e.g. mylist = (3j, 5j+2, 1j) max((abs(x),i,x) for i,x in enumerate(mylist))[2] (2+5j) -- http://mail.python.org/mailman/listinfo/python-list
Re: [[x,f(x)] for x in list that maximizes f(x)] --newbie help
Duncan Booth wrote: wrote: In other words I am looking for a short version of the following: pair=[mylist[0],f(mylist[0])] for x in mylist[1:]: if f(x) pair[1]: pair=[x,f(x)] this is already very short, what else you want? May be this : max(((f(x), x) for x in mylist)) That is first generate the (f(x),x) pairs then find the max one(first compare f(x) then x) It might be better to do: max((f(x),i,x) for i,x in enumerate(mylist))[2] as that will handle the case where x is not comparable but f(x) is. e.g. mylist = (3j, 5j+2, 1j) max((abs(x),i,x) for i,x in enumerate(mylist))[2] (2+5j) thanks. I don't know what max can or cannot compare. -- http://mail.python.org/mailman/listinfo/python-list
Re: [[x,f(x)] for x in list that maximizes f(x)] --newbie help
[EMAIL PROTECTED] 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). Alex -- http://mail.python.org/mailman/listinfo/python-list
Re: [[x,f(x)] for x in list that maximizes f(x)] --newbie help
Alex Martelli wrote: [EMAIL PROTECTED] 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) -- http://mail.python.org/mailman/listinfo/python-list