Pythonic的方式来find最大值和它的索引在列表中?

如果我想要列表中的最大值,我可以写max(List) ,但是如果我还需要最大值的索引呢?

我可以写这样的东西:

 maximum=0 for i,value in enumerate(List): if value>maximum: maximum=value index=i 

但是这对我来说很乏味。

如果我写:

 List.index(max(List)) 

然后它将迭代列表两次。

有没有更好的办法?

有很多select,例如:

 import operator index, value = max(enumerate(my_list), key=operator.itemgetter(1)) 

我认为接受的答案很好,但是你为什么不明确地做呢? 我觉得更多的人会理解你的代码,这与PEP 8是一致的:

 max_value = max(my_list) max_index = my_list.index(max_value) 

这个方法也比接受的答案快三倍:

 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) 

结果,因为他们在我的电脑上运行:

 Explicit: 8.07 usec/pass Implicit: 22.86 usec/pass 

其他设置:

 Explicit: 6.80 usec/pass Implicit: 19.01 usec/pass 

这个答案比@Escualo快33倍,假设列表非常大,并假设它已经是一个np.array()。 我不得不拒绝testing运行的次数,因为这个testing看起来不仅仅是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 ) 

结果在我的电脑上:

 Npmax: 8.78 msec/pass Explicit: 290.01 msec/pass Implicit: 790.27 msec/pass 
 max([(v,i) for i,v in enumerate(my_list)]) 

用Python构build的库很容易:

 a = [2, 9, -10, 5, 18, 9] max(xrange(len(a)), key = lambda x: a[x]) 

也许你需要一个sorting列表吗?

尝试这个:

 your_list = [13, 352, 2553, 0.5, 89, 0.4] sorted_list = sorted(your_list) index_of_higher_value = your_list.index(sorted_list[-1]) 
 max([(value,index) for index,value in enumerate(your_list)]) #if maximum value is present more than once in your list then this will return index of the last occurrence 

如果现在不止一次出现最大值,并且想获得所有指数,

 max_value = max(your_list) maxIndexList = [index for index,value in enumerate(your_list) if value==max(your_list)]