Michael Tan
Michael Tan

Reputation: 81

How to map/assign/replace values of one dictionary to another dictionary?

Say I have the following 3 dictionaries:

General case:

d1 = {'c_choice1': '1.10', 'c_choice2': '1.20',...}
d2 = {'John': {'strength': 'choice1', 'agility': 'choice2',...},...}
d3 = {'John': {'choice1': 'c_choice1', 'choice2': 'c_choice2',...},...}

d2 and d3 can be:

d2 = {'John': {'strength': 'choice1', 'agility': 'choice2'}, 'Kevin': {'int': 'choice1'}}
d3 = {'John': {'choice1': 'c_choice1', 'choice2': 'c_choice2'}, 'Kevin': {'choice1': 'c_choice1'}}

or

d2 = {'John': {'strength': 'choice1', 'agility': 'choice1'}}
d3 = {'John': {'choice1': 'c_choice1'}}

What I want to do (using general case as example):

a) from d3, let choice1 = c_choice1, choice2 = c_choice2, etc.

choice1 = c_choice1
choice2 = c_choice2

b) lookup d1 and find the value for c_choice1, c_choice2, etc.

c_choice1 = 1.10
c_choice2 = 1.20

c) create a final dictionary with choice1, choice2, etc in d2 replaced with the value in d1

My desired output (using general case as example):

d4 = {'John': {'strength': '1.10', 'agility': '1.20',...}...}

Note:

a) the 3 dictionaries are created when extracting the data from a input file, the keys and values are not defined by myself, hence I do not know the order of the keys and values in the dictionaries.

b) I am using Python 2.7.

How am I supposed to do this?

Upvotes: 0

Views: 1873

Answers (3)

Tom Karzes
Tom Karzes

Reputation: 24052

Updated version: Based on the info given in the comments, I have modified my solution to handle multiple instances of the same choice value, or missing instances, in d2. Here is the new solution, which I believe will do what was requested. I have modified the sample data to exercise the new functionality:

d1 = {'c_choice1': '1.10', 'c_choice2': '1.20'}
d2 = {'John': {'strength': 'choice1', 'agility': 'choice1'}}
d3 = {'John': {'choice1': 'c_choice1', 'choice2': 'c_choice2'}}

d4 = {}
for k in d3:
    d2x = d2[k]
    d3x = d3[k]

    d2y = {v: [] for x in (d2x.values(), d3x.keys()) for v in x}
    for k2 in d2x:
        d2y[d2x[k2]].append(k2)

    d4x = {v2 : d1[d3x[k3]] for k3 in d3x for v2 in d2y[k3]}
    d4[k] = d4x

Here is what d4 looks like:

{'John': {'agility': '1.10', 'strength': '1.10'}}

Note that choice1 occurs twice in d2, and choice2 is absent.

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599610

You could do this as a single nested dict comprehension:

d4 = {name: {stat: d1[d3[name][choice]]
             for name, choices in d2.items()
             for stat, choice in stats.items()}
      for name, stats in d2.items()}

but that's almost impossible to understand. Instead, a loop and a dict comp works well:

d4 = {}
for name, stats in d2.items():
    d4[name] = {stat: d1[d3[name][choice]] for name, choices in d2.items()
                                           for stat, choice in stats.items()}

Upvotes: 2

Gal Dreiman
Gal Dreiman

Reputation: 4009

You need to iterate over the keys and use then to extract data. for example:

In order to extract the names from d2:

for name in d2:
    print name

output:
John

Extract the dictionary for each name:

for name  in d2:
    for choice in d2[name]:
        print choice

output:
agility
strength

Upvotes: 0

Related Questions