在python列表中查找最大值和索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39748916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:39:35  来源:igfitidea点击:

Find maximum value and index in a python list?

pythonlistmax

提问by rksh

I have a python list that is like this,

我有一个像这样的python列表,

[[12587961, 0.7777777777777778], [12587970, 0.5172413793103449], [12587979, 0.3968253968253968], [12587982, 0.88], [12587984, 0.8484848484848485], [12587992, 0.7777777777777778], [12587995, 0.8070175438596491], [12588015, 0.4358974358974359], [12588023, 0.8985507246376812], [12588037, 0.5555555555555555], [12588042, 0.9473684210526315]]

This list can be up to thousand elements in length, how can I get the maximum value in the list according to the second item in the sub-array, and get the index of the maximum value which is the fist element in the sub-array in python?

这个列表的长度最多可以有千个元素,如何根据子数组中的第二项获取列表中的最大值,并获取最大值的索引,即子数组中的第一个元素在蟒蛇?

回答by thefourtheye

Use the maxfunction and its keyparameter, to use only the second element to compare elements of the list.

使用max函数及其key参数,仅使用第二个元素来比较列表的元素。

For example,

例如,

>>> data = [[12587961, 0.7777777777777778], [12587970, 0.5172413793103449], [12587979, 0.3968253968253968].... [12588042, 0.9473684210
526315]]
>>> max(data, key=lambda item: item[1])
[12588042, 0.9473684210526315]

Now, if you want just the first element, then you can simply get the first element alone, or just unpack the result, like this

现在,如果你只想要第一个元素,那么你可以简单地单独获取第一个元素,或者只是解压结果,就像这样

>>> index, value = max(data, key=lambda item: item[1])
>>> index
12588042
>>> value
0.9473684210526315


Edit: If you want to find the maximum index (first value) out of all elements with the maximum value (second value), then you can do it like this

编辑:如果您想从具有最大值(第二个值)的所有元素中找到最大索引(第一个值),那么您可以这样做

>>> _, max_value = max(data, key=lambda item: item[1])
>>> max(index for index, value in data if value == max_value)

You can do the same in a single iteration, like this

你可以在一次迭代中做同样的事情,就像这样

max_index = float("-inf")
max_value = float("-inf")

for index, value in data:
      if value > max_value:
          max_value = value
          max_index = index
      elif value == max_value:
          max_index = max(max_index, index)

回答by Patrick Haugh

Use maxwith a key.

使用max钥匙。

l = [[12587961, 0.7777777777777778], [12587970, 0.5172413793103449], [12587979, 0.3968253968253968], [12587982, 0.88], [12587984, 0.8484848484848485], [12587992, 0.7777777777777778], [12587995, 0.8070175438596491], [12588015, 0.4358974358974359], [12588023, 0.8985507246376812], [12588037, 0.5555555555555555], [12588042, 0.9473684210526315]]
max_sub = max(l, key=lambda x: x[1])
max_val = max_sub[1]
max_index = max_sub[0]

回答by Alberto Garcia-Raboso

from operator import itemgetter

a = [[12587961, 0.7777777777777778], [12587970, 0.5172413793103449], [12587979, 0.3968253968253968], [12587982, 0.88], [12587984, 0.8484848484848485], [12587992, 0.7777777777777778], [12587995, 0.8070175438596491], [12588015, 0.4358974358974359], [12588023, 0.8985507246376812], [12588037, 0.5555555555555555], [12588042, 0.9473684210526315]]

max(a, key=itemgetter(1))[0]
// => 12588042

回答by Janarthanan .S

allData = [[12587961, 0.7777777777777778], [12587970, 0.5172413793103449], [12587979, 0.3968253968253968], [12587982, 0.88], [12587984, 0.8484848484848485], [12587992, 0.7777777777777778], [12587995, 0.8070175438596491], [12588015, 0.4358974358974359], [12588023, 0.8985507246376812], [12588037, 0.5555555555555555], [12588042, 0.9473684210526315]]

listOfSecondData = [i[1] for i in allData]
result = allData[listOfSecondData.index(max(listOfSecondData))][0]

print(result)
#Output: 12588042

回答by TanJay

Simple

简单的

list = [[12587961, 0.7777777777777778], [12587970, 0.5172413793103449], [12587979, 0.3968253968253968], [12587982, 0.88], [12587984, 0.8484848484848485], [12587992, 0.7777777777777778], [12587995, 0.8070175438596491], [12588015, 0.4358974358974359], [12588023, 0.8985507246376812], [12588037, 0.5555555555555555], [12588042, 0.9473684210526315]]
list2 = []

for x in list:
    list2.append(x[1])
print "index->" + str(list[list2.index(max(list2))][0])
print "max value->" + str(list[list2.index(max(list2))][1])