sort dictionary by value
Alex Martelli
aleaxit at yahoo.com
Wed Apr 11 12:03:12 EDT 2001
<bas.vangils at home.nl> wrote in message
news:Jl_A6.51605$Gn3.698740 at dbsch1.home.nl...
> Hi everyone,
>
> I've been toying around with sorting a dictonary _by value_ for the last
couple
> of days. Using 'seqdict' from the ndict-thiny I found, I do just what I
want...
> at least, that's what I thought *sigh*.
>
> The problem is that I have some values that are not unique. That is,
suppose I
> have these key-value combinations stored in a dictionary called bla:
>
> 1 -> c
> 2 -> b
> 3 -> a
> 4 -> b
So, what DO you want to happen when a value is duplicated...?
> In short, my question is: I want to sort a dictionary by value and then
print
> some key-value pairs out of them. How do I go about this ?
What about:
auxlist = [ (value, key) for key, value in dict.items() ]
auxlist.sort()
for value, key in auxlist:
print key, value
This will sort entries with the same value by-key. Is that ok...?
Alex
More information about the Python-list
mailing list