小编Eya*_*l.D的帖子

在 Python 中按可重复键对 JSON 字典数组进行分组和排序

我有一个 json,它是一个字典列表,如下所示:

我使用 pymysql 从 MySQL 获取它

[{
    "id": "123",
    "name": "test",
    "group": "test_group"
},
{
    "id": "123",
    "name": "test",
    "group": "test2_group"
},
{
    "id": "456",
    "name": "test2",
    "group": "test_group2"
},
{
    "id": "456",
    "name": "test2",
    "group": "test_group3"
}]
Run Code Online (Sandbox Code Playgroud)

我需要对其进行分组,以便每个“名称”只有一个字典,并且它将包含该名称下所有组的列表。像这样的东西:

[{
    "id": "123",
    "name": "test",
    "group": ["test2_group", "test_group"]
},
{
    "id": "456",
    "name": "test2",
    "group": ["test_group2", "test_group3"]
}]
Run Code Online (Sandbox Code Playgroud)

我想得到一些帮助,谢谢!

python arrays json python-3.x

3
推荐指数
1
解决办法
1870
查看次数

标签 统计

arrays ×1

json ×1

python ×1

python-3.x ×1