Re: for x in list that maximizes f(x)] <--newbie help
- From: bearophileHUGS@xxxxxxxxx
- Date: 1 Dec 2005 06:01:00 -0800
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
.
- References:
- [[x,f(x)] for x in list that maximizes f(x)] <--newbie help
- From: Niels L Ellegaard
- [[x,f(x)] for x in list that maximizes f(x)] <--newbie help
- Prev by Date: Re: General question about Python design goals
- Next by Date: Re: python speed
- Previous by thread: [[x,f(x)] for x in list that maximizes f(x)] <--newbie help
- Next by thread: Re: [[x,f(x)] for x in list that maximizes f(x)] <--newbie help
- Index(es):
Relevant Pages
|