Python 如果键值对相同​​,则字典追加值

Python Dict append value if key value pair are same

我是 python 字典的新手,对向键附加值有疑问。示例 python 字典如下所示。如果键值对、ID 和时间相同,如何附加值?请参阅下面的预期结果。尝试了 append()、pop()、update(),但无法获得预期的结果。感谢任何帮助。


    {
        "Total": [
            {
                "ID": "ID_1000",
                "time": 1000,
                "name": {
                    "first_name": "John",
                    "last_name": "Brown"
                }
            },
            {
                "ID": "ID_5000",
                "time": 5000,
                "name": {
                    "first_name": "Jason",
                    "last_name": "Willams"
                }
            },
            {
                "ID": "ID_5000",
                "time": 5000,
                "name": {
                    "first_name": "Mary",
                    "last_name": "Jones"
                }
            },
            {
                "ID": "ID_1000",
                "time": 1000,
                "name": {
                    "first_name": "Michael",
                    "last_name": "Kol"
                }
            }
        ]
    }

以下是预期结果。


    {
        "Total": [
            {
                "ID": "ID_1000",
                "time": 1000,
                "name": [
                    {
                        "first_name": "John",
                        "last_name": "Brown"
                    },
                    {
                        "first_name": "Michael",
                        "last_name": "Kol"
                    }
                ]
            },
            {
                "ID": "ID_5000",
                "time": 5000,
                "name": [
                    {
                        "first_name": "Jason",
                        "last_name": "Willams"
                    },
                    {
                        "first_name": "Mary",
                        "last_name": "Jones"
                    }
                ]
            }
        ]
    }

一种选择是使用以您的 ID 作为键的中间字典。 请注意,此代码不管理时间数据,因为不清楚您是否要添加或期望什么。

import json

values = {
        "Total": [
            {
                "ID": "ID_1000",
                "time": 1000,
                "name": {
                    "first_name": "John",
                    "last_name": "Brown"
                }
            },
            {
                "ID": "ID_5000",
                "time": 5000,
                "name": {
                    "first_name": "Jason",
                    "last_name": "Willams"
                }
            },
            {
                "ID": "ID_5000",
                "time": 5000,
                "name": {
                    "first_name": "Mary",
                    "last_name": "Jones"
                }
            },
            {
                "ID": "ID_1000",
                "time": 1000,
                "name": {
                    "first_name": "Michael",
                    "last_name": "Kol"
                }
            }
        ]
    }

# first create a dictionary with the ids as keys
consolidated_names = {}
for total_value in values["Total"]:
    id = total_value["ID"]
    if id not in consolidated_names:
        consolidated_names[id] = [total_value["name"]]
    else:
        consolidated_names[id].append(total_value["name"])

# then create the structure that you want
processed_values = []
for id in consolidated_names:
    processed_values.append({"ID": id, "name": consolidated_names[id]})


print(json.dumps({"Total": processed_values}, indent=4))

结果:

{
    "Total": [
        {
            "ID": "ID_1000",
            "name": [
                {
                    "first_name": "John",
                    "last_name": "Brown"
                },
                {
                    "first_name": "Michael",
                    "last_name": "Kol"
                }
            ]
        },
        {
            "ID": "ID_5000",
            "name": [
                {
                    "first_name": "Jason",
                    "last_name": "Willams"
                },
                {
                    "first_name": "Mary",
                    "last_name": "Jones"
                }
            ]
        }
    ]
}