sort dictionary by value

bas.vangils at home.nl bas.vangils at home.nl
Wed Apr 11 11:02:01 EDT 2001


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

What I did was:

	s = seqdict(bla)
	s.swap()
	s.sort(lambda x,y:cmp(x,y))

and then do some printing (not interesting here, so left out). The problem is,
that after swapping the keys with the values, the keys aren't unique anymore:(
So sorting and printing them well ... goes wrong :-)

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 ?

	Bas


-- 
Bas van Gils <bas.vangils at home.nl>
An invasion of armies can be resisted, but not an idea whose time has come.
                                                           (Victor Hugo) 



More information about the Python-list mailing list