Sorting (deeply) nested lists

Discussion in 'Python' started by mamboknave@gmail.com, Mar 2, 2013.

  1. Guest

    I cannot resolve this on my own. Need help, please...

    nestedTuples = [
    [ (L0t0e0, L0t0e1, L0t0e2), (L0t1e0, 2, L0t1e2), (L0t2e0, L0t2e1, L0t2e2) ],
    [ (L1t0e0, L1t0e1, L1t0e2), (L1t1e0, 0, L1t1e2), (L1t2e0, L1t2e1, L1t2e2) ],
    [ (L2t0e0, L2t0e1, L2t0e2), (L2t1e0, 1, L2t1e2), (L2t2e0, L2t2e1, L2t2e2) ] ]

    With LNtXeY I mean the element Y in the tuple X of the list N.

    How can I sort nestedTuples by, say, the 2nd element in the 2nd tuple of each list?

    The above should get sorted as :

    nestedTuples = [
    [ (L1t0e0, L1t0e1, L1t0e2), (L1t1e0, 0, L1t1e2), (L1t2e0, L1t2e1, L1t2e2) ],
    [ (L2t0e0, L2t0e1, L2t0e2), (L2t1e0, 1, L2t1e2), (L2t2e0, L2t2e1, L2t2e2) ],
    [ (L0t0e0, L0t0e1, L0t0e2), (L0t1e0, 2, L0t1e2), (L0t2e0, L0t2e1, L0t2e2) ] ]

    Thanks so much!!
     
    , Mar 2, 2013
    #1
    1. Advertising

  2. Peter Otten Guest

    wrote:

    > I cannot resolve this on my own. Need help, please...
    >
    > nestedTuples = [
    > [ (L0t0e0, L0t0e1, L0t0e2), (L0t1e0, 2, L0t1e2), (L0t2e0, L0t2e1, L0t2e2)
    > [ ], (L1t0e0, L1t0e1, L1t0e2), (L1t1e0, 0, L1t1e2), (L1t2e0, L1t2e1,
    > [ L1t2e2) ], (L2t0e0, L2t0e1, L2t0e2), (L2t1e0, 1, L2t1e2), (L2t2e0,
    > [ L2t2e1, L2t2e2) ] ]
    >
    > With LNtXeY I mean the element Y in the tuple X of the list N.
    >
    > How can I sort nestedTuples by, say, the 2nd element in the 2nd tuple of
    > each list?


    def getkey(item):
    return item[1][1]
    namedTuples.sort(key=getkey)

    You can also write this as

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

    > The above should get sorted as :
    >
    > nestedTuples = [
    > [ (L1t0e0, L1t0e1, L1t0e2), (L1t1e0, 0, L1t1e2), (L1t2e0, L1t2e1, L1t2e2)
    > [ ], (L2t0e0, L2t0e1, L2t0e2), (L2t1e0, 1, L2t1e2), (L2t2e0, L2t2e1,
    > [ L2t2e2) ], (L0t0e0, L0t0e1, L0t0e2), (L0t1e0, 2, L0t1e2), (L0t2e0,
    > [ L0t2e1, L0t2e2) ] ]
     
    Peter Otten, Mar 2, 2013
    #2
    1. Advertising

  3. Guest

    On Saturday, March 2, 2013 9:36:43 AM UTC-8, Peter Otten wrote:
    >
    > You can also write this as
    >
    > namedTuples.sort(key=lambda item: item[1][1])
    >


    That's exactly what I did before and got "IndexError: list index out of range".

    So, I thought my lambda was wrong and posted here.

    Now, having seen you reply, I dug deeper and... of course I had IndexError: the list was horribly empty!

    Thanks so much for replying so quickly, Peter!
     
    , Mar 2, 2013
    #3
  4. Guest

    On Saturday, March 2, 2013 9:36:43 AM UTC-8, Peter Otten wrote:
    >
    > You can also write this as
    >
    > namedTuples.sort(key=lambda item: item[1][1])
    >


    That's exactly what I did before and got "IndexError: list index out of range".

    So, I thought my lambda was wrong and posted here.

    Now, having seen you reply, I dug deeper and... of course I had IndexError: the list was horribly empty!

    Thanks so much for replying so quickly, Peter!
     
    , Mar 2, 2013
    #4
    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. Edward C. Jones

    Pickle vs. eval for deeply nested objects

    Edward C. Jones, Feb 18, 2004, in forum: Python
    Replies:
    0
    Views:
    510
    Edward C. Jones
    Feb 18, 2004
  2. Ori Y
    Replies:
    1
    Views:
    415
    Bob Ippolito
    Feb 28, 2004
  3. Kay Schluehr
    Replies:
    7
    Views:
    381
    Kay Schluehr
    Jan 20, 2006
  4. donn

    Walking deeply nested lists

    donn, Aug 28, 2010, in forum: Python
    Replies:
    11
    Views:
    632
  5. ccc31807
    Replies:
    1
    Views:
    145
    Jim Gibson
    Nov 7, 2013
Loading...

Share This Page