I have a dictionary of dictionaries, which is typed with inscriptions or two, looks like this:
a = {114907: {114905: 1.4351310915,
114908: 0.84635577943,
114861: 61.490648372},
113820: {113826: 8.6999361654,
113819: 1.1412795216,
111068: 1.1964946282,
117066: 1.5595617822,
113822: 1.1958951003},
114908: {114906: 1.279878388,
114907: 0.77568252572,
114862: 2.5412545474}
}
The operation I want to perform is as follows:
For each key a:
- If its value (the innermost dictionary, for example
{114905: 1.435.., 114908: 0.846.., 114861: 61.490..}) contains keys that are present as keys on the outermost (in this case 114908), replace them with the values k, vfrom the latter and completely delete it. - Finally, convert the foreign key into a tuple containing both the source key and the key that was knocked out of the innermost dict.
The desired result will be as follows:
b = {(114907, 114908): {114905: 1.4351310915,
114906: 1.279878388,
114862: 2.5412545474,
114861: 61.490648372},
113820: {113826: 8.6999361654,
113819: 1.1412795216,
111068: 1.1964946282,
117066: 1.5595617822,
113822: 1.1958951003}
}
I really hope that you have what I am trying to achieve here because it is not even described.
, , , , . , , - .
from copy import deepcopy
temp = deepcopy(a)
for item in temp:
for subitems, values in temp[item].items():
if values < 1.0:
for k, v in temp[subitems].items():
if k != item:
a[item][k] = v
for i in a:
print(i, a[i])
, pop value, key: value?
, , , , , - dict. 1.0, dict.