Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning the three maximal values in a dictionary

I have the following dictionary:

'{0: 0, 1: 11, 2: 26, 3: 43, 4: 14, 5: 29, 6: 34, 7: 49, 8: 49, 9: 108, 10: 124, 11: 108, 12: 361, 13: 290, 14: 2118, 15: 5408, 16: 43473, 17: 109462, 18: 111490, 19: 244675, 20: 115878, 21: 6960}'

And for this dictionary I want write a function that returns the three key-value pairs that have the highest values (So in this case key 18, 19, 20).

I came up with the following:

cachedict = nr_of_objects_per_century() #Dictionary mentioned above

def top_3_centuries():
        max_nr_works_list = sorted(cachedict.values())
        top_3_values = []
        for i in range(len(max_nr_works_list)-3, len(max_nr_works_list)):
            top_3_values.append(max_nr_works_list[i])
            print(top_3_values)

This gives me a list of the max-values I want to lookup. But how do I proceed from here? Is there a way to do this without a reverse-lookup (Which is slow for dictionaries, right?) I have the feeling that I can do this task much more efficiently/pythonic.

like image 301
Psychotechnopath Avatar asked Nov 20 '18 10:11

Psychotechnopath


People also ask

Can you have 3 items in a dictionary Python?

You can add as many items as you like.

What method used to return a list of all the values in the dictionary?

values() method returns a view object that displays a list of all values in a given dictionary.

How do you find the max 3 values in Python?

Step 1: input three user input number. Step2: Add three numbers to list. Step 3: Using max() function to find the greatest number max(lst). Step 4: And finally we will print maximum number.

How do you return the maximum value in a dictionary?

Python find highest value in dictionary By using the built-in max() method. It is provided with the 'alpha_dict' variable to obtain the highest value from and to return the key from the given dictionary with the highest value, the dict. get() method is used.


4 Answers

You could also use collections.Counter with most_common (which internally uses a heap queue):

from collections import Counter

dct = {0: 0, 1: 11, 2: 26, 3: 43, 4: 14, 5: 29, 6: 34, 7: 49, 8: 49, 
       9: 108, 10: 124, 11: 108, 12: 361, 13: 290, 14: 2118, 15: 5408, 
       16: 43473, 17: 109462, 18: 111490, 19: 244675, 20: 115878, 21: 6960}

count = Counter(dct)
print(count.most_common(3))  # [(19, 244675), (20, 115878), (18, 111490)]
like image 146
hiro protagonist Avatar answered Oct 24 '22 11:10

hiro protagonist


heapq.nlargest

You can avoid a full sort here by using a heap queue:

from heapq import nlargest
from operator import itemgetter

dct = {0: 0, 1: 11, 2: 26, 3: 43, 4: 14, 5: 29, 6: 34, 7: 49, 8: 49,
       9: 108, 10: 124, 11: 108, 12: 361, 13: 290, 14: 2118, 15: 5408,
       16: 43473, 17: 109462, 18: 111490, 19: 244675, 20: 115878, 21: 6960}

res = nlargest(3, dct.items(), key=itemgetter(1))

print(res)
# [(19, 244675), (20, 115878), (18, 111490)]
like image 30
jpp Avatar answered Oct 24 '22 11:10

jpp


You can use this:

a = {0: 0, 1: 11, 2: 26, 3: 43, 4: 14, 5: 29, 6: 34, 7: 49, 8: 49,
       9: 108, 10: 124, 11: 108, 12: 361, 13: 290, 14: 2118, 15: 5408,
       16: 43473, 17: 109462, 18: 111490, 19: 244675, 20: 115878, 21: 6960}

l = sorted(list(a.items()), key=lambda tup: tup[1], reverse=True)[:3]
print(l) # [(19, 244675), (20, 115878), (18, 111490)]

It converts the dictionary a into a list of tuples, sort by tup[1], reverse it and get the first 3 hits.

like image 3
b-fg Avatar answered Oct 24 '22 10:10

b-fg


You can do it like so:

dct = {0: 0, 1: 11, 2: 26, 3: 43, 4: 14, 5: 29, 6: 34, 7: 49, 8: 49, 9: 108, 10: 124, 11: 108, 12: 361, 13: 290, 14: 2118, 15: 5408, 16: 43473, 17: 109462, 18: 111490, 19: 244675, 20: 115878, 21: 6960}

res = [next(k for k in dct if dct[k]==v) for v in sorted(dct.values(), reverse=True)[:3]]
print(res)  # -> [19, 20, 18]

Break-down:

  • sorted(dct.values(), reverse=True)[:3]:: Takes the 3 max dictionary values.
  • next(k for k in dct if dct[k]==v):: returns the dictionary key, for which the value is one of the above 3 (iteratively).
like image 2
Ma0 Avatar answered Oct 24 '22 10:10

Ma0