Sorting of list containing tuples

Discussion in 'Python' started by Ronny Mandal, May 18, 2006.

  1. Ronny Mandal

    Ronny Mandal Guest

    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
    Ronny Mandal, May 18, 2006
    #1
    1. Advertising

  2. Ronny Mandal

    Paul Rubin Guest

    Ronny Mandal <> 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])
    Paul Rubin, May 18, 2006
    #2
    1. Advertising

  3. Ronny Mandal

    Ronny Mandal Guest

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

    On 18 May 2006 12:38:55 -0700, Paul Rubin
    <http://> wrote:

    >Ronny Mandal <> 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, May 18, 2006
    #3
  4. Ronny Mandal

    Dave Hansen Guest

    On Thu, 18 May 2006 21:29:59 +0200 in comp.lang.python, Ronny Mandal
    <> 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.
    Dave Hansen, May 18, 2006
    #4
  5. 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)
    Wojciech =?iso-8859-2?Q?Mu=B3a?=, May 18, 2006
    #5
  6. Ronny Mandal

    Guest

    >>> 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, 2006
    #6
  7. On Thu, May 18, 2006 at 12:38:55PM -0700, Paul Rubin wrote:
    > Ronny Mandal <> 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, May 18, 2006
    #7
  8. 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 <> 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
    Christoph Haas, May 18, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giovanni Toffoli
    Replies:
    1
    Views:
    507
    Bruno Desthuilliers
    Feb 16, 2007
  2. Replies:
    5
    Views:
    549
    Thomas J. Gritzan
    Oct 6, 2006
  3. tuples within tuples

    , Oct 26, 2007, in forum: Python
    Replies:
    12
    Views:
    566
    Dennis Lee Bieber
    Oct 27, 2007
  4. xera121
    Replies:
    8
    Views:
    716
    lolmc
    Sep 30, 2009
  5. Jon Reyes
    Replies:
    18
    Views:
    224
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page