Sorting of list containing tuples

2006-05-18 Thread Ronny Mandal
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 --

Re: Sorting of list containing tuples

2006-05-18 Thread Paul Rubin
Ronny Mandal [EMAIL PROTECTED] 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]) -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting of list containing tuples

2006-05-18 Thread Ronny Mandal
Uhm, thanks. (I've used lambda-sort earlier, but quite forgot..) :) On 18 May 2006 12:38:55 -0700, Paul Rubin http://[EMAIL PROTECTED] wrote: Ronny Mandal [EMAIL PROTECTED] writes: And now I want to sort l reverse by the second element in the tuple, i.e the result should ideally be: l

Re: Sorting of list containing tuples

2006-05-18 Thread Wojciech Muła
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):

Re: Sorting of list containing tuples

2006-05-18 Thread Dave Hansen
On Thu, 18 May 2006 21:29:59 +0200 in comp.lang.python, Ronny Mandal [EMAIL PROTECTED] 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 =

Re: Sorting of list containing tuples

2006-05-18 Thread Christoph Haas
On Thu, May 18, 2006 at 12:38:55PM -0700, Paul Rubin wrote: Ronny Mandal [EMAIL PROTECTED] 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:

Re: Sorting of list containing tuples

2006-05-18 Thread bearophileHUGS
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 -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting of list containing tuples

2006-05-18 Thread Christoph Haas
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 [EMAIL PROTECTED] writes: And now I want to sort l reverse by the second element in the tuple, i.e the result should ideally be: l =