我有3个字典,其中包含学生hw,测验和考试成绩的分数,但是,我需要将这三个字典放入一个超级字典中,其中所有学生的分数都具有以下结构。
“123-45-6789”:{“hw”:[98,89,92,75],“测验”:[45,36,42,50,29,27,40,41],“考试“:[175157]}
我尝试制作一个默认字典,其中包含所有学生ID作为键,然后是空值,但我不太熟悉如何正确操作字典,因此如果要找到一个键将字典添加到字典中超级字典
以下是我将3个文本文件转换为3个词典的代码
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}
答案 0 :(得分:0)
public bool IsArrayEmpty(object[] arr)
{
return arr == null || arr.Length < 1;
}
事实证明,这就是我必须要做的所有这一循环,因为fullRoster是一个字典的学生作为键,值是一个空字典本身,循环迭代找到适当的匹配ID然后添加它。