-1

This is my dictionary :

d = {'jan': 50, 'feb': 30, 'march': 60, 'april': 50, 'may': 50, 'june': 60, 'july': 20}

I am expecting output like this :

d = {'jan': 50, 'april': 50, 'may': 50, 'march': 60, 'june': 60, 'feb': 30, 'july': 20}

When I run this program I am getting different output than expected :

d = {'jan': 50, 'feb': 30, 'march': 60, 'april': 50, 'may': 50, 'june': 60, 'july': 20}
sortlist = sorted(d, key=d.get)
print(sortlist)
wim
  • 338,267
  • 99
  • 616
  • 750
edward
  • 29
  • 1
  • 7
  • 1
    If you want to sort by the number of duplicates, then you need to do some counting... `key=d.get` obviously isn't gonna do the trick. – Aran-Fey Mar 20 '19 at 08:42
  • 2
    It would be useful if you could provide a bit more information on what you are actually trying to achieve? As it stands it is very unclear, at least to me. – John Sloper Mar 20 '19 at 08:47
  • Looks like a job for `Counter`. Perhaps count the repeating values of the dict and then display them in desc ord – DirtyBit Mar 20 '19 at 08:53
  • I guess you want to do these 3 things, 1: count how many times did the number appeared, 2: sort by the count, 3: if the number is the same, sort by months in the key. Correct? – Yang HG Mar 20 '19 at 08:59
  • @Ramani how exactly do you want your dict to be sorted ? It is not clear as to you want it to be sorted by key or value – penta Mar 20 '19 at 09:25
  • Answered below, which should be top rated search... https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value – Bazzert Jul 21 '20 at 22:56

2 Answers2

1

You can start by counting the amount of times each value appears with collections.Counter:

from collections import Counter
c = Counter(d.values())
# Counter({20: 1, 30: 1, 50: 3, 60: 2})

And now sort the dictionary looking up how many times each value appears using a key in sorted:

sorted(d.items(), key=lambda x: c[x[1]], reverse=True)
[('jan', 50), ('april', 50), ('may', 50), ('march', 60), ('june', 60), 
 ('feb', 30), ('july', 20)]

Note however that if you obtain a dictionary from the result, the order will not be mantained, as dictionaries have no order.

So one thing you can so is use collections.OrderedDict to keep the order, simply call OrderedDict(res) on the resulting list of tuples.

yatu
  • 86,083
  • 12
  • 84
  • 139
-1
d={'january': 500, 'feb':600, 'march':300,'april':500,'may':500,'june':600,'july':200}

from collections import defaultdict
from collections import OrderedDict

count_dict = defaultdict(int)
for key, value in d.items():
  count_dict[value] += 1

First, we count occurences of each value. Counter can be used instead of defaultdict. Then sort them according to count_dict lookup table we just created.

sorted_dict = OrderedDict(sorted(d.items(), key=lambda item: count_dict[item[1]], reverse=True))
print(sorted_dict)

>>> OrderedDict([('january', 500), ('april', 500), ('may', 500), ('feb', 600), ('june', 600), ('march', 300), ('july', 200)])

Update : You can create count_dict with Counter like:

from collections import Counter

count_dict = Counter(d.values())
Seljuk Gulcan
  • 1,826
  • 13
  • 24