因此,我的代码应该做的是获取学生ID,并将其放入字典中,然后再将其作业,考试和测验成绩也放入字典中。
所以应该看起来像
{'123456':{'exam':[97, 98, 45], 'homework': [44,45]} etc.
因此,为了快速起见,我只添加了一部分来关注功课部分。我该如何将作业字典添加到学生证的字典中?(作业文件中有学生编号,然后是他们的成绩)
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)
我关闭了吗?
您的问题不清楚,因为输入数据的形式未知。但总的来说,您可以执行以下操作:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句