Lets say I have a dictionary:
{key1:value1........... keyn:valuen}
So lets say I want to write a function
def return_top_k(
O(n log k):
O(n log k)
import heapq k_keys_sorted = heapq.nlargest(k, dictionary)
You could use key keyword parameter to specify what should be used as a sorting key e.g.:
key
k_keys_sorted_by_values = heapq.nlargest(k, dictionary, key=dictionary.get)