Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the max object as per some custom criterion?

Tags:

python

I can do max(s) to find the max of a sequence. But suppose I want to compute max according to my own function, something like:

currmax = 0
def mymax(s):
  for i in s :
    #assume arity() attribute is present
    currmax = i.arity() if i.arity() > currmax else currmax

Is there a clean pythonic way of doing this?

like image 841
MK. Avatar asked May 28 '10 19:05

MK.


People also ask

How will you get the max value item of a list?

Use max() to Find Max Value in a List of Strings and Dictionaries. The function max() also provides support for a list of strings and dictionary data types in Python. The function max() will return the largest element, ordered by alphabet, for a list of strings. The letter Z is the largest value, and A is the smallest.

How do you find the max of something in Python?

In Python, there is a built-in function max() you can use to find the largest number in a list. To use it, call the max() on a list of numbers. It then returns the greatest number in that list.

How do you find the max and min in Python?

Use Python's min() and max() to find smallest and largest values in your data. Call min() and max() with a single iterable or with any number of regular arguments. Use min() and max() with strings and dictionaries.


1 Answers

max(s, key=operator.methodcaller('arity'))

or

max(s, key=lambda x: x.arity())
like image 186
Ignacio Vazquez-Abrams Avatar answered Oct 07 '22 12:10

Ignacio Vazquez-Abrams