I have a following dictionary (nested):
d = {'q0': {'VB': 2, 'NN': 8}, 'VB': {'DT': 5, 'NN': 3, 'IN': 2}}
My Goal is to update the values present in the above dictionary to probabilities. That is calculated as P(VB|q0) = count of verb/total count of q0
Thus P(VB|q0) = 2/10 from the given dictionary. After similar calculation of every key my new dictionary should look like
d_new = {'q0': {'VB': 0.2, 'NN': 0.8}, 'VB': {'DT': 0.5, 'NN': 0.3, 'IN': 0.2}}
Now the code I wrote to implement above is as follows:
d = {'q0': {'VB': 2, 'NN': 8}, 'VB': {'DT': 5, 'NN': 3, 'IN': 2}}
d_new = {}
d_new.update(d)
for key in d.keys():
for i in d[key].keys():
d_new[key][i] = d[key][i]/ float(sum(d[key].values()))
This gives me the result as:
{'q0': {'VB': 0.2, 'NN': 0.9756097560975611}, 'VB': {'DT': 0.5, 'NN': 0.5454545454545454, 'IN': 0.6567164179104478}}
I am assuming that the original d is also getting updated. To prevent this I used update(). But it does not seem to work. How can I get the desired result?