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

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Thu Dec 1 09:01:00 EST 2005


If the elements of mylist can be compared (es. not complex values),
then this can be a solution for you:

from math import sin as f
mylist = [float(2*i)/10 for i in xrange(10)]
pairs = [(f(x), x) for x in mylist]
ymax = max(pairs)[1]
print pairs, "\n"
print ymax

You can also try this, for Py2.4:
print max((f(x), x) for x in mylist)[1]

Bye,
bearophile




More information about the Python-list mailing list