Tan Wen Yan Tan Wen Yan - 5 months ago 7
Python Question

Python: Nested dictionaries: getting string(key+value) as values then swap with keys, combining dictionaries

Say I have this 3 dictionaries:

d1 = {'Ben': {'Skill': 'true', 'Magic': 'false'}, 'Tom': {'Skill': 'true', 'Magic': 'true'}}
d2 = {'Ben': {'Strength': 'wo_mana', 'Int': 'wi_mana', 'Speed': 'wo_mana'}, 'Tom': {'Int': 'wi_mana', 'Agility': 'wo_mana'}}
d3 = {'Ben': {'Strength': '1.10', 'Int': '1.20', 'Speed': '1.50'}, 'Tom': {'Int': '1.40', 'Agility': '1.60'}}


I want to get something like this:

d123_new = {'Ben': {'wo_mana': ['Strength = 1.10', 'Speed = 1.50'], 'wi_mana': ['Int = 1.20'], 'Skill': 'true', 'Magic': 'false'},
'Tom': {'wi_mana': ['Int = 1.40'], 'wo_mana': ['Agility = 1.60'], 'Skill': 'true', 'Magic': 'true'}}


which I think it is easier (I could be wrong) for me to print in a table format like this:

Name Skill Magic wo_mana wi_mana
Ben true false Strength = 1.10 Int = 1.20
Speed = 1.50
Tom true true Agility = 1.60 Int = 1.40


So I think I have to combine/append the
d1
and
d2
first, and this is my code and result (
d12
) for this part:

import itertools, collections

def update(d, u):
for k, v in u.iteritems():
if isinstance(v, collections.Mapping):
r = update(d.get(k, {}), v)
d[k] = r
else:
d[k] = u[k]
return d
d12 = update(d2, d1)
print(d12)

d12 = {'Ben': {'Skill': 'true', 'Magic': 'false', 'Strength': 'wo_mana', 'Int': 'wi_mana', 'Speed': 'wo_mana'},
'Tom': {'Skill': 'true', 'Magic': 'true', 'Int': 'wi_mana', 'Agility': 'wo_mana'}}


Then, I want to get
d3_new
which is something like this:

d3_new = {'Ben': ['Strength = 1.10', 'Int = 1.20', 'Speed = 1.50'],
'Tom': ['Int = 1.40', 'Agility = 1.60']}


my code and result(
d3_new
) for this part:

d3_new = {}
for i in d3;
a = ''.join(['%s = %s' %(k,v) for k,v in d3[i].iteritems()])
d3_new[i] = list()
d3_new[i].append(a)
print(d3_new)

d3_new = {'Ben': ['Int = 1.20Strength = 1.10Speed = 1.50'],
'Tom': ['Int = 1.40Agility = 1.60']}


Obviously, my desired output and the output that I get for
d3_new
is different. How should I fix this?

What is my next step after I got the desired output for
d3_new
? Or should I follow these steps in the first place:

a) change
d3
to
d3_new


b) merge
d2
and
d3_new
to

d23 = {'Ben': {'wo_mana': ['Strength = 1.10', 'Speed = 1.50'], 'wi_mana': ['Int = 1.20']},
'Tom': {'wi_mana': ['Int = 1.40'], 'wo_mana': ['Agility = 1.60']}}


c) combine
d1
and
d23
using the append method I did for
d12


How should I do for
b)
?

Please advice me if
d123_new
is not suitable to print the data in my desired table format.

Answer

This could do the trick:

d4 = {}
for k, v in d2.items():
  d4[k] = {}
  for k2, v2 in v.items():
    if v2 not in d4[k]:
      d4[k][v2] = [k2 + ' = ' + d3[k][k2]]
    else:
      d4[k][v2].append(k2 + ' = ' + d3[k][k2])

for k, v in d1.items():
  for k2, v2 in v.items():
    d4[k][k2] = v2

print(d4 == d123_new)
# -> True
Comments