Order by value in dictionary

Abandoned besturk at gmail.com
Wed Oct 17 18:12:59 CEST 2007


I tried these:

def largest_sort(d, n):
    return sorted(d, key=d.__getitem__, reverse=True)[:n]


def largest_heap(d, n):
    return heapq.nlargest(n, d, d.__getitem__)

def sortt(d):
    sorted_items = sorted((item[1], item[0]) for item in
d.iteritems(),
                          reverse=True)
    return map(operator.itemgetter(1), sorted_items)

.....
and the fastest method is second ;)




More information about the Python-list mailing list