473,399 Members | 3,832 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,399 software developers and data experts.

Sorting of list containing tuples

Hi!

Assume we have a list l, containing tuples t1,t2...

i.e. l = [(2,3),(3,2),(6,5)]

And now I want to sort l reverse by the second element in the tuple,
i.e the result should ideally be:

l = [(6,5),(2,3),(3,2)]
Any ideas of how to accomplish this?
Thanks,

Ronny Mandal
May 18 '06 #1
7 15276
Ronny Mandal <ro*****@math.uio.no> writes:
And now I want to sort l reverse by the second element in the tuple,
i.e the result should ideally be:

l = [(6,5),(2,3),(3,2)]


sorted(l, key = lambda a: -a[1])
May 18 '06 #2
Uhm, thanks. (I've used lambda-sort earlier, but quite forgot......)
:)

On 18 May 2006 12:38:55 -0700, Paul Rubin
<http://ph****@NOSPAM.invalid> wrote:
Ronny Mandal <ro*****@math.uio.no> writes:
And now I want to sort l reverse by the second element in the tuple,
i.e the result should ideally be:

l = [(6,5),(2,3),(3,2)]


sorted(l, key = lambda a: -a[1])

May 18 '06 #3
Ronny Mandal wrote:
Assume we have a list l, containing tuples t1,t2...

i.e. l = [(2,3),(3,2),(6,5)]

And now I want to sort l reverse by the second element in the tuple,
i.e the result should ideally be:

l = [(6,5),(2,3),(3,2)]
Any ideas of how to accomplish this?


def cmpfun(a,b):
return cmp(b[1],a[1])

l.sort(cmpfun)
May 18 '06 #4
On Thu, 18 May 2006 21:29:59 +0200 in comp.lang.python, Ronny Mandal
<ro*****@math.uio.no> wrote:
Hi!

Assume we have a list l, containing tuples t1,t2...

i.e. l = [(2,3),(3,2),(6,5)]

And now I want to sort l reverse by the second element in the tuple,
i.e the result should ideally be:

l = [(6,5),(2,3),(3,2)]
Any ideas of how to accomplish this?
Thanks,

Ronny Mandal

def my_cmp(t1,t2): c1 = t1[1]
c2 = t2[1]
if c1 > c2: return 1
if c2 > c1: return -1
return 0 l [(2, 3), (3, 2), (6, 5)] l.sort(cmp=my_cmp, reverse = True)
l

[(6, 5), (2, 3), (3, 2)]

HTH,
-=Dave

--
Change is inevitable, progress is not.
May 18 '06 #5
>>> l = [(2,3),(3,2),(6,5)]
from operator import itemgetter
sorted(l, key=itemgetter(1), reverse=True)

[(6, 5), (2, 3), (3, 2)]

Bye,
bearophile

May 18 '06 #6
On Thu, May 18, 2006 at 12:38:55PM -0700, Paul Rubin wrote:
Ronny Mandal <ro*****@math.uio.no> writes:
And now I want to sort l reverse by the second element in the tuple,
i.e the result should ideally be:

l = [(6,5),(2,3),(3,2)]


sorted(l, key = lambda a: -a[1])


Or in Python <2.4:

l.sort(lambda x,y: x[1]-y[1])

(Although that's not technically perfect. Sort expect a function that
returns -1, 0 or 1. Here we get positive integers and negative
integers. YMMV.)

Kindly
Christoph
May 18 '06 #7
On Thu, May 18, 2006 at 09:52:39PM +0200, Christoph Haas wrote:
On Thu, May 18, 2006 at 12:38:55PM -0700, Paul Rubin wrote:
Ronny Mandal <ro*****@math.uio.no> writes:
And now I want to sort l reverse by the second element in the tuple,
i.e the result should ideally be:

l = [(6,5),(2,3),(3,2)]


sorted(l, key = lambda a: -a[1])


Or in Python <2.4:

l.sort(lambda x,y: x[1]-y[1])

(Although that's not technically perfect. Sort expect a function that
returns -1, 0 or 1. Here we get positive integers and negative
integers. YMMV.)


Crap... why do I always forget about cmp()? :)

This should be it:

l.sort(lambda x,y: cmp(x[1],y[1]))

Kindly
Christoph

May 18 '06 #8

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

Similar topics

4
by: David Bear | last post by:
I have a list of tupples. Each tupple has 3 items. The first item is an integer. I'd like to sort the list(of tupples), based on the first element of the tupple. I thought about stringifying...
1
by: shalendra chhabra | last post by:
Hi, I just had a tryst with python. I was wondering if python is good enough to do this kind of job -- for it has extensive support of string and pattern matching, ordering and list handling. ...
6
by: praba kar | last post by:
Dear All, I have doubt regarding sorting. I have a list that list have another list (eg) list = ,,] I want to sort only numeric value having array field. How I need to do for that.
10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
8
by: nidhog | last post by:
Hello guys, I made a script that extracts strings from a binary file. It works. My next problem is sorting those strings. Output is like: ---- snip ---- 200501221530
17
by: John Salerno | last post by:
Hi everyone. If I have a list of tuples, and each tuple is in the form: (year, text) as in ('1995', 'This is a citation.') How can I sort the list so that they are in chronological order based...
1
by: Giovanni Toffoli | last post by:
Hi, I'm not in the mailing list. By Googling, I stepped into this an old post: (Thu Feb 14 20:40:08 CET 2002) of Jeff Shannon:...
9
by: Wolfgang Draxinger | last post by:
H folks, I got, hmm not really a problem, more a question of elegance: In a current project I have to read in some files in a given directory in chronological order, so that I can concatenate...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
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: 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
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
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
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...

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.