23

I have a python list that is like this,

[[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?

4
  • You are right @unwind. I think he wants to find all the elements with maximum value according to the second element and find the maximum out of them based on the first value. Commented Sep 28, 2016 at 13:43
  • well sorry if you guys didn't get it, it's like this I want the subarray with the maximum second value, second value means the [1]th item in a subarray Commented Sep 28, 2016 at 13:51
  • Okay ... I still struggle to understand. A small example on e.g. a four or five element list would be helpful. But I guess it's already solved. Commented Sep 28, 2016 at 13:52
  • @rksh What if more than one subarray has got the maximum second value? Commented Sep 28, 2016 at 13:54

5 Answers 5

39

Use the max function and its key parameter, to use only the second element to compare elements of the list.

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks actually what I wanted was the something like you mentioned in the original answer, the subarray with the maxumum second value in a subarray :) is there a way to get the n number of arrays with the maximum value, like top 10 highest values :)
@rksh You should give proper examples. The words are simply confusing.
ok i will take a simple example if there is an array like this data = [1,2,5,7,6,8,3,9,4] how can i get the three maximum values? in this case it's 7,8,9
@rksh Sort them and slice the result list.
5

Use max with a key.

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]

Comments

1
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

Comments

0
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

Comments

-3

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])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.