I have this dictionary of lists:
my_dict = {'Summer':['Summer','Summer','gone'],'gone':['forever'],'forever':['gone']}
And I want the probabilities for each word in the list as a dictionary, in this case the expected dictionary is:
my_dict_probs = {'Summer':{'Summer':0.66,'gone':0.33}, 'gone':{'forever':1}, 'forever'{'Summer':1}
So I have tried this:
prob_dict = {}
for k,v in my_dict.items():
prob_dict[k] = v/len(v)
prob_dict
And I get this error: TypeError: unsupported operand type(s) for /: 'list' and 'int'
. I guess that I should count per each unique value, so my approach is not working. Please, could you help me?
Try this
my_dict = {'Summer':['Summer','Summer','gone'],'gone':['forever'],'forever':['gone']}
for v in my_dict:
my_dict[v]={j:round(my_dict[v].count(j)/len(my_dict[v]),2) for j in my_dict[v]}
print (my_dict)