nDimensional sparse histogram in python.

Alex Martelli aleaxit at yahoo.com
Wed Feb 1 23:22:04 EST 2006


KraftDiner <bobrien18 at yahoo.com> wrote:

> Many thanks everyone.
> 
> One last quick question...
> The dictionary, I believe, is sorted by the key.

Dictionaries are NOT sorted -- they're hashtables.

> Is there a way to sort it by the value?
> Say I wanted to put out a list of the frequency sorted by highest to
> lowest?

import operator
for k, v in sorted(somedict, key=operator.itemgetter(1), reverse=1):
    print k, v

emits key/value pairs, one per line, for any dictionary sorted by
descending order of values.  Embellish to taste.


Alex



More information about the Python-list mailing list