0

I have two lists and I want to sort the one of them (scores) in reverse order and get the corresponding indexes in order to sort the second one (section_id).

For example:

section_id = [5, 6, 8, 14]
scores = [4, 11, 13, 7]

The new lists will be:

sorted_reverse_scores = [13, 11, 7, 4]
sorted_section_id = [8, 6, 14, 5]

Do you know how to achieve this?

Currently the only thing I do is:

sorted_reverse_scores = section_id.sort(reverse=True)
zinon
  • 4,427
  • 14
  • 70
  • 112

1 Answers1

1
section_id  = [5, 6, 8, 14]
scores  = [4, 11, 13, 7]
sorted_reverse_scores =[]
sorted_section_id =[]
for i in sorted(zip(scores,section_id),reverse=True):
  sorted_reverse_scores.append(i[0])
  sorted_section_id.append(i[1])
print(sorted_reverse_scores)
print(sorted_section_id)

output

[13, 11, 7, 4] [8, 6, 14, 5]

Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27