alv*_*vas 2 python function list max argmax
如果我有一个列表和一个计算得分的函数,我可以这样计算argmax:
maxscore = 0; argmax = None
x = [3.49, 0.122, 293, 0.98] # Imagine a LARGE list.
for i in x:
# Maybe there're some other func() to calculate score
# For now just sum the digits in i.
score = sum([int(j) for j in str(i) if j.isdigit()])
print i, score
if maxscore < score:
maxscore = score
argmax = i
Run Code Online (Sandbox Code Playgroud)
有没有其他方法来实现argmax?什么是pythonic方式呢?
def score(i):
return sum([int(j) for j in str(i) if j.isdigit()])
max(x, key=score)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
428 次 |
最近记录: |