sorting list of tuples by second (third...) tuple item

Discussion in 'Python' started by Giovanni Toffoli, Feb 16, 2007.

  1. 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:
    http://mail.python.org/pipermail/python-list/2002-February/128438.html

    <<<
    def SortOnItem(mylist, index):
    templist = [ (line[index], line) for line in mylist ]
    templist.sort()
    return [ line[1:] for line in templist ]

    What this does is build a separate list containing a tuple of the
    element that you want to sort on, and the entire line, sorts that
    list (by the first element, of course), and then strips that first
    element off ..
    >>>


    It seems to me that the tuples aren't sorted only by the first element but,
    I suppose, other elements are also used if needed to discriminate.
    In some cases I got some exceptions when an element of the tuple, other than
    the first, didn't admit comparison.
    In these cases I had to use an ad hoc comparison function.

    Regards, Giovanni Toffoli
     
    Giovanni Toffoli, Feb 16, 2007
    #1
    1. Advertising

  2. Giovanni Toffoli a écrit :
    > 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:
    > http://mail.python.org/pipermail/python-list/2002-February/128438.html
    >
    > <<<
    > def SortOnItem(mylist, index):
    > templist = [ (line[index], line) for line in mylist ]
    > templist.sort()
    > return [ line[1:] for line in templist ]
    >
    > What this does is build a separate list containing a tuple of the
    > element that you want to sort on, and the entire line, sorts that
    > list (by the first element, of course), and then strips that first
    > element off ..


    It's the "decorate/sort/undecorate" pattern. You may also google for
    "schwarzian transform"

    >>>>

    >
    > It seems to me that the tuples aren't sorted only by the first element
    > but, I suppose, other elements are also used if needed to discriminate.


    Yes. When compared, tuples first compare on the first element, then on
    the second etc...

    > In some cases I got some exceptions when an element of the tuple, other
    > than the first, didn't admit comparison.
    > In these cases I had to use an ad hoc comparison function.


    Did you try the sorted() function ? Used with operator.itemgetter as the
    'key' argument, it may do the trick.
     
    Bruno Desthuilliers, Feb 16, 2007
    #2
    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. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    868
    Antoon Pardon
    Mar 30, 2005
  2. beginner
    Replies:
    27
    Views:
    972
    Marc 'BlackJack' Rintsch
    Jul 26, 2007
  3. Davy
    Replies:
    3
    Views:
    1,917
    Wildemar Wildenburger
    Nov 7, 2007
  4. Gnarlodious

    Transform two tuples item by item

    Gnarlodious, Dec 20, 2011, in forum: Python
    Replies:
    3
    Views:
    174
    Gnarlodious
    Dec 20, 2011
  5. Jon Reyes
    Replies:
    18
    Views:
    271
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page