Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old March 26th, 2004, 12:06 AM
Questioner Questioner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Questioner's home
Posts: 89 Questioner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 32 m 10 sec
Reputation Power: 6
Sorting tuples

Lets say I have a list containing tuples like this : [('abc', 321, 'car'), ('abb', 152, 'airplane'), ('bbb', 201, 'tank'), ('lcl', 200, 'ship')]

I'd like to sort the tuples by the second element in the tuples - the numbers, izzit possible?
__________________
What can change the nature of a man?

Reply With Quote
  #2  
Old March 26th, 2004, 01:36 AM
rebbit rebbit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 84 rebbit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m
Reputation Power: 5
it certainly is. here's a little example:

Code:
>>> l = [(1, 8, 1), (1, 1, 1), (1, 5, 1), (1, 2, 1), (1, 3, 1), (1, 4, 1)]
>>> l.sort(lambda x, y: x[1] - y[1])
>>> l
[(1, 1, 1), (1, 2, 1), (1, 3, 1), (1, 4, 1), (1, 5, 1), (1, 8, 1)]
>>> 


applying that to your list of tuples should be easy from there. I believe there is a nice little doc on sorting objects on the Python site. try the "Howto texts" section at python.org/doc

Reply With Quote
  #3  
Old March 26th, 2004, 03:01 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,195 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 13 h 31 m 54 sec
Reputation Power: 252
You can pass a comparision function to list.sort(), as rebbit pointed out. However as this involve calling the fuction for every comparison it can seriously slow down sorting of large lists.

An alternative and faster way is to use the Decorate-Sort-Undecorate, or DSU, pattern. To do this, transform the list into a list of tuples with the value to be sorted on as the first element, sort the list, then transform it back. This can easily be done with list substitutions. e.g.

Code:
>>> lst = [('abc', 321, 'car'), ('abb', 152, 'airplane'), ('bbb', 201, 'tank'), ('lcl', 200, 'ship')]
>>> decorated = [ (val[1], val) for val in lst]
>>> decorated.sort()
>>> sorted = [ val for (key,val) in decorated]
>>> sorted
[('abb', 152, 'airplane'), ('lcl', 200, 'ship'), ('bbb', 201, 'tank'), ('abc', 321, 'car')]
>>>


This is also sometimes known as a Schwartzian Transform, after the originator of the perl version.

Dave - The Developers' Coach

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Sorting tuples


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway