• 全部
  • 互动交流
  • 文章分享

python数据格式处理问题

nebuung 2021-03-02 04:56:54

show_data = (('2019', '01', '1,601.0'), ('2019', '02', '1,397.0'), ('2019', '03', '2,219.0'), ('2019', '04', '3,229.5'),
('2020', '01', '7,060.0'), ('2020', '02', '3,500.5'), ('2020', '03', '5,031.0'), ('2020', '04', '3,623.0'))

求大神指导怎么变成下面的格式

show_data_list = [
{'year': '2019',
'sales': [{'month': '01', 'amount': '1,601.0'}, {'month': '02', 'amount': '1,397.0'},
{'month': '03', 'amount': '2,219.0'}, {'month': '04', 'amount': '3,229.5'}]
},
{'year': '2020',
'sales': [{'month': '01', 'amount': '7,060.0'}, {'month': '02', 'amount': '3,500.5'},
{'month': '03', 'amount': '5,031.0'}, {'month': '04', 'amount': '3,623.0'}]
}
]

...全文
108 点赞 收藏 5
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
nebuung 03-04
加分结帖·
回复
import more_itertools as mi

show_data = (('2019', '01', '1,601.0'), ('2019', '02', '1,397.0'), ('2019', '03', '2,219.0'), ('2019', '04', '3,229.5'),
             ('2020', '01', '7,060.0'), ('2020', '02', '3,500.5'), ('2020', '03', '5,031.0'), ('2020', '04', '3,623.0'))

keyfun = lambda x: x[0]
valfun = lambda x: (x[1], x[2])

re = mi.groupby_transform(show_data, keyfun, valfun)
result = [{'year': i,'sales': [{'month': k[0],'amount': k[1],} for k in j]} for i, j in re]

print(result)
回复
nebuung 03-03
引用 1 楼 陈年椰子 的回复:
简单的方法就是遍历



show_data = (('2019', '01', '1,601.0'), ('2019', '02', '1,397.0'), ('2019', '03', '2,219.0'), ('2019', '04', '3,229.5'),
('2020', '01', '7,060.0'), ('2020', '02', '3,500.5'), ('2020', '03', '5,031.0'), ('2020', '04', '3,623.0'))


show_data_list = []
show_data_dict = {}
for data in show_data:
if data[0] in show_data_dict:
data_temp = show_data_dict[data[0]]
data_temp[data[1]] = data[2]
else:
show_data_dict[data[0]] = {data[1]:data[2]}


for data in show_data_dict:
data_list_temp = []
for data1 in show_data_dict[data]:
data_list_temp.append({'month': data1, 'amount': show_data_dict[data][data1]})
show_data_list.append({'year': data,'sales': data_list_temp})


for data in show_data_list:
print(data)




谢谢,又学习了一招
回复
DuskCrow_ 03-03
来个笨办法: show_data = (('2019', '01', '1,601.0'), ('2019', '02', '1,397.0'), ('2019', '03', '2,219.0'), ('2019', '04', '3,229.5'), ('2020', '01', '7,060.0'), ('2020', '02', '3,500.5'), ('2020', '03', '5,031.0'), ('2020', '04', '3,623.0')) show_list = [] for y in set([y[0] for y in show_data]): show_dict = dict.fromkeys(('year','salse')) show_dict['year'] = y data_year = [year_data for year_data in show_data if year_data[0] == y] show_dict['salse'] = [{'month':data[1],'amount':data[2]} for data in data_year] show_list.append(show_dict) print(show_list)
回复
简单的方法就是遍历


show_data = (('2019', '01', '1,601.0'), ('2019', '02', '1,397.0'), ('2019', '03', '2,219.0'), ('2019', '04', '3,229.5'),
             ('2020', '01', '7,060.0'), ('2020', '02', '3,500.5'), ('2020', '03', '5,031.0'), ('2020', '04', '3,623.0'))


show_data_list = []
show_data_dict = {}
for data in show_data:
    if data[0] in show_data_dict:
        data_temp = show_data_dict[data[0]]
        data_temp[data[1]] = data[2]
    else:
        show_data_dict[data[0]] = {data[1]:data[2]}


for data in show_data_dict:
    data_list_temp = []
    for data1 in show_data_dict[data]:
        data_list_temp.append({'month': data1, 'amount': show_data_dict[data][data1]})
    show_data_list.append({'year': data,'sales': data_list_temp})


for data in show_data_list:
    print(data)


回复
相关推荐
发帖
脚本语言
创建于2007-08-27

3.7w+

社区成员

JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
申请成为版主
帖子事件
创建了帖子
2021-03-02 04:56
社区公告

CSDN 脚本语言社区接受专栏投稿(专栏会在顶部创建专属你的栏目),投稿需满足以下要求:

  • 脚本语言技术相关;
  • 文章持续更新,保持活跃;
  • 内容清晰明了,干货为主;
  • 文章排版有序,有条有理。

本社区开通招聘专栏,发布招聘信息请联系版主,发布者需要保证招聘信息真实有效,CSDN 平台和版主不对招聘内容负责!

联系方式:私聊版主、发送邮件、QQ联系等均可: