所以我設法將3個txt文件轉換成一個字典列表,但現在我必須將這3個字典放入具有以下結構的超級字典中。其中有3個字典的字典
「123-45-6789」:{「hw」:[98,89,92,75],「quiz」:[45,36,42,50,29,27,40,41],「考試「:[175157]}
我提出與所有的都沒有的值 的studentids默認字典和我有三個字典稱爲examList,hwList和quizList具有以下結構(值的長度而變化)
{ '709-40-8165':[168,98], '560-33-3099':[176,16]}
所以問題是如何能夠通過默認字典studentid迭代添加以下字典?
下面是一些代碼
fullRoster= dict()
idList= []
quizList= []
hwList= []
examList= []
studentids= open("studentids.txt", "r")
idList= [line.rstrip()for line in studentids]
studentids.close()
idList= dict.fromkeys(idList)
#hwFile converted into a list and then into a dictionary
#the exam and homework files follow the same structure
hwFile= open("hwscores.txt", "r")
hwList= [line.rstrip().split() for line in hwFile]
hwFile.close()
#searches for similar ids then places quiz score into single list
for i in range (15):
for k in range ((len(hwList))):
if hwList[i][0]== hwList[k][0] and i!=k:
hwList[i].append((hwList[k][1]))
hwList= hwList[:15]
#adds zero if hw list is not 5
for i in range (15):
if len(hwList[i])!=5:
while len(hwList[i])<5:
hwList[i].append(0)
#dictionary comprehension to create dictionary
hwList= {l[0]: [int(x) for x in l[1:]] for l in hwList}
如果您可以顯示一些代碼並引用該代碼來解釋您的問題正在發生什麼,那麼將更好地說明您的問題。它會讓讀者對如何幫助你有更快/更好的理解。 – idjaw
@idjaw剛剛編輯 –