python 字典添加键值对 键相同值被覆盖的问题
数据
data = {'2021-02':
{
'应收账款': {'this_month_cumulative_target': '2154.00', 'same_month_cumulative_target': '21245.00'},
'存货': {'this_month_cumulative_target': '4308.00', 'same_month_cumulative_target': '42490.00'},
'\xa0\xa0其中:产成品': {'this_month_cumulative_target': '6462.00',
'same_month_cumulative_target': '63735.00'},
'流动资产合计': {'this_month_cumulative_target': '8616.00', 'same_month_cumulative_target': '84980.00'},
'主营业务税金及附加': {'this_month_cumulative_target': '10770.00',
'same_month_cumulative_target': '106225.00'},
'营业费用': {'this_month_cumulative_target': '12924.00', 'same_month_cumulative_target': '127470.00'},
'管理费用': {'this_month_cumulative_target': '15078.00', 'same_month_cumulative_target': '148715.00'},
'应交增值税': {'this_month_cumulative_target': '17232.00',
'same_month_cumulative_target': '169960.00'},
'资产合计': {'this_month_cumulative_target': '19386.00', 'same_month_cumulative_target': '191205.00'},
'负债合计': {'this_month_cumulative_target': '21540.00', 'same_month_cumulative_target': '212450.00'},
'主营业务收入': {'this_month_cumulative_target': '23694.00',
'same_month_cumulative_target': '233695.00'},
'主营业务成本': {'this_month_cumulative_target': '25848.00',
'same_month_cumulative_target': '254940.00'},
'财务费用': {'this_month_cumulative_target': '28002.00', 'same_month_cumulative_target': '276185.00'},
'\xa0\xa0其中:利息支出': {'this_month_cumulative_target': '30156.00',
'same_month_cumulative_target': '297430.00'},
'利润总额': {'this_month_cumulative_target': '32310.00', 'same_month_cumulative_target': '318675.00'},
'全部从业人员平均人数': {'this_month_cumulative_target': '33666.00',
'same_month_cumulative_target': '320133.00'}}, '2021-01':
{
'应收账款': {'this_month_cumulative_target': '34777.00', 'same_month_cumulative_target': '341378.00'},
'存货': {'this_month_cumulative_target': '36931.00', 'same_month_cumulative_target': '362623.00'},
'\xa0\xa0其中:产成品': {'this_month_cumulative_target': '39085.00',
'same_month_cumulative_target': '383868.00'},
'流动资产合计': {'this_month_cumulative_target': '41239.00',
'same_month_cumulative_target': '405113.00'},
'主营业务税金及附加': {'this_month_cumulative_target': '43393.00',
'same_month_cumulative_target': '426358.00'},
'营业费用': {'this_month_cumulative_target': '45547.00', 'same_month_cumulative_target': '447603.00'},
'管理费用': {'this_month_cumulative_target': '47701.00', 'same_month_cumulative_target': '468848.00'},
'应交增值税': {'this_month_cumulative_target': '49855.00',
'same_month_cumulative_target': '490093.00'},
'资产合计': {'this_month_cumulative_target': '52009.00', 'same_month_cumulative_target': '511338.00'},
'负债合计': {'this_month_cumulative_target': '54163.00', 'same_month_cumulative_target': '532583.00'},
'主营业务收入': {'this_month_cumulative_target': '56317.00',
'same_month_cumulative_target': '553828.00'},
'主营业务成本': {'this_month_cumulative_target': '58471.00',
'same_month_cumulative_target': '575073.00'},
'财务费用': {'this_month_cumulative_target': '60625.00', 'same_month_cumulative_target': '596318.00'},
'\xa0\xa0其中:利息支出': {'this_month_cumulative_target': '62779.00',
'same_month_cumulative_target': '617563.00'},
'利润总额': {'this_month_cumulative_target': '64933.00', 'same_month_cumulative_target': '638808.00'},
'全部从业人员平均人数': {'this_month_cumulative_target': '66289.00',
'same_month_cumulative_target': '640266.00'}}, '2020-12': {}, '2020-11': {}, '2020-10': {},
'2020-09': {}, '2020-08': {}, '2020-07': {}, '2020-06': {}, '2020-05': {}, '2020-04': {}, '2020-03': {},
'2020-02': {}}
data2 = [{'应收账款': '-32623.00', '存货': '-32623.00', '\xa0\xa0其中:产成品': '-32623.00',
'流动资产合计': '-32623.00', '主营业务税金及附加': '-32623.00', '营业费用': '-32623.00',
'管理费用': '-32623.00', '应交增值税': '-32623.00', '资产合计': '-32623.00',
'负债合计': '-32623.00', '主营业务收入': '-32623.00', '主营业务成本': '-32623.00',
'财务费用': '-32623.00', '\xa0\xa0其中:利息支出': '-32623.00', '利润总额': '-32623.00',
'全部从业人员平均人数': '-32623.00'},
{'应收账款': '34777.00', '存货': '36931.00', '\xa0\xa0其中:产成品': '39085.00',
'流动资产合计': '41239.00', '主营业务税金及附加': '43393.00', '营业费用': '45547.00',
'管理费用': '47701.00', '应交增值税': '49855.00', '资产合计': '52009.00',
'负债合计': '54163.00', '主营业务收入': '56317.00', '主营业务成本': '58471.00',
'财务费用': '60625.00', '\xa0\xa0其中:利息支出': '62779.00', '利润总额': '64933.00',
'全部从业人员平均人数': '66289.00'}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
需求就是data2的值添加到data中,键自定义 值追加
解决方法 直接上代码
方式一
keys = list(data.keys())
for index, res in enumerate(data2):
for k, v in res.items():
if data[keys[index]] and data[keys[index]].get(k):
data[keys[index]][k].update({'this': v})
print(data)
方式二
keys = list(data.keys())
for index, res in enumerate(data2):
if index > len(keys):
brack
for k, v in res.items():
data_key = keys[index]
if data_key and data[data_key] and data[data_key].get(k):
data[data_key][k].update({'this': v})
print(data)
方式三
这种是针对 相同的地方 数据我也放上去
data = {'2021-02':
{'应收账款': {'this_month_cumulative_target': '2154.00', 'same_month_cumulative_target': '21245.00', },
"存货": {'this_month_cumulative_target': '787.00', 'same_month_cumulative_target': '234.00', }},
'2021-01': {
'应收账款': {'this_month_cumulative_target': '34777.00', 'same_month_cumulative_target': '341378.00', },
"存货": {'this_month_cumulative_target': '123.00', 'same_month_cumulative_target': '987.00', }}
}
data2 = [
{'应收账款': '2021-02应收账款', '存货': '2021-02存货'},
{'应收账款': '2021-01应收账款', '存货': '2021-01存货'}
]
for res in data2:
for k, v in res.items():
date_str = v[:7]
if data.get(date_str) and data[date_str].get(k):
data[date_str][k].update({'this': v})
print(data)