2016-04-21 62 views
-2

我想按class1的值排序score_listPython:我如何排序字典裏面的列表

我可以按字母順序對score_list進行排序,只需對鍵進行排序,但我無法弄清楚如何從最大到最小的數值排序列表。

此外,我想score_list只存儲最後3個輸入,如果可能,我想找到每個學生最後3個分數的平均值。

enter code here: 




score_list = [] 
class1 = {} 
what_class = input ("what class would you like?") 
if what_class == ("1") : 
    print("class1 results") 
    student_name =input("name?") 
    score = input("score?") 
    choose = input("do you want to entre another student? y,n") 
    while choose == "y": 
     student_name =input("name?") 
     score = input("score?") 
     score_list.append(score) 
     choose = input("do you want to entre another score? y,n") 
     class1.setdefault(student_name,score_list) 
     if choose == "n": 
      sorted_names = sorted(class1.keys()) 
      print(class1) 

class1_items = class1.items() 
print (type(class1_items)) 

for i in class1_items: 
    print (i) 

,如果有人能幫助我,我會非常感激它
感謝你的幫助

[]的目標更好看:1

enter code here 
import heapq 
from collections import deque 
print("alphabetical(type:A),highest to lowest(type:B),Average(type:C)") 
student_name = input("ENTER NAME") 
result_type = input ("WHICH TYPE OF RESULTS WOULD YOU LIKE?") 
which_class = input ("which class?") 

if result_type == ("A") or result_type == ("a"): 
    print("alphabetical order(type:A)") 


if result_type == ("B") or result_type == ("b"): 
    print("results in highest to lowest.") 
    score_list = deque(maxlen=3) 
    scores = input("Please enter your score.") 
    choose = input("do you want to entre another score? y,n") 
    while choose != "n": 
     scores = input("Please enter your score.") 
     choose = input("do you want to entre another score? y,n") 
     score_list.append(scores) 
     if choose != "y" : 
      print(score_list) 
      print((student_name)+"'s:"+"lowest to largest"+str(heapq.nsmallest(3, score_list))) 
      print((student_name)+"'s:"+"largest to lowest"+str(heapq.nlargest(3, score_list))) 

if result_type == ("C") or result_type == ("c"): 
    print("results in average order.") 
    score_list = deque(maxlen=3) 
    scores = input("Please enter your score.") 
    choose = input("do you want to entre another score? y,n") 
    while choose != "n": 
     scores = input("Please enter the scores.") 
     choose = input("do you want to entre another score? y,n") 
     score_list.append(int(scores)) 
     #print(len(score_list)) 
     #print(sum(score_list)) 
     total = (int(sum(score_list))) 
     length = (int(len(score_list))) 
     average= (total)/(length) 
     if choose != "y" : 
      print(student_name+"'s "+"average score:"+str(average)) 
      #print("average: " +str(average)) 

這是怎麼了我改進了它,我仍然無法弄清楚按字母順序排序的部分。

the objectives to help you know what i am trying to do

回答

0

更新,你應該能夠只粘貼並運行下面的代碼 但儘管如此,你需要通過你的自我,這意味着包裹輸入迴路,連接接口/函數調用

import json 
class Quize(): 
    def __init__(self): 
     self.db = {} 

    def createClass(self, class_name): 
     self.db[class_name] = {} 

    def createUser(self, class_name, user_name): 
     self.db[class_name][user_name] = [] 

    def addScore(self, class_name, user_name, score): 
     self.db[class_name][user_name].append(score) 
     self.db[class_name][user_name] = self.db[class_name][user_name][-3:] 

    def printClass(self, class_name, order = None): 
     if not order: 
      print json.dumps(self.db[class_name], indent=4) 
     else: 
      to_sort = self.db[class_name] 
      print 'order', order 
      if order == 'by highest': 
       print sorted([ [max(to_sort[name]), name] for name in to_sort ], cmp = lambda x, y : y[0] - x[0]) 
      if order == 'by lowest': 
       print sorted([ [min(to_sort[name]), name] for name in to_sort ], cmp = lambda x, y : x[0] - y[0]) 
      if order == 'by average': 
       print sorted([ [sum(to_sort[name])/len(to_sort[name]), name] for name in to_sort ], 
           cmp = lambda x, y : y[0] - x[0]) 
      if order == 'by name': 
       print sorted([ [to_sort[name], name] for name in to_sort ], cmp = lambda x, y : x[1] > y[1]) 
    def printUser(self, class_name, user_name): 
     print json.dumps(self.db[class_name][user_name], indent=4) 

    def dump(self): 
     print json.dumps(self.db, indent = 4) 

ins = Quize() 
ins.createClass('0302') 
ins.createUser('0302', 'jack') 
ins.createUser('0302', 'tom') 

ins.addScore('0302', 'jack', 99) 
ins.addScore('0302', 'jack', 92) 
ins.addScore('0302', 'tom', 99) 
ins.addScore('0302', 'tom', 92) 
ins.addScore('0302', 'tom', 19) 
ins.addScore('0302', 'tom', 29) 
ins.addScore('0302', 'tom', 66) 

ins.printClass('0302', 'by highest') 
ins.printClass('0302', 'by lowest') 
ins.printClass('0302', 'by average') 
ins.printClass('0302', 'by name') 

ins.printUser('0302', 'tom') 
ins.printUser('0302', 'jack') 
ins.printClass('0302') 

ins.addScore('0302', 'jack', 11) 
ins.addScore('0302', 'jack', 37) 
ins.addScore('0302', 'tom', 80) 
ins.addScore('0302', 'tom', 72) 

ins.printClass('0302', 'by highest') 
ins.printClass('0302', 'by lowest') 
ins.printClass('0302', 'by average') 
ins.printClass('0302', 'by name') 

ins.printUser('0302', 'tom') 
ins.printUser('0302', 'jack') 
ins.printClass('0302') 
+0

,這並不似乎工作:這是我的任務 •\t做一個日誌,根據他們的表現給用戶。 •\t系統應該存儲每個學生的最後三個分數。 •\t輸出的測驗結果的特定類,分類: •\t與每個學生得分最高的測試 •\t由最高得分按字母順序排列,從最高到最低 •\t通過平均分,最高以最低 •\t允許教師在排序輸出數據時選擇要查看哪個課程組以及使用哪個字段。 –

+0

他沒有爲每個學生存儲多個測試分數,從我可以收集的數據中。編輯:好吧,所以我想他正在評論他的評論對不起! – Chris

+0

哦,男人,你只是太懶惰,python足夠方便表達想法 –

0

這裏有很多問題,但我會介紹幾個。您需要確保將輸入轉換爲int(),否則您將對字符串進行數學計算,這會導致問題。其次,也是最重要的是,字典沒有排序,所以你永遠不會得到相同的最後3個條目。

您可以使用列表理解來獲取等級並對它們進行排序,這是else之後的第一件事:因爲輸入順序是不相關的。對於接下來的兩個,你不能使用字典值,你必須參考你用來跟蹤輸入等級的列表。

grade_list = [] 
class1 = {} 
if input('what class would you like? ') == '1': 
    while True: 
      student_name = input('name? ') 
      score = int(input('score? ')) # must convert input to int 
      grade_list.append(score) 
      class1[student_name] = score 
      if input('do you want to enter another student? ') == 'y': 
       pass 
      else: 
       # fetch grades 
       grades = [v for k, v in class1.items()] 
       # print highest to lowest 
       print(sorted(grades, reverse=True)) 
       # print last 3 grades 
       print(grade_list[-3:]) 
       # print avg of last 3 grades 
       print(sum(grade_list[-3:])/3) 
       break 

輸出

what class would you like? 1 
name? Abby 
score? 11 
do you want to enter another student? y 
name? Bobby 
score? 22 
do you want to enter another student? y 
name? Charles 
score? 33 
do you want to enter another student? y 
name? Damon 
score? 44 
do you want to enter another student? n 
[44, 33, 22, 11] 
[22, 33, 44] 
33.0 
+0

我不知道如果你想爲每個學生存儲多個測試成績,但是如果你希望這爲如何解決問題提供了一個很好的基礎。 – Chris

+0

我需要一個所有學生的名字和他們的最後3個分數,而不僅僅是所有學生中的最高3個分數 –

+0

然後我建議你得到破解。這裏有足夠多的評論來幫助你入門。 – Chris