>>> list_dicts = [{"people": "Bob", "animal": "dog", "frequencies": 2},
... {"people": "Bob", "animal": "cat", "frequencies": 1},
... {"people": "Bob", "animal": "parrot", "frequencies": 1},
... {"people": "Kate", "animal": "dog", "frequencies": 1},
... {"people": "Kate", "animal": "parrot", "frequencies": 1},
... {"people": "Joy", "animal": "cat", "frequencies": 3}]
>>>
>>> temp = defaultdict(list)
>>> for item in list_dicts:
... temp[item["animal"]].append({"name": item["people"], "freq": item["frequencies"]})
...
>>> result = {"title": "frequencies", "children":[
... {"animal": animal, "people": people} for animal, people in temp.items()
... ]}
>>> result
{'title': 'frequencies', 'children': [{'animal': 'dog', 'people': [{'Bob': 2}, {'Kate': 1}]}, {'animal': 'cat', 'people': [{'Bob': 1}, {'Joy': 3}]}, {'animal': 'parrot', 'people': [{'Bob': 1}, {'Kate': 1}]}]}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…