【发布时间】:2023-02-09 18:19:00
【问题描述】:
我有两个 JSON 对象。我想合并它们,但是只要键相同,就应该对字段 obj_count 求和。在 python 中有什么办法解决它吗?
这是一个例子: 这是第一个 JSON 对象
[
{"text": " pen and ink and watercolour", "id": "x32505 ", "obj_count": 1855},
{"text": " watercolour", "id": "x33202 ", "obj_count": 674},
{"text": "pencil", "id": "AAT16013 ", "obj_count": 297}
]
这是第二个 json 对象
[
{"text": " pen and ink and watercolour", "id": "x32505 ", "obj_count": 807},
{"text": " watercolour", "id": "x33202 ", "obj_count": 97},
{"text": " ink", "id": "AAT15012 ", "obj_count": 297}
]
我想要的是这样的:
[
{"text":" pen and ink and watercolour","id":"x32505 ","obj_count": 2662 #summed},
{"text":" watercolour","id":"x33202 ","obj_count": 771 #summed},
{"text":" ink","id":"AAT15012 ","obj_count":297},
{"text":"pencil","id":"AAT16013 ","obj_count":297}
]
【问题讨论】: