finding max in python as per some custom criterion

后端 未结 4 599
夕颜
夕颜 2020-12-11 00:55

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)          


        
相关标签:
4条回答
  • 2020-12-11 01:44
    max(s, key=operator.methodcaller('arity'))
    

    or

    max(s, key=lambda x: x.arity())
    
    0 讨论(0)
  • 2020-12-11 01:52

    You can still use the max function:

    max_arity = max(s, key=lambda i: i.arity())
    
    0 讨论(0)
  • 2020-12-11 01:52

    I think the generator expression of doublep is better, but we seldom get to use methodcaller, so...

    from operator import methodcaller
    max(map(methodcaller('arity'), s))
    
    0 讨论(0)
  • 2020-12-11 01:56

    For instance,

    max (i.arity() for i in s)
    
    0 讨论(0)
提交回复
热议问题