473,396 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

dictionary: sorting the values preserving the order

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/Pyt...k/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.

Jul 18 '05 #1
5 2072

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]] = i
and pass d1 to your sortedDictValue3 function, no?

thanks,
Vikram
On 31 Mar 2005, Rakesh wrote:
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/Pyt...k/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.


Jul 18 '05 #2
Rakesh wrote:
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], 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

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/Pyt...k/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.

Jul 18 '05 #3
On 31 Mar 2005 22:40:53 -0800, "Rakesh" <ra***********@yahoo.com>
wrote:
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

Jul 18 '05 #4
Another alternative:

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

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

Jul 18 '05 #5

"Rakesh" <ra***********@yahoo.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rory Campbell-Lange | last post by:
I have a dictionary of images. I wish to sort the dictionary 'v' by a dictionary value using python 2.3. The dictionary value is the date attribute as shown here: v This attribute is an...
9
by: jwedel_stolo | last post by:
Hi I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table I have used two methods in order to determine the sort order of the...
12
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
5
by: JerryB | last post by:
Hi, I have a dictionary for counting ocurrences of strings in a document. The dictionary looks like this: 'hello':135 'goodbye':30 'lucy':4 'sky':55 'diamonds':239843 'yesterday':4
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
2
by: Chris Kratz | last post by:
We are having a weird problem that we ran into recently. If I use the following statements to create a test table and then run the select statement at the end, we get a very strange sort order. ...
8
by: spohle | last post by:
hi i have a normal dictionary with key and value pairs. now i wanna sort by the keys BUT in a specific order i determine in a list !? any ideas dic = {'key1':'value1', 'key2':'value2',...
2
by: jediknight | last post by:
Hi, I have a listview which has columns of text and columns of numerical data. I need to be able to sort these columns into ascending/desending order whenever the user clicks on the column...
4
by: rajtalent | last post by:
hi all, I want to sort the colum when clicks the columnheader using vb.net 2005 .But i receive the following error "Error 1 Overload resolution failed because no accessible 'New' accepts...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.