python 处理字典键值合并

import pandas as pd
import numpy as np
dataqq = [{'name':'bbbb','time':'021'},{'name':'bbbb','time':'201'},{'name':'aaaa','time':'2021'},{'name':"aaaa",'time':'2020'}]
data = pd.DataFrame(dataqq)

data1 = np.array(data.groupby(['name']))#按照id进行分类
# 转变成array对象之后,可以根据data1[0][1]查看其结构]
all_value = []
for j in range(len(data1)):
    value = []
    for i in data1[j][1]['time']:
        value.append(i)
    all_value.append({'name':data1[j][0]})
    all_value.append({'time': value})
print(all_value)

执行结果:

[{'name': 'aaaa'}, {'time': ['2021', '2020']}, {'name': 'bbbb'}, {'time': ['021', '201']}]

 

上一篇:A记录、CNAME记录、MX记录、AAAA记录、SOA记录


下一篇:Redis实现排行榜