473,395 Members | 1,972 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,395 software developers and data experts.

Sort dictionary

Hi!

I have:

x = {'a':3, 'b':2, 'c':4}

How can I sort x by value? (I tried using sorted() with x.items() - but I
didn't get a dictionary as response.)

My second question:
How can I reduce the dictionary to 2 items (so delete everything after the
first two items)

Thanks in advance.

Best regards,
Markus
Jan 2 '06 #1
4 1807
Markus Franz wrote:
Hi!

I have:

x = {'a':3, 'b':2, 'c':4}

How can I sort x by value? (I tried using sorted() with x.items() - but I
didn't get a dictionary as response.)

My second question:
How can I reduce the dictionary to 2 items (so delete everything after the
first two items)

Thanks in advance.

Best regards,
Markus


See my reply in de.comp.lang.python

Claudio
Jan 2 '06 #2
This function return a tuple of value-key pairs, sorted by value:
x = {'a':3, 'b':2, 'c':4}
def sortByValue(myDict): return sorted( [(v,k) for k,v in myDict.items()] )

If you want to get the first two value-key pairs:
sortByValue(x)[:2]

[(2, 'b'), (3, 'a')]

Hope this helps...
Luis

Jan 2 '06 #3
This function returns a tuple of value-key pairs, sorted by value:
x = {'a':3, 'b':2, 'c':4}
def sortByValue(myDict): return sorted( [(v,k) for k,v in myDict.items()] )

If you want to get the first two value-key pairs: sortByValue(x)[:2]

[(2, 'b'), (3, 'a')]

Hope this helps...
Luis

Jan 2 '06 #4
You can't get a dictionary ordered by values, but you can get a list of
key-value pairs ordered by value:

def sortByValue(myDict):
x = sorted( [(v,k) for k,v in myDict.items()] )
return [(k,v) for v,k in x]

For getting only the first two pairs:

sortByValue(x)[:2]

Hope this helps...
Luis

Jan 3 '06 #5

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

Similar topics

9
by: Afanasiy | last post by:
This is fairly simple in PHP, how do I do it in Python? http://www.php.net/manual/en/function.ksort.php
15
by: KraftDiner | last post by:
I have two lists. I want to sort by a value in the first list and have the second list sorted as well... Any suggestions on how I should/could do this?
99
by: Shi Mu | last post by:
Got confused by the following code: >>> a >>> b >>> c {1: , ], 2: ]} >>> c.append(b.sort()) >>> c {1: , ], 2: , None]}
6
by: max sharma | last post by:
Hi all, I am using a hashtable for my application. Its similar to word count application. How can I sort the hashtable w.r.t. the VALUE and not the KEY. The sample data of the table is given below...
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',...
5
by: Neil Chambers | last post by:
Hi All, I'm looking to see if it's feasible to use the SortObjectCommand included in the Microsoft.Powershell.Commands assembly. I have a Dictionary Dictionary<string, intd = new...
2
by: kdt | last post by:
Hi, I need to perform some horrible functions in python I need to do, using sort in a similar way that Excel can. With a dictionary like: >>> d {8: (99, 99), 9: , 4: , 5: (67, 77)} I...
1
by: pravinasp | last post by:
Hello there Iam stuck trying to sort the dictionary object in classic asp. Am trying to sort by the values of the dictionary object and not using keys, and once sorted by values i need the keys...
4
by: NvrBst | last post by:
I have a log viewer. I sort the DataGridView by the Time Column and then run a function to set all cell backcolors depending if the cell above is different. This works correctly, however, when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.