Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 25th, 2008, 8:37 AM   #1
shocker
Newbie
 
Join Date: Mar 2008
Posts: 1
Rep Power: 0 shocker is on a distinguished road
A sort question

Hi all,


I have a sort questions. I can't really find an easy way to do this, so any advice is appreciated. Basically, I have a few dictionaries as such:


{a:[7,8,9]}
{b:[4,5,6,]}
{c:[1,2,3]}

Is there any easy way to sort by the second item in the list such that I get:
{c:[1,2,3]}
{b:[4,5,6,]}
{a:[7,8,9]}


I can do something similar in the shell with sort -k, but I'm not sure if there is a simple way to do this in python. Any help is appreciated!
shocker is offline   Reply With Quote
Old Mar 25th, 2008, 8:56 AM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,837
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: A sort question

Sure thing. Just use a lambda function as a sort callback to the .sort() method.

Lambda functions allow you to pass a function as an argument. You could also create a function with def, and pass that through, but it's not as fancy.

This is according to your example, where each dictionary always has one item. If it doesn't always have one item, you will have to do some sort of nested sort according to more specific requirements.

>>> foo = [ {'a':[7,8,9]}, {'b':[4,5,6]}, {'c':[1,2,3]}, {'d':[0,4,10]} ]
>>> foo.sort(lambda x,y: x.values()[0][1]-y.values()[0][1])
>>> foo
[{'c': [1, 2, 3]}, {'d': [0, 4, 10]}, {'b': [4, 5, 6]}, {'a': [7, 8, 9]}]

I'm not sure how much gibberish I'm spouting, so if you need me to explain sort callbacks or lambda functions, just ask.
Sane is online now   Reply With Quote
Old Mar 25th, 2008, 8:56 AM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Re: A sort question

First of all, I think you're representing your dictionary wrongly. Don't you mean:
{ a:[7,8,9],
  b:[4,5,6],
  c:[1,2,3] }

You have to remember that a dictionary has no inherent order. That said, you can convert the dictionary into a tuple using the items or iteritems methods, which return a list of tuples and an iterator over said list respectively.

A bit of Googling found this interesting solution: http://www.python.org/dev/peps/pep-0265/

For a dictionary x, this will work:
sorted(x.iteritems(), key = lambda ( (_, value) ) : value)
This tells the sorted function to iterate over the list of tuples and to use the second part of the tuple as its sort key.

EDIT: Seems Sane beat me to it, though his method works differently.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 25th, 2008, 3:42 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,837
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: A sort question

Yes. It seems a bit strange to me too how each dictionary has only one item.

But it may not be the way you suggested Ooble, because a dictionary can not be sorted. The order of items in a dictionary can't be changed since the positions are arbitrarily assigned as a result of hash values. If you wanted to sort a dictionary, you would have to change its data type (which is in fact accomplished with the code you posted).
Sane is online now   Reply With Quote
Old Mar 25th, 2008, 3:58 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Re: A sort question

Shocker, if you want a list of single-value dictionaries as Sane suggests, you'd be better off using tuples instead of dictionaries anyway.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question regarding data input into PHP/Mysql using something other than GET davil PHP 6 Nov 20th, 2007 8:06 AM
Quick Sort program jazz C 11 Jun 1st, 2006 4:08 AM
sort of a noob question... Varrickmana C++ 5 May 26th, 2006 10:00 AM
sort and swap brad sue C 1 Mar 25th, 2006 6:33 AM
threaded merge sort help AusTex C 1 May 1st, 2005 4:58 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:28 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC