抛砖引玉吧
from operator import itemgetter
li = [
{"姓名":"小明","日期":"2021-03","基本工资":"5100","绩效":"2000","提成":"1000",},
{"姓名":"小明","日期":"2021-02","基本工资":"5300","绩效":"2000","提成":"2000",},
{"姓名":"小明","日期":"2021-01","基本工资":"5000","绩效":"1000","提成":"3000",},
{"姓名":"小强","日期":"2021-04","基本工资":"2100","绩效":"1000","提成":"400",},
{"姓名":"小强","日期":"2021-03","基本工资":"2100","绩效":"1000","提成":"500",},
]
'''
我们需要的结果
res = [
{"薪资类型":"","姓名":,"":,"日期1":"","日期2":"","日期n":"",}
]
'''
#工资类型
res=[]
ts = ("基本工资","绩效","提成")
#员工姓名结合去重
names = set([i["姓名"] for i in li])
dates = set([i["日期"] for i in li])
for t in ts:
temp ={}
for name in names:
temp["薪资类型"] = t
temp["姓名"] = name
temp.update({}.fromkeys(sorted(list(dates))))
for dt in li:
if dt["姓名"] == name:
temp[dt["日期"]] = dt[t]
res.append(temp)
res.sort(key=itemgetter('姓名'))
print(res)