修改代码,以便能够向我的字典添加另一个值



我需要创建一个字典,它的键是唯一的课程代码,它的值是具有单个键值对的字典,按字母顺序描述该课程代码可用的部分,以及属于特定部分的ID号,按升序排列。

数据来自一个名为student_data的字典,如下所示:

student_data[:3]
>>>[{'enlistment': [{'course code': 'UK 60', 'section': 'A'}, 
{'course code': 'GF 24', 'section': 'B'}, 
{'course code': 'ME 40', 'section': 'B'}, 
{'course code': 'VY 44', 'section': 'D'}, 
{'course code': 'EN 94', 'section': 'B'}], 
'id': '201001', 'paid': True, 'school': 'SOSE', 'year level': 2}, 
{'enlistment': [{'course code': 'EQ 61', 'section': 'D'}, 
{'course code': 'UZ 22', 'section': 'B'}, 
{'course code': 'KS 36', 'section': 'B'}, 
{'course code': 'VH 63', 'section': 'A'}, 
{'course code': 'IW 81', 'section': 'C'}], 
'id': '211002', 'paid': True, 'school': 'JGSOM', 'year level': 1}, 
{'enlistment': [{'course code': 'WE 15', 'section': 'D'}, 
{'course code': 'ZP 68', 'section': 'A'}, 
{'course code': 'GI 78', 'section': 'A'}, 
{'course code': 'GK 72', 'section': 'C'}, 
{'course code': 'FA 24', 'section': 'D'}, 
{'course code': 'UJ 28', 'section': 'A'}], 
'id': '201003', 'paid': True, 'school': 'JGSOM', 'year level': 2}]

输出格式:

{
course_code: [{
"section": section_letter,
"class list": [
id_number_1,
id_number_2,
id_number_3,
]
}
course_code: [{
"section": section_letter,
"class list": [
id_number_1,
id_number_2,
id_number_3,
]
}
]
}

我的代码现在看起来像这样,但这只需要每个课程代码的部分。我还需要在每个课程代码的每个部分下取ID号。有人知道如何通过修改我的代码下面做到这一点吗?

courses = {}
for i in student_data:
for j in i['enlistment']:
if j['course code'] not in courses:
courses[j['course code']] = {'sections':[j['section']]}
else:
if j['section'] not in courses[j['course code']]['sections']:
courses[j['course code']]['sections'].append(j['section']) 
for i in courses.keys():
courses[i]['sections'] = sorted(courses[i]['sections'])

请帮

这是你想要的。你建议的输出格式并不是很有用;每个课程都应该有一个字典,以节ID作为键,以类列表作为值,而不是对象列表。这实际上是我首先构造的,然后转换它:

student_data = [
{'enlistment': [{'course code': 'UK 60', 'section': 'A'},
{'course code': 'GF 24', 'section': 'B'},
{'course code': 'ME 40', 'section': 'B'},
{'course code': 'VY 44', 'section': 'D'},
{'course code': 'EN 94', 'section': 'B'}],
'id': '201001', 'paid': True, 'school': 'SOSE', 'year level': 2},
{'enlistment': [{'course code': 'EQ 61', 'section': 'D'},
{'course code': 'UZ 22', 'section': 'B'},
{'course code': 'KS 36', 'section': 'B'},
{'course code': 'VH 63', 'section': 'A'},
{'course code': 'IW 81', 'section': 'C'}],
'id': '211002', 'paid': True, 'school': 'JGSOM', 'year level': 1},
{'enlistment': [{'course code': 'WE 15', 'section': 'D'},
{'course code': 'ZP 68', 'section': 'A'},
{'course code': 'GI 78', 'section': 'A'},
{'course code': 'GK 72', 'section': 'C'},
{'course code': 'FA 24', 'section': 'D'},
{'course code': 'UJ 28', 'section': 'A'}],
'id': '201003', 'paid': True, 'school': 'JGSOM', 'year level': 2}]
courses = {}
for i in student_data:
for j in i['enlistment']:
if j['course code'] not in courses:
courses[j['course code']] = {}
if j['section'] not in courses[j['course code']]:
courses[j['course code']][j['section']] = []
courses[j['course code']][j['section']].append( i['id'] )
from pprint import pprint
for cc,it in courses.items():
courses[cc] = list( { 'section': k, 'class_list': v } for k,v in it.items() )
pprint(courses)

输出:

{'EN 94': [{'class_list': ['201001'], 'section': 'B'}],
'EQ 61': [{'class_list': ['211002'], 'section': 'D'}],
'FA 24': [{'class_list': ['201003'], 'section': 'D'}],
'GF 24': [{'class_list': ['201001'], 'section': 'B'}],
'GI 78': [{'class_list': ['201003'], 'section': 'A'}],
'GK 72': [{'class_list': ['201003'], 'section': 'C'}],
'IW 81': [{'class_list': ['211002'], 'section': 'C'}],
'KS 36': [{'class_list': ['211002'], 'section': 'B'}],
'ME 40': [{'class_list': ['201001'], 'section': 'B'}],
'UJ 28': [{'class_list': ['201003'], 'section': 'A'}],
'UK 60': [{'class_list': ['201001'], 'section': 'A'}],
'UZ 22': [{'class_list': ['211002'], 'section': 'B'}],
'VH 63': [{'class_list': ['211002'], 'section': 'A'}],
'VY 44': [{'class_list': ['201001'], 'section': 'D'}],
'WE 15': [{'class_list': ['201003'], 'section': 'D'}],
'ZP 68': [{'class_list': ['201003'], 'section': 'A'}]}

相关内容

  • 没有找到相关文章

最新更新