views:

90

answers:

4

Hi,

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 so -

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?

Thanks!

+2  A: 

You can still use the max function:

max_arity = max(s, key=lambda i: i.arity())
jellybean
Interesting, I didn't know you could do that.
twneale
+6  A: 

For instance,

max (i.arity() for i in s)
doublep
+8  A: 
max(s, key=operator.methodcaller('arity'))

or

max(s, key=lambda x: x.arity())
Ignacio Vazquez-Abrams