【问题标题】:Summing values in json objects in Python在 Python 中对 json 对象中的值求和
【发布时间】: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}
]

【问题讨论】:

    标签: python json pandas


    【解决方案1】:

    使用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
    
    # back to list of items
    result = list(result.values())
    

    【讨论】:

      【解决方案2】:

      是的

      任何加载/保存都可以使用 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  # 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)
      

      输出

      [{'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'}]
      

      【讨论】:

      • 不要使用内置方法名sum
      • 是的,谢谢你指出这一点
      猜你喜欢
      • 1970-01-01
      • 2018-04-23
      • 2019-04-19
      • 2020-10-14
      • 2021-11-22
      • 2021-08-09
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多