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



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

.



Relevant Pages

  • Re: List of strings to list of floats ?
    ... from math import hypot ... Prev by Date: ...
    (comp.lang.python)
  • Re: Simple and safe evaluator
    ... a few more funcs like int() and sin(). ... # expose selected math constants and functions ... I'm assuming that the _ast definitions are buried in the C part of python, but that is just a silly guess. ...
    (comp.lang.python)
  • changing txt to number
    ... but the field type is txt, in my asp ... do math with the data. ... Prev by Date: ...
    (microsoft.public.inetserver.asp.db)
  • Re: globals?
    ... It is completely relevant if you use an alternative math library. ... Nobody I know qualifies Sin() with Math they ... various programming languages. ... You can also say that other programming ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Age of dog/ personality
    ... Prev by Date: ...
    (rec.pets.dogs.behavior)