Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #40338 > unrolled thread
| Started by | mamboknave@gmail.com |
|---|---|
| First post | 2013-03-02 09:18 -0800 |
| Last post | 2013-03-02 09:49 -0800 |
| Articles | 4 — 2 participants |
Back to article view | Back to comp.lang.python
Sorting (deeply) nested lists mamboknave@gmail.com - 2013-03-02 09:18 -0800
Re: Sorting (deeply) nested lists Peter Otten <__peter__@web.de> - 2013-03-02 18:36 +0100
Re: Sorting (deeply) nested lists mamboknave@gmail.com - 2013-03-02 09:49 -0800
Re: Sorting (deeply) nested lists mamboknave@gmail.com - 2013-03-02 09:49 -0800
| From | mamboknave@gmail.com |
|---|---|
| Date | 2013-03-02 09:18 -0800 |
| Subject | Sorting (deeply) nested lists |
| Message-ID | <c4703b9f-44fd-4a6b-a600-b94de993bb4e@googlegroups.com> |
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!!
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-03-02 18:36 +0100 |
| Message-ID | <mailman.2783.1362245815.2939.python-list@python.org> |
| In reply to | #40338 |
mamboknave@gmail.com 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) ] ]
[toc] | [prev] | [next] | [standalone]
| From | mamboknave@gmail.com |
|---|---|
| Date | 2013-03-02 09:49 -0800 |
| Message-ID | <a6b8d573-495d-4c18-bfb0-975ccf44d8a2@googlegroups.com> |
| In reply to | #40343 |
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!
[toc] | [prev] | [next] | [standalone]
| From | mamboknave@gmail.com |
|---|---|
| Date | 2013-03-02 09:49 -0800 |
| Message-ID | <mailman.2784.1362246550.2939.python-list@python.org> |
| In reply to | #40343 |
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!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web