[英]How to sort students list according to their Marks using Insertion sort in Python
I am trying to sort students list and rank each student according to their marks using insertion sort.我正在尝试对学生列表进行排序,并使用插入排序根据他们的分数对每个学生进行排名。 Data of students include Name, Roll no, Address, Mark.学生资料包括姓名、卷号、地址、分数。
Here, I store the Mark of students in one list - Marklist
and other data of students in a second list - stdData
.在这里,我将学生的标记存储在一个列表中 - Marklist
和学生的其他数据在第二个列表 - stdData
中。
I sorted the student Mark List using Insertion sort.我使用插入排序对学生标记列表进行了排序。 But right now I have 2 separate lists.但现在我有 2 个单独的列表。 How can I merge and print the sorted list of each student with their marks?如何合并并打印每个学生的排序列表及其分数?
import csv
stdData = [] # store RollNum,student name last name,address
Marklist = [] # store the final mark of each student
#generallist=[]
with open("studentlist.csv", "r") as f1:
recordReader = csv.DictReader(f1)
for row in recordReader:
#generallist.append(row)
row['Mark']=int(row['Mark'])
Marklist.append(row['Mark'])
stdData.append(row['RollNo'])
stdData.append(row['Name'])
stdData.append(row['LastName'])
stdData.append(row['Address'])
print(Marklist)
print(stdData)
for i in range(1, len(Marklist)):
key = Marklist[i]
j = i - 1
while j >= 0 and key < Marklist[j]:
Marklist[j + 1] = Marklist[j]
j -= 1
Marklist[j + 1] = key
print("Sorted List: ",Marklist)
Thanks.谢谢。
You are very near to the correct solution.您非常接近正确的解决方案。 The answer lies in答案在于
[ [student1 details], [student2 details], [student3 details] ]
例如: [ [student1 details], [student2 details], [student3 details] ]
stdData
using the indices of MarkList
.使用stdData
的索引对MarkList
进行排序。Below is the code modified to address the above points:以下是为解决上述问题而修改的代码:
import csv
stdData = [] # store RollNum,student name last name,address
Marklist = [] # store the final mark of each student
generallist=[]
with open("studentlist.csv", "r") as f1:
recordReader = csv.DictReader(f1)
for row in recordReader:
#generallist.append(row)
row['Mark']=int(row['Mark'])
Marklist.append(row['Mark'])
tmp_data = []
tmp_data.append(row['RollNo'])
tmp_data.append(row['Name'])
tmp_data.append(row['LastName'])
tmp_data.append(row['Address'])
stdData.append(tmp_data) # Storing student details as list of lists
print(Marklist)
print(stdData)
for i in range(1, len(Marklist)):
key = Marklist[i]
data = stdData[i] # Sort the elements in stdData using indices of MarkList
j = i - 1
while j >= 0 and key < Marklist[j]:
Marklist[j + 1] = Marklist[j]
stdData[j+1] = stdData[j]
j -= 1
Marklist[j + 1] = key
stdData[j+1] = data
print("Sorted List: ",Marklist)
for student_data in stdData:
print(student_data)
Even though the above solution gives the correct answer, it uses two lists.即使上述解决方案给出了正确的答案,它也使用了两个列表。
We can sort a list using keys (need not to be actual list elements).我们可以使用键对列表进行排序(不必是实际的列表元素)。 The below code implements it and is a better solution.下面的代码实现了它,是一个更好的解决方案。
import csv
stdData = [] # store RollNum,student name last name,address
with open("studentlist.csv", "r") as f1:
recordReader = csv.DictReader(f1)
for row in recordReader:
tmp_data = []
tmp_data.append(row['RollNo'])
tmp_data.append(row['Name'])
tmp_data.append(int(row['Mark']))
tmp_data.append(row['LastName'])
tmp_data.append(row['Address'])
stdData.append(tmp_data) # Storing student details as list of lists
print(stdData)
for i in range(1, len(stdData)):
key = stdData[i][2] # here the key is the mark
data = stdData[i] # we will copy the data to correct index
j = i - 1
while j >= 0 and key < stdData[j][2]:
stdData[j+1] = stdData[j]
j -= 1
stdData[j+1] = data
print("Sorted List:")
for rollno, name, mark, lastname, address in stdData:
print(rollno, name, mark, lastname, address)
Happy coding.快乐编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.