Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Sorting (deeply) nested lists

Reply
Thread Tools

Sorting (deeply) nested lists

 
 
mamboknave@gmail.com
Guest
Posts: n/a
 
      03-02-2013
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!!
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      03-02-2013
http://www.velocityreviews.com/forums/(E-Mail Removed) 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) ] ]



 
Reply With Quote
 
 
 
 
mamboknave@gmail.com
Guest
Posts: n/a
 
      03-02-2013
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!
 
Reply With Quote
 
mamboknave@gmail.com
Guest
Posts: n/a
 
      03-02-2013
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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting list vs sorting vector boltar2003@boltar.world C++ 2 07-06-2010 09:40 AM
fired event Sorting which wasn't handled - sorting and SelectedIndexChanged Jason ASP .Net Web Controls 0 10-04-2006 02:19 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
Help with sorting lists of lists Chris Weisiger Perl Misc 7 10-14-2004 01:42 PM
Sorting lists of lists... JustSomeGuy C++ 0 06-17-2004 05:44 PM



Advertisments