我正在製作一個程序,它讀取文件內容並顯示模塊代碼,學號,學生姓名和最終標記從最高到最低。到目前爲止,我已經通過快速排序對最終標記進行了排序。Python如何根據最高到最低排序文件中的學生最後標記的每一行
這是我的文件的內容,77.5是17.5,20.0,20.0,20.0,20.0和最終成績,隨後被其他
DICT 201707011 Johnny_Guy 77.5 17.5 20.0 20.0 20.0
DICT 201707012 Jenny_Girl 70.0 17.5 17.5 17.5 17.5
DICT 201707013 Joshua_Tan 79.5 17.5 22.0 20.0 20.0
DICT 201707014 Jamine_Butters 67.5 17.5 10.0 20.0 20.0
DICT 201707015 Jone_Davy 97.5 17.5 30.0 30.0 20.0
這是我的代碼
from pathlib import Path
home = str(Path.home())
stdData = [] # store module code, student number and name
markData = [] # store the final mark of each student
def qsort(myList):
if not myList:
return []
else:
pivot = myList[0]
remainingList = myList[1:]
lessOrEqual = [ y for y in remainingList if y <= pivot ]
greater = [ y for y in remainingList if y > pivot ]
return qsort(greater) + [pivot] + qsort(lessOrEqual)
def sortedMark():
with open(home + "\\Desktop\\PADS Assignment\\test(sorted).txt", "a") as f:
with open(home + "\\Desktop\\PADS Assignment\\test(unsorted).txt", "r") as f1:
lines = [line.split() for line in f1] # convert each line in file into 2d array
print(lines)
for a in lines:
moduleCode = a[0] # DICT is the module code
stdNum = a[1] # 201707011 and so on is the student number
stdName = a[2] # Jonny_Guy and so on is the student name
finalMark = a[3] # 77.5 and 70.0 and so on is the final mark
markData.append(finalMark) # append final mark of lines in each line into an array
stdData.append(moduleCode)
stdData.append(stdNum)
stdData.append(stdName)
finalSortMark = qsort(markData)
print(finalSortMark)
sortedMark()
這是我的輸出,所述第一輸出是文件和所述第二輸出的2D陣列是學生的快速排序與排序最終標記
[['DICT', '201707011', 'Johnny_Guy', '77.5', '17.5', '20.0', '20.0', '20.0'], ['DICT', '201707012', 'Jenny_Girl', '70.0', '17.5', '17.5', '17.5', '17.5'], ['DICT', '201707013', 'Joshua_Tan', '79.5', '17.5', '22.0', '20.0', '20.0'], ['DICT', '201707014', 'Jamine_Butters', '67.5', '17.5', '10.0', '20.0', '20.0'], ['DICT', '201707015', 'Jone_Davy', '97.5', '17.5', '30.0', '30.0', '20.0']]
['97.5', '79.5', '77.5', '70.0', '67.5']
現在的問題是,我如何將模塊代碼,學號,學生姓名,最終標記從最高到最低排序到像這樣的輸出文件中?
DICT 201707015 Jone_Davy 97.5 17.5 30.0 30.0 20.0
DICT 201707013 Joshua_Tan 79.5 17.5 22.0 20.0 20.0
DICT 201707011 Johnny_Guy 77.5 17.5 20.0 20.0 20.0
DICT 201707012 Jenny_Girl 70.0 17.5 17.5 17.5 17.5
DICT 201707014 Jamine_Butters 67.5 17.5 10.0 20.0 20.0
你意識到你覆蓋已保留字'sorted'? – Mangohero1
在你的例子中,最後的分數從最低到最高排序,但你要求從最高分到最低分。你是指哪一個? – Antimony
ops,讓我編輯問題 –