我试图将 score 的每个值添加到字典名称 i,e score[0] 到 names[0] 等等......
names=[{'id': 1, 'name': 'laptop'}, {'id': 2, 'name': 'box'}, {'id': 3, 'name': 'printer'}]
score = [0.9894376397132874, 0.819094657897949, 0.78116521835327]
输出应该是这样的
names=[{'id': 1, 'name': 'laptop','score':0.98}, {'id': 2, 'name': 'box','score':0.81}, {'id': 3, 'name': 'printer','score':0.78}]
如何实现?提前致谢
最佳答案
我会这样理解:
>>> [{**d, 'score':s} for d, s in zip(names, score)]
[{'id': 1, 'name': 'laptop', 'score': 0.9894376397132874}, {'id': 2, 'name': 'box', 'score': 0.819094657897949}, {'id': 3, 'name': 'printer', 'score': 0.78116521835327}]
关于python - 如何将列表值添加到python中的现有字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61907311/