Opticgenius - 2 years ago 72
Python Question

# finding probablity of values in dictionary

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():
``````

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