У меня возникла странная проблема со словарем. У меня есть 2 словаря source_dict и target_dict. source_dict формируется с собственным логом c. Я пытаюсь получить target_dict с помощью source_dict. Ниже мой код.
source_dict:
{
"t1": {
"t1c": 92074660,
"t1p": 92074660,
"t1l": 0,
"s1": {
"s1c": 1558475,
"s1p": 1558475,
"s1l": 0,
"s1t1": {
"s1t1c": 1558475,
"s1t1p": 1558475,
"s1t1l": 0
}
},
"s2": {
"s2c": 4439058,
"s2p": 4439058,
"s2l": 0,
"s2t1": {
"s2t1c": 83946,
"s2t1p": 83946,
"s2t1l": 0
},
"s2t2": {
"s2t2c": 4355112,
"s2t2p": 4355112,
"s2t2l": 0
}
}
}
}
ожидаемый target_dict:
{
"c": 5341515,
"p": 5341515,
"l": 0,
"s1": {
"s1c": 5341515,
"s1p": 5341515,
"s1l": 0,
"s1t1": {
"s1t1c": 5341515,
"s1t1p": 5341515,
"s1t1l": 0
}
}
}
Arithmeti c Расчет:
c = source_dict(t1.t1c + t2.t2c + t3.t3c)
s1c = source_dict(t1.s1.s1c + t2.s1.s1c + t3.s1.s1c)
My Logi c:
for k, v in source_dict.items():
for s, offset in v.items():
if isinstance(offset, dict):
for tpc, val in offset.items():
if 'c' not in tpc and 'p' not in tpc and 'l' not in tpc:
if tpc not in target_dict[s].keys():
target_dict[s][tpc] = val
else:
target_dict[s][tpc]['c'] = target_dict[s][tpc]['c'] + val['c']
target_dict[s][tpc]['p'] = target_dict[s][tpc]['p'] + val['p']
target_dict[s][tpc]['l'] = target_dict[s][tpc]['l'] + val['l']
Проблема: С этим logi c также обновляются значения в source_dict. Могу ли я получить некоторую помощь в понимании того, что именно происходит не так, что заставляет source_dict обновляться с новыми вычисленными значениями?