我有3本词典,其中包含学生hw,测验和考试分数的分数,但是,我需要将这三个词典放入超级词典中,其中所有学生的分数都与以下结构相同。比较字典,然后附加它们,如果是真的?
“123-45-6789”:{“hw”:[98,89,92,75],“quiz”:[45,36,42,50,29,27,40,41],“考试“[175,157]}
我试着制作一个默认词典,所有的学生id都是键值,然后是空值,但我不太熟悉如何正确操作词典,这样如果一个键是发现添加字典进入超级字典
下面是一些代码我有3个转换文本文件到3个dictionaies
def create_dictionary():
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}
什么是每个输入文件的格式?你能提一下这个问题吗? –
要检查密钥是否存在于字典中,可以在student_id_dict.keys()中使用'if'student_id'之类的内容。如果键存在,则可以将该值附加到现有列表中,否则您可以创建新的键值对。 –
@RohanSadale这些文件是所有学生ID的转储,其旁边有一个分数: 709-40-8165 168 560-33-3099 176 503-27-1729 194 267-10-7633 142 807-49-0073 82 –