我之前已經問過類似的問題,所以我很抱歉,但是我讀回任務並誤解了最初的需求。排序字典鍵內的列表 - 從列表中獲得最高分3
因此,基於我在這裏獲得的反饋,這是我使用的代碼:
def task3():
classList = {}
classSearch = input("Which class would you like to interrogate? ")
try:
with open("answers " + classSearch + ".txt", 'rb') as handle:
classList = pickle.loads(handle.read())
except IOError as error:
print ("Sorry, this file does not exist")
sortOption = int(input("Would you like sort the students in alphabetical order? Enter 1\n Would you like to sort the students by highest score? Enter 2 \nWould you like to sort students by their average score?Enter 3\n"))
if sortOption == 1:
x = sorted(classList.items())
for key, value in x:
value.sort()
value.reverse()
print (x)
所以我真正需要做的是輸出每個學生的最高分,是按字母順序排序名稱。在classList字典裏面是學生名字,然後是包含他們在測驗中收到的最後3個分數的列表。對於多名學生來說這顯然是重複的。任何幫助將大規模讚賞。
標準字典是無序的。這意味着你無法對其進行分類。我想你需要一個有序的詞典。 https://docs.python.org/2/library/collections.html#collections.OrderedDict – 2014-12-03 10:56:45
謝謝,我自己最後通過打印索引位置來修復它0 – RH84 2014-12-03 12:27:23