Source: Python Crawler and Data Mining
I. Preface
A few days ago, in Python's strongest king exchange group [鄒鄒鄒ི།།།與歌同行ཉྀ] asked a Python dictionary processing problem, and the screenshot of the question is as follows.
Here is the data of his elements.
a = [
{'time': '8:30-9:30', 'content': 'opening speech', 'speaker': [{'name': 'Li Ming', 'hs': 'Chongqing-affiliated Yongchuan'}]}
{'time': '8:30-9:30', 'content': 'Opening Address', 'speaker': [{'name': 'Chairman:Li Wei', 'hs': 'Suzhou Affiliated Hospital'}]},
{'time': '8:30-9:30', 'content': 'Opening Address', 'speaker': [{'name': 'Wang Bin', 'hs': 'Foshan City Hospital'}]}
]
Want to achieve the desired effect is shown in the following figure.
Second, the implementation process
Here [ning students] provided a code, as follows.
I later gave a code myself, the code is as follows.
a = [
{'time': '8:30-9:30', 'content': 'opening speech', 'speaker': [{'name': 'Li Ming', 'hs': 'Chongqing affiliated Yongchuan'}]}
{'time': '8:30-9:30', 'content': 'Opening Address', 'speaker': [{'name': 'Chairman:Li Wei', 'hs': 'Suzhou Affiliated Hospital'}]},
{'time': '8:30-9:30', 'content': 'Opening Address', 'speaker': [{'name': 'Wang Bin', 'hs': 'Foshan City Hospital'}]}
]
new_dict = {}
new_lst = [] for item in a:
new_dict.setdefault('speaker', []).append(item['speaker']) # print(new_dict) front_dict = {'time': '8:30-9:30', 'content': 'opening speech'} # new_lst. append(a[0][0]) final_dict = {**front_dict, **new_dict} print(final_dict)
There is some redundancy, but it is possible to get the desired effect.
Later [ning students] also used Pandas to show a show, as follows.
Later [next door hawthorn] for the above two code, both made an optimization, the code are as follows.
# This is written as follows from itertools import groupby from operator import itemgetter
[dict(zip(('time', 'content', 'speaker'),
(*key, sum([i['speaker'] for i in value], [])))) for key, value in groupby(a, itemgetter('time', 'content'))])
For the Pandas write, the code is as follows.
# This is written like this import pandas as pd pd.DataFrame(a).groupby(['time', 'content']).speaker.sum().reset_index().to_dict(orient='records')
Simply too show!
III. Summary
Hello everyone, I am Pippi. This article is mainly an inventory of a Python dictionary processing problem, the text gives a specific analysis of the problem and code implementation, to help fans successfully solve the problem.