好吧,我有這個簡單的函數,它可以找到使另一個正函數的值最大化的列表元素。更多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