Michael Tan
Michael Tan

Reputation: 81

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.

Upvotes: 1

Views: 108

Answers (2)

Alec
Alec

Reputation: 1469

This could do the trick:

d4 = {}
for k, v in d2.items():
  d4[k] = {}
  for k2, v2 in v.items():
    try:
      if v2 not in d4[k]:
        d4[k][v2] = [k2 + ' = ' + d3[k][k2]]
      else:
        d4[k][v2].append(k2 + ' = ' + d3[k][k2])
    except KeyError:
        # this means k2 is a typo in d2; skip
        assert k in d3 # be sure that a missing name isn't causing the KeyError
        # you only need to use pass when you don't have any other operations in the scope

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

print(d4 == d123_new)
# -> True

Upvotes: 1

dmitryro
dmitryro

Reputation: 3506

Here's how you can do this:

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'}}

d123_new = {} # create an empty dict

for key in d1: # run over first dict and get the keys containing names

    try:   # Case of Skill

        d = {
             key:{ # we know the key and Agility case 
                  d2[key]['Agility']:['Agility='+d3[key]['Agility']],
                  d2[key]['Int']: ['Int='+d3[key]['Agility']],
                  'Skill':d1[key]['Skill'],'Magic':d1[key]['Magic']
             }
       }

   except KeyError: # case of Strength

       d = {
             key:{ # no Agility - Strength
                  d2[key]['Strength']:['Strength='+d3[key]['Strength'],
                  'Speed='+d3[key]['Speed']],
                  d2[key]['Int']: ['Int='+d3[key]['Strength']],
                 'Skill':d1[key]['Skill'],'Magic':d1[key]['Magic']
             }
        }

    d123_new.update(d)

print d123_new # output the result

Upvotes: 1

Related Questions