如果我想要列表中的最大值,我可以写max(List),但如果我还需要最大值的索引怎么办?
我可以这样写:
maximum=0
for i,value in enumerate(List):
if value>maximum:
maximum=value
index=i
Run Code Online (Sandbox Code Playgroud)
但这对我来说看起来很乏味.
如果我写:
List.index(max(List))
Run Code Online (Sandbox Code Playgroud)
然后它将迭代列表两次.
有没有更好的办法?
Esc*_*alo 301
我认为接受的答案很棒,但为什么不明确地这样做呢?我觉得更多的人会理解你的代码,这与PEP 8一致:
max_value = max(my_list)
max_index = my_list.index(max_value)
Run Code Online (Sandbox Code Playgroud)
这种方法也比接受的答案快三倍:
import random
from datetime import datetime
import operator
def explicit(l):
max_val = max(l)
max_idx = l.index(max_val)
return max_idx, max_val
def implicit(l):
max_idx, max_val = max(enumerate(l), key=operator.itemgetter(1))
return max_idx, max_val
if __name__ == "__main__":
from timeit import Timer
t = Timer("explicit(l)", "from __main__ import explicit, implicit; "
"import random; import operator;"
"l = [random.random() for _ in xrange(100)]")
print "Explicit: %.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
t = Timer("implicit(l)", "from __main__ import explicit, implicit; "
"import random; import operator;"
"l = [random.random() for _ in xrange(100)]")
print "Implicit: %.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
Run Code Online (Sandbox Code Playgroud)
在我的电脑中运行的结果:
Explicit: 8.07 usec/pass
Implicit: 22.86 usec/pass
Run Code Online (Sandbox Code Playgroud)
其他套装:
Explicit: 6.80 usec/pass
Implicit: 19.01 usec/pass
Run Code Online (Sandbox Code Playgroud)
Sve*_*ach 174
有很多选择,例如:
import operator
index, value = max(enumerate(my_list), key=operator.itemgetter(1))
Run Code Online (Sandbox Code Playgroud)
por*_*ast 19
这个答案比@Escualo快33倍,假设列表非常大,假设它已经是一个np.array().我不得不拒绝测试运行次数,因为测试正在查看10000000个元素而不仅仅是100个元素.
import random
from datetime import datetime
import operator
import numpy as np
def explicit(l):
max_val = max(l)
max_idx = l.index(max_val)
return max_idx, max_val
def implicit(l):
max_idx, max_val = max(enumerate(l), key=operator.itemgetter(1))
return max_idx, max_val
def npmax(l):
max_idx = np.argmax(l)
max_val = l[max_idx]
return (max_idx, max_val)
if __name__ == "__main__":
from timeit import Timer
t = Timer("npmax(l)", "from __main__ import explicit, implicit, npmax; "
"import random; import operator; import numpy as np;"
"l = np.array([random.random() for _ in xrange(10000000)])")
print "Npmax: %.2f msec/pass" % (1000 * t.timeit(number=10)/10 )
t = Timer("explicit(l)", "from __main__ import explicit, implicit; "
"import random; import operator;"
"l = [random.random() for _ in xrange(10000000)]")
print "Explicit: %.2f msec/pass" % (1000 * t.timeit(number=10)/10 )
t = Timer("implicit(l)", "from __main__ import explicit, implicit; "
"import random; import operator;"
"l = [random.random() for _ in xrange(10000000)]")
print "Implicit: %.2f msec/pass" % (1000 * t.timeit(number=10)/10 )
Run Code Online (Sandbox Code Playgroud)
我的电脑上的结果:
Npmax: 8.78 msec/pass
Explicit: 290.01 msec/pass
Implicit: 790.27 msec/pass
Run Code Online (Sandbox Code Playgroud)
Sun*_*pil 16
使用Python内置库,它非常简单:
a = [2, 9, -10, 5, 18, 9]
max(xrange(len(a)), key = lambda x: a[x])
Run Code Online (Sandbox Code Playgroud)
Lui*_*eva 10
max([(v,i) for i,v in enumerate(my_list)])
Run Code Online (Sandbox Code Playgroud)
小智 6
我建议一种非常简单的方法:
import numpy as np
l = [10, 22, 8, 8, 11]
print(np.argmax(l))
print(np.argmin(l))
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
284199 次 |
| 最近记录: |