更多pythonic方法在列表中查找最大化函数的元素

Man*_*áoz 4 python coding-style list maximize

好的,我有这个简单的函数,它找到列表中最大化另一个正函数值的元素.

def get_max(f, s):
    # f is a function and s is an iterable

    best = None
    best_value = -1

    for element in s:
        this_value = f(element)
        if this_value > best_value:
            best = element
            best_value = this_value
    return best
Run Code Online (Sandbox Code Playgroud)

但我发现它的工作很简单.事实上,它让我想起了Java(brrrr).任何人都可以告诉我一个更加pythonic和干净的方式吗?

谢谢!
曼努埃尔

Ale*_*lli 15

def get_max(f, s):
  return max(s, key=f)
Run Code Online (Sandbox Code Playgroud)