468,760 Members | 1,913 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,760 developers. It's quick & easy.

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 15102
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by David Bear | last post: by
1 post views Thread by shalendra chhabra | last post: by
6 posts views Thread by praba kar | last post: by
10 posts views Thread by Philippe C. Martin | last post: by
8 posts views Thread by nidhog | last post: by
17 posts views Thread by John Salerno | last post: by
1 post views Thread by Giovanni Toffoli | last post: by
9 posts views Thread by Wolfgang Draxinger | last post: by
5 posts views Thread by jrod11 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.