用Python构建我的词典

时间:2017-12-28 09:05:08

标签: python json dictionary

我正在尝试用Python构建我的字典。输入列表如下所示:

[
    {
        "title": "Test A",
        "date": "28.12.17",
        "name": "Person 1"
    },
    {
        "title": "Test B",
        "date": "28.12.17",
        "name": "Person 2"
    },
    {
        "title": "Test C",
        "date": "28.12.17",
        "name": "Person 1"
    },
    {
        "title": "Test D",
        "date": "28.12.17",
        "name": "Person 2"
    },
    {
        "title": "Test E",
        "date": "29.12.17",
        "name": "Person 1"
    },
    {
        "title": "Test F",
        "date": "29.12.17",
        "name": "Person 2"
    },
    {
        "title": "Test G",
        "date": "29.12.17",
        "name": "Person 1"
    },
    {
        "title": "Test H",
        "date": "29.12.17",
        "name": "Person 2"
    }
]

现在我想生成一个如下所示的输出:

[
    {
        "entries": [
            {
                "entries": [
                    {
                        "title": "Test A",
                        "date": "28.12.17",
                        "name": "Person 1"
                    },
                    {
                        "title": "Test B",
                        "date": "28.12.17",
                        "name": "Person 1"
                    }
                ],
                "name": "Person 1"
            },
            {
                "entries": [
                    {
                        "title": "Test C",
                        "date": "28.12.17",
                        "name": "Person 2"
                    },
                    {
                        "title": "Test D",
                        "date": "28.12.17",
                        "name": "Person 2"
                    }
                ],
                "name": "Person 2"
            }
        ],
        "date": "28.12.17"
    },
    {
        "entries": [
            {
                "entries": [
                    {
                        "title": "Test E",
                        "date": "29.12.17",
                        "name": "Person 1"
                    },
                    {
                        "title": "Test E",
                        "date": "29.12.17",
                        "name": "Person 1"
                    }
                ],
                "name": "Person 1"
            },
            {
                "entries": [
                    {
                        "title": "Test F",
                        "date": "29.12.17",
                        "name": "Person 2"
                    },
                    {
                        "title": "Test G",
                        "date": "29.12.17",
                        "name": "Person 2"
                    }
                ],
                "name": "Person 2"
            }
        ],
        "date": "29.12.17"
    }
]

我怎样才能用Python做到这一点?感谢您提前给出答案。

1 个答案:

答案 0 :(得分:0)

试试这个:

# Suppose data holds your input
out = []
for date in set([d['date'] for d in data]):
    d_by_date = {"date": date, "entries":[]}
    for person in set([d['name'] for d in data]):
        p_entries = {}
        p_list = []
        for d in data:
            if d["name"] == person and d["date"] == date:
                p_list.append(d)
        p_entries["entries"] = p_list
        p_entries["name"] = person
        d_by_date["entries"].append(p_entries)
    out.append(d_by_date)