英文:
Summing values in json objects in Python
问题
这是两个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 #累加},
{"text":"watercolour","id":"x33202 ","obj_count": 771 #累加},
{"text":"ink","id":"AAT15012 ","obj_count":297},
{"text":"pencil","id":"AAT16013 ","obj_count":297}
]
英文:
I have two JSON objects. I want to merge them but wherever the keys are the same the field obj_count should be summed. Is there any way around it in python?
Here is an example of it:
This is the 1st JSON object
[
{"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}
]
And here is the second json object
[
{"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}
]
What I want is something like this:
[
{"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}
]
答案1
得分: 3
使用一个dict
来存储是否已经见过一个id
- 如果已经见过,将它们的
obj_count
相加 - 如果没有见过,只需保存该项
values_a = [
{"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}
]
values_b = [
{"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}
]
result = {}
for item in [*values_a, *values_b]:
if item['id'] in result:
result[item['id']]['obj_count'] += item['obj_count']
else:
result[item['id']] = item
# 转回项目列表
result = list(result.values())
英文:
Use a dict
to store whether you have seen an id
or not
- if you have, sum their
obj_count
- if you haven't, just save the item
<!-- -->
values_a = [
{"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}
]
values_b = [
{"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}
]
result = {}
for item in [*values_a, *values_b]:
if item['id'] in result:
result[item['id']]['obj_count'] += item['obj_count']
else:
result[item['id']] = item
# back to list of items
result = list(result.values())
答案2
得分: 1
是的
可以使用json模块来进行加载和保存(以下代码未使用)
def sum_list_of_dict(source, add):
for add_elem in add:
found = False
for source_elem in source:
if add_elem["id"] == source_elem["id"]:
source_elem["obj_count"] += add_elem["obj_count"]
found = True
break # 不应该存在重复项
if not found:
source.append(add_elem)
return source
data1 = [
{"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},
]
data2 = [
{"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},
]
data3 = sum_list_of_dict(data1, data2)
# 仅用于美观打印
from pprint import pprint
pprint(data3)
输出
[{'id': 'x32505', 'obj_count': 2662, 'text': 'pen and ink and watercolour'},
{'id': 'x33202', 'obj_count': 771, 'text': 'watercolour'},
{'id': 'AAT16013', 'obj_count': 297, 'text': 'pencil'},
{'id': 'AAT15012', 'obj_count': 297, 'text': 'ink'}]
英文:
Yes
Any loading/saving can be done with the json module (not used below though)
def sum_list_of_dict(source, add):
for add_elem in add:
found = False
for source_elem in source:
if add_elem["id"] == source_elem["id"]:
source_elem["obj_count"] += add_elem["obj_count"]
found = True
break # dupes should not be present
if not found:
source.append(add_elem)
return source
data1 = [
{"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},
]
data2 = [
{"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},
]
data3 = sum_list_of_dict(data1, data2)
# just for pretty printing
from pprint import pprint
pprint(data3)
output
[{'id': 'x32505', 'obj_count': 2662, 'text': 'pen and ink and watercolour'},
{'id': 'x33202', 'obj_count': 771, 'text': 'watercolour'},
{'id': 'AAT16013', 'obj_count': 297, 'text': 'pencil'},
{'id': 'AAT15012', 'obj_count': 297, 'text': 'ink'}]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论