0
您好,我最近開始編程,並遇到一個有關exerciseism練習的小問題。 練習題:(https://github.com/exercism/xpython/tree/master/grade-school?)在Dictionary中創建格式化值python
我無法讓它通過最後的測試。 它要求的輸出是按以下方式:
sorted_students = {3:( 「凱爾」,),4:( 「克里斯托弗」, 「珍」,),6:( 「天鉤」 ,)}
我將信息存儲在字典中,成績是關鍵字,學生包含在集合中作爲值。然而,最後的測試希望字典中的值是某種列表,我不知道如何創建它。 到目前爲止,我的解決方案輸出正確的值並在值周圍增加[]括號。我得到的錯誤如下:
{3:('Kyle',),4 :('Christopher','Jennifer'),6 :('Kareem',)}!= {3: [( '凱爾',)],4:[( '克里斯托弗',),( '珍',)],6:[( '卡里姆',)]}
我的代碼,以便:
class School:
def __init__(self,name):
self.name = name
self.db={}
def add(self,student, grad):
if grad not in self.db:
self.db[grad]={student}
else:
self.db[grad].add(student)
def grade(self,grad):
if grad not in self.db:
return set()
else:
return self.db[grad]
def sort(self): #where I fail
newdict={} #new dictionary
for i in self.db:
newdict[i]=[(k,) for k in self.db[i]] #converst from set to list
return newdict
這是一個字典'{INT:元組}'。見例如https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences – jonrsharpe 2014-11-14 16:38:28