Sorting lists

Discussion in 'Python' started by asc, Nov 17, 2008.

  1. asc

    asc Guest

    Hi all,
    I have a problem and I'm not sure whether sort() can help me.
    I understand that if I have a list; say L = ['b', 'c', 'a']
    I can use L.sort() and I will then have; L = ['a', 'b', 'c']

    But my problem is this. I have a list, that contains a number of
    embeded lists;
    e.g. L2 = [['something', 'bb'], ['somethingElse', 'cc'],
    ['anotherThing', 'aa']]
    Now I want to sort this list by the second item of each sublist. So
    the outcome I would like is;
    L2 = [['anotherThing', 'aa'], ['something', 'bb'], ['somethingElse',
    'cc']]

    Is there a way I can use sort for this? Or am I going to have write a
    custom function to sort this out?

    Many thanks.
    asc, Nov 17, 2008
    #1
    1. Advertising

  2. asc

    John Machin Guest

    On Nov 17, 8:56 pm, asc <> wrote:

    > But my problem is this. I have a list, that contains a number of
    > embeded lists;
    > e.g. L2 = [['something', 'bb'], ['somethingElse', 'cc'],
    > ['anotherThing', 'aa']]
    > Now I want to sort this list by the second item of each sublist. So
    > the outcome I would like is;
    > L2 = [['anotherThing', 'aa'], ['something', 'bb'], ['somethingElse',
    > 'cc']]
    >
    > Is there a way I can use sort for this?


    Yes. Read the manual. Look for the key argument. You need to make up a
    function (using def or lambda) that will pluck the desired sort-key
    out of a sub-list.
    John Machin, Nov 17, 2008
    #2
    1. Advertising

  3. asc

    Chris Rebert Guest

    On Mon, Nov 17, 2008 at 1:56 AM, asc <> wrote:
    > Hi all,
    > I have a problem and I'm not sure whether sort() can help me.
    > I understand that if I have a list; say L = ['b', 'c', 'a']
    > I can use L.sort() and I will then have; L = ['a', 'b', 'c']
    >
    > But my problem is this. I have a list, that contains a number of
    > embeded lists;
    > e.g. L2 = [['something', 'bb'], ['somethingElse', 'cc'],
    > ['anotherThing', 'aa']]
    > Now I want to sort this list by the second item of each sublist. So
    > the outcome I would like is;
    > L2 = [['anotherThing', 'aa'], ['something', 'bb'], ['somethingElse',
    > 'cc']]
    >
    > Is there a way I can use sort for this? Or am I going to have write a
    > custom function to sort this out?


    You use the `key` argument to .sort():

    L2.sort(key=lambda item: item[1])

    This sorts L2 by the result of applying the `key` function to each of
    the items. Behind the scenes, I believe it does a Schwartzian
    transform.

    Cheers,
    Chris
    --
    Follow the path of the Iguana...
    http://rebertia.com

    >
    > Many thanks.
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Chris Rebert, Nov 17, 2008
    #3
  4. asc

    Guest

    Chris Rebert:
    > You use the `key` argument to .sort():
    > L2.sort(key=lambda item: item[1])


    I like the lambda because it's a very readable solution that doesn't
    require the std lib and it doesn't force the programmer (and the
    person that reads the code) to learn yet another thing/function.

    But I can suggest to also look for operator.itemgetter.

    ------------
    In C# a syntax for lambdas may become a little shorter, but the
    parsing may become less easy:
    L2.sort(key = sub => sub[1])

    In Mathematica they use the & to define lambdas, and #1, #2, etc, to
    denote the arguments.

    Bye,
    bearophile
    , Nov 17, 2008
    #4
  5. asc

    Terry Reedy Guest

    wrote:
    > Chris Rebert:
    >> You use the `key` argument to .sort():
    >> L2.sort(key=lambda item: item[1])

    >
    > I like the lambda because it's a very readable solution that doesn't
    > require the std lib and it doesn't force the programmer (and the
    > person that reads the code) to learn yet another thing/function.
    >
    > But I can suggest to also look for operator.itemgetter.


    Since itemgetter is builtin, it will probably run faster, though the
    O(nlogn) time for sorting will override the O(n) time for key calls.
    Terry Reedy, Nov 17, 2008
    #5
  6. On Mon, 17 Nov 2008 14:36:03 -0500, Terry Reedy wrote:

    > wrote:
    >> Chris Rebert:
    >>> You use the `key` argument to .sort(): L2.sort(key=lambda item:
    >>> item[1])

    >>
    >> I like the lambda because it's a very readable solution that doesn't
    >> require the std lib and it doesn't force the programmer (and the person
    >> that reads the code) to learn yet another thing/function.
    >>
    >> But I can suggest to also look for operator.itemgetter.

    >
    > Since itemgetter is builtin, it will probably run faster, though the
    > O(nlogn) time for sorting will override the O(n) time for key calls.


    Well, eventually, for large enough lists, sure. But if the constants are
    sufficiently different, the key calls may be the bottleneck. O(foo)
    analysis is valuable, but it isn't the full story. A fast enough O(n**2)
    algorithm might be preferable to a slow O(log n) algorithm for any data
    you're interested in.

    To give an extreme example, suppose your itemgetter function (not the
    Python built-in!) had to query some remote database over the internet. It
    might be O(n) but the multiplicative constant would be so large that the
    time taken to get items far dominates the time to sort the list, unless
    the list is *seriously* huge:

    (Say) sorting takes O(n*log n) with multiplicative constant of 1e-5.
    Item getting takes O(n) with multiplicative constant of 1.

    Then the time taken to sort doesn't become larger than the time taken to
    get the items until approximately:

    1e-5*n*log(n) > n
    1e-5*log(n) > 1
    log(n) > 1e5
    n > 2**100000

    which is pretty big... for any reasonable-sized list, the O(n) part
    dominates despite the asymptotic behaviour being O(n*log n).



    --
    Steven
    Steven D'Aprano, Nov 18, 2008
    #6
    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. JustSomeGuy

    Sorting lists of lists...

    JustSomeGuy, Jun 17, 2004, in forum: C++
    Replies:
    0
    Views:
    300
    JustSomeGuy
    Jun 17, 2004
  2. Jon Slaughter

    lists of lists

    Jon Slaughter, Dec 13, 2004, in forum: C++
    Replies:
    4
    Views:
    399
    Buster
    Dec 13, 2004
  3. Charlotte Henkle

    Counter for items in lists in lists?

    Charlotte Henkle, Sep 25, 2004, in forum: Python
    Replies:
    8
    Views:
    385
    Charlotte Henkle
    Sep 26, 2004
  4. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    379
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  5. Chris Weisiger

    Help with sorting lists of lists

    Chris Weisiger, Oct 14, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    117
    Tad McClellan
    Oct 14, 2004
Loading...

Share This Page