Show_data=https://bbs.csdn.net/topics/((' 2019 ', '01', '1601.0 versus'), (' 2019', '02', '1397.0 versus'), (' 2019', '3', '2219.0 versus'), (' 2019', '4', ', 3229 '),
(' 2020 ', '01', '7060.0 versus'), (' 2020', '02', ', 3500 '), (' 2020 ', '3', '5031.0 versus'), (' 2020', '4', '3623.0 versus'))
Pray god to guide how to become a
the following format
Show_data_list=[
{' year ':' 2019 ',
'sales' : [{' month' : '01', 'amount' : '1601.0 versus'}, {' month' : '02', 'amount' : '1397.0 versus'},
{' month ', '3', 'amount' : '2219.0 versus'}, {' month' : '04,' amount ':', 3229 '}]
},
{' year ':' 2020 ',
'sales' : [{' month' : '01', 'amount' : '7060.0 versus'}, {' month' : '02', 'amount' : ', 3500 '},
{' month ', '3', 'amount' : '5031.0 versus'}, {' month' : '04,' amount ':' 3623.0 versus'}]
}
]
CodePudding user response:
Simple method is to traverse the
Show_data=https://bbs.csdn.net/topics/((' 2019 ', '01', '1601.0 versus'), (' 2019', '02', '1397.0 versus'), (' 2019', '3', '2219.0 versus'), (' 2019', '4', ', 3229 '),
(' 2020 ', '01', '7060.0 versus'), (' 2020', '02', ', 3500 '), (' 2020 ', '3', '5031.0 versus'), (' 2020', '4', '3623.0 versus'))
Show_data_list=[]
Show_data_dict={}
For the data in show_data:
If the data [0] in show_data_dict:
Data_temp=show_data_dict [data [0]]
Data_temp [data [1]]=data [2]
The else:
Show_data_dict [data [0]]={data [1] : data [2]}
For the data in show_data_dict:
Data_list_temp=[]
For data1 show_data_dict in [the data] :
Data_list_temp. Append ({' month ': data1,' amount ': show_data_dict [data] [data1]})
Show_data_list. Append ({' year ': data,' sales' : data_list_temp})
For the data in show_data_list:
Print (data)