好吧,我有这个简单的函数,它可以找到使另一个正函数的值最大化的列表元素。更多pythonic方式找到列表中的元素,使函数最大化
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
但是我发现它做的简单工作很长。事实上,它让我想起Java(brrrr)。 任何人都可以告诉我一个更pythonic和干净的方式做到这一点?
谢谢!
曼努埃尔
除了风格,如果你仔细观察的话,你会注意到,在这个实现多个错误。 – 2010-02-11 06:01:34