2

How do you sort a dictionary by the frequency of its values in Python? For example, if I have a dictionary:

{'apples': 2, 'oranges': 5, 'bananas': 2, 'corn': 1, 'tangerines': 2, 'popsicles': 5}

How can I get it to return:

{'apples': 2, 'bananas': 2, 'tangerines': 2, 'oranges': 5, 'popsicles': 5, 'corn': 1}

EDIT: Upon closer inspection, the only answer currently given does not answer this question. The answer sorts the dictionary according to the "size" of the values and not the frequency with which the values occur. Also, the answer that is linked also does not answer this question. Like the only answer currently given, the question linked is about sorting dictionaries according to the size of the values, not the frequency with which the values occur

Aaron
  • 131
  • 5
  • Then mark that answer as accepted. You might need to wait for some time, though. – Kitswas Jun 16 '22 at 05:24
  • The question you have linked actually does not answer my question - the question you have linked is about sorting dictionaries according to the size of the values, not the frequency of the values. For example, if an arbitrary value occurs 5 times in a dictionary, it should be sorted to come before a value that occurs 4 times, which itself should be sorted to come before another value that occurs 3 times – Aaron Jun 17 '22 at 04:56
  • I have an answer ready. @mention me when this question is reopened. – Kitswas Jun 17 '22 at 12:27
  • Why does it go 2, 5, 1? – ChrisGPT was on strike Jun 17 '22 at 15:51
  • @Kitswas would you be able to post it as a comment and then when the question is reopened repost it as an answer? – Aaron Jun 17 '22 at 16:28
  • @Chris it seems that 2 is the value that occurs most frequently in this dictionary, so all the 2-values would come first, and then 5 is the value that occurs next most frequently, so all the 5-values would come next, and then 1 is the value that occurs least frequently, so it would come last – Aaron Jun 17 '22 at 16:30
  • @Aaron You will need to fix the indentation yourself. ``` store = {'apples': 2, 'oranges': 5, 'bananas': 2, 'corn': 1, 'tangerines': 2, 'popsicles': 5} freq = {} for value in store.values(): if freq.get(value) == None: freq[value] = 1 else: freq[value] += 1 pass print(freq) # debugging only pos = sorted(freq.items(), key=lambda item: item[1], reverse=True) # use reverse=True here to reverse sort pos = list(map(lambda x: x[0], pos)) print(pos) store = dict(sorted(store.items(), key=lambda item: pos.index(item[1]))) print(store) ``` – Kitswas Jun 17 '22 at 17:30
  • @metatoaster can you reopen this question? For the reasons I explained in my EDIT, this question is not a duplicate of any that I am aware of – Aaron Jun 18 '22 at 01:14

1 Answers1

1

Try this:

data = {'apples': 5, 'oranges': 2, 'bananas': 5, 'corn': 1, 'tangerines': 5, 'popsicles': 2}
{j: i for j, i in sorted(data.items(), key=lambda item: item[1], reverse=True)}
B. Ozen
  • 63
  • 10
  • Upon closer inspection, this actually does not answer my question - for example, ```df = {'Pdpn': 26, 'Hrct1': 25, 'Gpr4': 25, 'S100a16': 25, 'Pdp1': 25, 'Atp2b2': 23}``` is not sorted according to the frequency of the values. I see now that the method of sorting suggested in this answer sorts according to the size of the value, I believe – Aaron Jun 17 '22 at 04:51
  • The question was somewhat lacking. Now I can understand you more clearly. I don't know if there is a direct formula, but I followed a path like this. First, I put the values ​​into a list, sorted the most repetitive ones, and printed the dict according to the values. I can't enter new reply. Open a new question and I'll share the code. – B. Ozen Jun 17 '22 at 05:48