You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python合并字典列表,按module去重并聚合其余字段为列表?

Python字典列表按module分组聚合去重实现

核心逻辑

  • 以module作为唯一分组键
  • 用集合自动对product_range、receipt_category字段的值去重
  • 所有聚合结果统一用列表格式存储,单元素也保留列表结构
  • 支持直接输出标准JSON格式

可运行代码

import json

# 原始数据
raw_data = [
    {
        "module": "RECEIPT BISCUITS",
        "product_range": "ULKER BISCUITS",
        "receipt_category": "BISCUITS"
    },
    {
        "module": "RECEIPT BISCUITS",
        "product_range": "ULKER",
        "receipt_category": "BISCUITS"
    },
    {
        "module": "RECEIPT BISCUITS",
        "product_range": "ULKER BISCUITS GOLD",
        "receipt_category": "BISCUITS GOLD"
    },
    {
        "module": "RECEIPT COFFEE",
        "product_range": "BLACK GOLD",
        "receipt_category": "BLACK GOLD"
    }
]

# 分组存储中间结果,用集合自动去重
group_cache = {}
for item in raw_data:
    module_key = item["module"]
    # 首次遇到该module时初始化缓存
    if module_key not in group_cache:
        group_cache[module_key] = {
            "product_range": set(),
            "receipt_category": set()
        }
    # 写入对应字段值
    group_cache[module_key]["product_range"].add(item["product_range"])
    group_cache[module_key]["receipt_category"].add(item["receipt_category"])

# 组装成目标结构
result = []
for module, values in group_cache.items():
    result.append({
        "module": module,
        "product_range": list(values["product_range"]),
        "receipt_category": list(values["receipt_category"])
    })

# 输出JSON格式结果
print(json.dumps(result, ensure_ascii=False, indent=4))

# 如果需要保存为JSON文件,取消注释下方代码
# with open("聚合结果.json", "w", encoding="utf-8") as f:
#     json.dump(result, f, ensure_ascii=False, indent=4)

运行输出

[
    {
        "module": "RECEIPT BISCUITS",
        "product_range": [
            "ULKER BISCUITS",
            "ULKER",
            "ULKER BISCUITS GOLD"
        ],
        "receipt_category": [
            "BISCUITS",
            "BISCUITS GOLD"
        ]
    },
    {
        "module": "RECEIPT COFFEE",
        "product_range": [
            "BLACK GOLD"
        ],
        "receipt_category": [
            "BLACK GOLD"
        ]
    }
]

内容的提问来源于stack exchange,提问作者ulkerbiscuits

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 11:54:00