所以我的代码应该做的是获取学生ID并将它们放入字典中,然后将他们的作业,考试和测验成绩也输入字典中。
所以它应该看起来像
{'123456':{'exam':[97, 98, 45], 'homework': [44,45]} etc.
因此,为了快速,我只添加了一部分专注于作业部分。我如何将该作业词典添加到学生ID字典中? (作业文件的学生编号后跟其成绩)
def create_dictionary(idfilename, hwfilename):
ids = open(idfilename, 'r')
hw = open(hwfilename, 'r')
d = {}
grades = {}
for i in ids:
ids.readline()
i = i.rstrip("\n")
grades[i] = d
for h in hw:
x = hw.readline()
x.split(' ')
if h in grades:
d = [i]
print(grades)
我离开了吗?
答案 0 :(得分:0)
您的问题不明确,因为输入数据的格式未知。但总的来说,你可以这样做:
out_dict = {'123456':{'exam':[97, 98, 45], 'homework': [44,45]}}
# example of a list with grades. maybe read from a file?
homework_grades = [1,2,3,4]
# appends grades one at a time
for a_grade in homework_grades:
out_dict['123456']['homework'].append(a_grade)
# or append all grades in one go.
out_dict['123456']['homework'] += homework_grades