Suppose I have two lists, one is student ID studentID=[1,4,2,3,5,10]
, the other is related scores for each individual student in the same index of the list studentScore=[1.0, 2.5, 3.0, 2.1, 5.0, 4.5]
(which means student with ID 1 has score 1.0, student with ID 4 has score 2.5, etc.), I want to order student ID by score (ascending order), which expected output is #[1,3,4,2,10,5]
.
My current solution is a bit naive, which setup a student class. In Python 2.7, wondering if any other ways to get sorted result of student ID based on related scores without using an additional class to setup their (two lists) relationship?
Post my current code below,
class Student:
def __init__(self, ID, score):
self.ID = ID
self.score = score
def __cmp__(self, other):
return cmp(self.score, other.score)
studentID=[1,4,2,3,5,10]
studentScore=[1.0, 2.5, 3.0, 2.1, 5.0, 4.5]
students = []
for i in range(len(studentID)):
students.append(Student(studentID[i], studentScore[i]))
sorted(students)
for i in students:
print i.ID
# expect output: sorted student ID by student score
#[1,3,4,2,10,5]