名单借助词典的列表,如下列:如何合并字典
user_course_score = [
{'course_id': 1456, 'score': 56},
{'course_id': 316, 'score': 71}
]
courses = [
{'course_id': 1456, 'name': 'History'},
{'course_id': 316, 'name': 'Science'},
{'course_id': 926, 'name': 'Geography'}
]
是什么将它们组合成字典以下列表中的最佳方式:
user_course_information = [
{'course_id': 1456, 'score': 56, 'name': 'History'},
{'course_id': 316, 'score': 71, 'name': 'Science'},
{'course_id': 926, 'name': 'Geography'} # Note: the student did not take this test
]
或者将它以更好地存储数据不同,如:
courses = {
'1456': 'History',
'316': 'Science',
'926': 'Geography'
}
感谢您的帮助。
我已经接受了亚当的答案,因为它不正是我需要。其他答案 - 建议我将数据重组为字典 - 工作正常,但对于我的项目,我宁愿不使用ID作为键。 – Chris 2010-08-06 09:48:52