dictionary: sorting the values preserving the order

R

Rakesh

Hi,
For a particular problem of mine, I want to sort <key, value> pairs
by its value.

Eg:

Input:

A, 4
B, 5
C, 1
D, 2
E, 3

I would like the output to be:

C
D
E
A
B

i.e. I would like to get the keys in the sorted order of values.

I did google around a little bit. One solution to a similar problem
suggested is:

# Courtesy:
http://aspn.activestate.com/ASPN/Python/Cookbook/Recipe/52306
def sortedDictValues3(adict):
keys = adict.keys()
keys.sort()
return map(adict.get, keys)

This gets a list sorted by the keys. How would I get a revised
dictionary
sorted by its values.
 
V

Vikram

hi,

assuming your key-value relationship is one-to-one then as a simple first
pass you can simply initialize d1={} and for i in d.keys(): d1[d] = i
and pass d1 to your sortedDictValue3 function, no?

thanks,
Vikram
 
S

Satchidanand Haridas

Rakesh said:
Hi,
For a particular problem of mine, I want to sort <key, value> pairs
by its value.

Eg:

Input:

A, 4
B, 5
C, 1
D, 2
E, 3

I would like the output to be:

C
D
E
A
B


the following code does that:
>>> d1 = {'a':4,'b':5,'c':1,'d':2,'e':3}
>>> i1 = [ (d1, i) for i in d1.keys() ]
>>> i1.sort()
>>> i1 [(1, 'c'), (2, 'd'), (3, 'e'), (4, 'a'), (5, 'b')]
>>> for each in i1:

.... print each[1]
c
d
e
a
b


thanks,
Satchit
 
R

Ron_Adam

Hi,
For a particular problem of mine, I want to sort <key, value> pairs
by its value.

Eg:

Input:

A, 4
B, 5
C, 1
D, 2
E, 3

I would like the output to be:

C
D
E
A
B

i.e. I would like to get the keys in the sorted order of values.

Generally, dictionaries nearly always have two parts. The dictionary
itself, and a separate list of keys to access it with.

To access the dictionary in a particular order, you just need a sorted
key list.

Since what you want is to access by value, you need to create a second
dictionary with the values as the keys. That will only work if the
values never repeat. If they do, then you need to use a list and not a
dictionary.

This creates a second dictionary with a sorted value key list.

alpha_dict = {'A':4, 'B':5, 'C':1, 'D':2, 'E':3}

# Create a new dictionary with keys and values exchanged.
num_dict = {}
for k in alpha_dict.keys():
num_dict[ alpha_dict[k] ] = k

# Get the num_dict keys and sort them.
num_keys = num_dict.keys()
num_keys.sort()


Ron
 
L

Luis M. Gonzalez

Another alternative:

d1 = {'a':4,'b':5,'c':1,'d':2,'e':3­}

il=[(v,k) for k,v in d1.items()]
il.sort()
 
T

Terry Reedy

Rakesh said:
This gets a list sorted by the keys.

That is all you *can* get (with the list keys being the dict values).
How would I get a revised dictionary sorted by its values.

You can't. A dictionary is not sorted. The print order is arbitrary and
not controllable.

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top