Because I made my dict
file with append list. I have the following dict file:
dict_1 = {'a':["1","2","d","d","d","1","2","2"],
'b':["1","2","e","e","5","5","5","6"]}
How do I sort the values by frequency
within the list
so I get output like:
dict_1 = {'a':["d","d","d","2","2","2","1","1"],
'b':["5","5","5","e","e","6","2","1"]}
The order doesn't matter for strings of the same frequency
I tried
result=[]
for k,v in dict_1.items():
result.append(sorted(v, key = v.count,
reverse = True))
and got
[['2', 'd', 'd', 'd', '2', '2', '1', '1'],
['5', '5', '5', 'e', 'e', '1', '2', '6']]
Something is wrong with the "2" in the first list.
Thanks.