Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python max-by function?

Tags:

python

Example:

print max(chain_length(i) for i in xrange(1,10001))

This returns the maximum/biggest "chain_length" (an arbitrary function), but what I want is the i value for input that produces the biggest value.

Is there a convenient way to do that?

like image 795
mpen Avatar asked Jul 05 '12 01:07

mpen


People also ask

Is Max () a method or function?

The max() method returns the largest value from the specified iterable or multiple arguments.

Can we use MAX function in list in Python?

The Python max() function returns the largest value in an iterable, such as a list. If a list contains strings, the last item alphabetically is returned by max().

Is Max a math function in Python?

Python – max() function. Python max() function returns the largest item in an iterable or the largest of two or more arguments. It has two forms.


1 Answers

max(xrange(1, 10001), key=chain_length)
like image 175
jamylak Avatar answered Sep 28 '22 06:09

jamylak