Opticgenius - 1 year ago 65

Python Question

I have a default dict which looks like this:

`my_dict = default(dict, {"K": {"k": 2, "x": 1.0}, "S": {"_":1.0, "s":1}, "EH": {"e":1.0}})`

The keys are phonemes, and values that are dictionaries themselves are graphemes which occur a certain amount of times, which are the respective numbers in the default dict.

The function should return another default dict containing the probabilities, which will look like this:

`defaultdict(<class 'dict'>, {'EH': {'e': 1.0}, 'K': {'k': 0.6666666666666666, 'x': 0.3333333333333333}, 'S': {'_': 0.5, 's': 0.5}})`

'e' remains the same, as 1.0/1 = 1.0. 'K' has values of 0.66666 and 0.33333 because 2/3 = 0.66666 and 1/3 = 0.3333333. 'S' has values of 0.5 and 0.5, because 1/2=0.5 for each of them. The probabilities in the return dict must always sum to one.

so far I have this:

`from collections import defaultdict`

my_dict = default(dict, {"K": {"k": 2, "x": 1.0}, "S": {"_":1.0, "s":1}, "EH": {"e":1.0}})

def dict_probability(my_dict):

return_dict = defaultdict(dict)

for char in my_dict.values():

I'm just not sure how I should go about this. Any help would be appreciated.

Answer Source

For each of your subdictionnaries, you would like to divide each value by the sum of the subdictionnary values:

```
my_dict = {"K": {"k": 2, "x": 1.0}, "S": {"_":1.0, "s":1}, "EH": {"e":1.0}}
{k:{k1:v1/sum(v.values()) for k1,v1 in v.iteritems()} for k,v in my_dict.iteritems()}
{'EH': {'e': 1.0},
'K': {'k': 0.6666666666666666, 'x': 0.3333333333333333},
'S': {'_': 0.5, 's': 0.5}}
```