源码

python – 如何合并字典列表


使用以下字典列表:

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'
}

谢谢你的帮助.

(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/python-ruhehebingzidianliebiao.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9 月 26, 2019 at 01:33 下午

热评文章