How can I populate the key value pairs of a new dictionary from an existing dictionary?

Viewed 59

I have a dictionary like below. The first value (outside brackets) is the key. The first value in the braces is the value.
Next in the square brackets are the metadata about the values.

So for the key 185589766 and the value 183701781 there are two entries in the dictionary for two separate book names- one for '"Cloud Computing" How to use it for your business' and another for "Learn How to Be a Success in Network Marketing". The second entry in the metadata is a probability value and the third entry is the sign (+,-)

25255942 {52691892: [('Internet of Things (IOT) Device Management', 0.4444444444444444, 'neg')], 72359602: [('Internet of Things (IOT) Device Management', 1.0, 'neg'), ('Questions', 0.07692307692307693, 'neg')]}
185589766 {183701781: [('"Cloud Computing" How to use it for your business', 0.4, 'pos'), ('Learn How to Be a Success in Network Marketing', 0.16666666666666666, 'pos')], 183702935: [('"Cloud Computing" How to use it for your business', 0.4, 'pos'), ('Learn How to Be a Success in Network Marketing', 0.16666666666666666, 'pos')], 110069642: [('Learn How to Be a Success in Network Marketing', 0.3333333333333333, 'pos'), ('How to make money in network marketing', 1.0, 'pos')]}
110370832 {9420651: [('Dinner and a Movie', 0.5, 'neg'), ("Let's get a Furmeet setup and make a home here", 0.14285714285714285, 'neg'), ('Afterwork Happy Hours and Dinner', 0.5, 'neg')], 76185392: [('Dinner and a Movie', 0.5, 'neg'), ("Let's get a Furmeet setup and make a home here", 0.3333333333333333, 'neg')], 9779381: [("Let's get a Furmeet setup and make a home here", 0.25, 'neg')]}

Now I need to create a new dictionary from this existing dictionary. The new dictionary will contain keys, values, book names of the existing dictionary excluding the signs and the probability values. Apart from that it will store a new entry 'propagation probability' in the metadata. How can I do that?

The new entry 'propagation probability' should be added to the dictionary in the format below (added sample for a key):

25255942 {52691892: [('Internet of Things (IOT) Device Management', prop_prob1)], 72359602: [('Internet of Things (IOT) Device Management', prop_prob2), ('Questions', prop_prob3)]}

where prop_prob1, prop_prob2, prop_prob3 are the respective propagation probabilities. These propagation probabilities will be computed by using an algorithm and will be stored in the new dictionary.

1 Answers

Try this out:

new_dict = {}

for key in some_dict:
    new_dict[key] = {}
    for subkey in some_dict[key]:
       prop_prob = # Your calculation here
       new_entry = [((some_dict[key][subkey][i])[0], prop_prob) 
                    for i in range(len(some_dict[key][subkey]))]
       new_dict[key][subkey] = new_entry
Related