社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python从另一个字典列表更新字典列表中的值

bayman • 5 年前 • 1196 次点击  

如果给定下面两个词典列表(score_list和update_list),如何从update_list中的词典列表更新score_list?

score_list = [{'id': 1, 'score': 123}, {'id': 2, 'score': 234}, {'id': 3, 'score': 345}]

update_list = [{'id': 1, 'score': 500}, {'id': 3, 'score': 300}]

# return this
score_list = [{'id': 1, 'score': 500}, {'id': 2, 'score': 234}, {'id': 3, 'score': 300}]
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/52115
 
1196 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Reut Sharabani
Reply   •   1 楼
Reut Sharabani    6 年前

我强烈建议在有唯一密钥要匹配时使用映射:

update_mapping = {d['id']: d for d in update_list}
score_list = [update_mapping.get(d['id'], d) for d in score_list]