Skip to content
Advertisement

Find max value of a column based on another in python

i have 2d list implementation as follows. It shows no. of times every student topped in exams:-

  list = main_record
  ['student1',1]
  ['student2',1]
  ['student2',2]
  ['student1',5]
  ['student3',3]

i have another list of unique students as follows:-

  list = students_enrolled
  ['student1','student2','student3']

which i want to display student ranking based on their distinctions as follows:-

  list = student_ranking
  ['student1','student3','student2']

What built in functions can be useful. I could not pose proper query on net. In other words i need python equivalent of following queries:-

select max(main_record[1]) where name = student1 >>> result = 5 
select max(main_record[1]) where name = student2 >>> result = 2
select max(main_record[1]) where name = student3 >>> result = 3

  

Advertisement

Answer

You define a dict base key of studentX and save the max value for each student key then sort the students_enrolled base max value of each key.

from collections import defaultdict

main_record = [['student1',1], ['student2',1], ['student2',2], ['student1',5], ['student3',3]]
students_enrolled = ['student1','student2','student3']

# defind dict with negative infinity and update with max in each iteration
tmp_dct = defaultdict(lambda: float('-inf'))
for lst in main_record:
    k, v = lst
    tmp_dct[k] = max(tmp_dct[k], v)
print(tmp_dct)

students_enrolled.sort(key = lambda x: tmp_dct[x], reverse=True)
print(students_enrolled)

Output:

# tmp_dct =>
defaultdict(<function <lambda> at 0x7fd81044b1f0>, 
            {'student1': 5, 'student2': 2, 'student3': 3})

# students_enrolled after sorting
['student1', 'student3', 'student2']
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement