Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Sorting of list containing tuples (http://www.velocityreviews.com/forums/t357820-sorting-of-list-containing-tuples.html)

 Ronny Mandal 05-18-2006 07:29 PM

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

 Paul Rubin 05-18-2006 07:38 PM

Re: Sorting of list containing tuples

Ronny Mandal <ronnyma@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])

 Ronny Mandal 05-18-2006 07:42 PM

Re: Sorting of list containing tuples

Uhm, thanks. (I've used lambda-sort earlier, but quite forgot......)
:)

On 18 May 2006 12:38:55 -0700, Paul Rubin
<http://phr.cx@NOSPAM.invalid> wrote:

>Ronny Mandal <ronnyma@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])

 Dave Hansen 05-18-2006 07:43 PM

Re: Sorting of list containing tuples

On Thu, 18 May 2006 21:29:59 +0200 in comp.lang.python, Ronny Mandal
<ronnyma@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.

 Wojciech =?iso-8859-2?Q?Mu=B3a?= 05-18-2006 07:45 PM

Re: Sorting of list containing tuples

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)

 bearophileHUGS@lycos.com 05-18-2006 07:52 PM

Re: Sorting of list containing tuples

>>> 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

 Christoph Haas 05-18-2006 07:52 PM

Re: Sorting of list containing tuples

On Thu, May 18, 2006 at 12:38:55PM -0700, Paul Rubin wrote:
> Ronny Mandal <ronnyma@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

 Christoph Haas 05-18-2006 07:55 PM

Re: Sorting of list containing tuples

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 <ronnyma@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

 All times are GMT. The time now is 05:24 AM.