我有一个字典列表,需要使用另一个字典列表中的信息进行更新。我当前的解决方案(如下)的工作方式是从第一个列表中取出每个词典,然后将其与第二个列表中的每个词典进行比较。它可以工作,但是有没有更快,更优雅的方法来达到相同的结果呢?
a = [ { "id": 1, "score":200 }, { "id": 2, "score":300 }, { "id":3, "score":400 } ]
b = [ { "id": 1, "newscore":500 }, { "id": 2, "newscore":600 } ]
# update a with data from b
for item in a:
for replacement in b:
if item["id"]==replacement["id"]:
item.update({"score": replacement["newscore"]})
有只小跳蛙
梵蒂冈之花
扬帆大鱼
相关分类