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.
You can add as many items as you like.
values() method returns a view object that displays a list of all values in a given dictionary.
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.
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.
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)]
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)]
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.
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).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With