Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40343

Re: Sorting (deeply) nested lists

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'say,': 0.05; 'list?': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tuple': 0.09; 'def': 0.10; 'item:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.17; 'element': 0.17; 'subject:) ': 0.20; 'sort': 0.21; 'own.': 0.22; 'header :User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.28; 'help,': 0.32; 'subject:lists': 0.32; 'to:addr:python-list': 0.33; 'list': 0.35; 'received:org': 0.36; 'should': 0.36; 'subject: (': 0.36; 'resolve': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'email addr:gmail.com': 0.63
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Sorting (deeply) nested lists
Date Sat, 02 Mar 2013 18:36:43 +0100
Organization None
References <c4703b9f-44fd-4a6b-a600-b94de993bb4e@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084963f.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2783.1362245815.2939.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1362245815 news.xs4all.nl 6922 [2001:888:2000:d::a6]:34262
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:40343

Show key headers only | View raw


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) ] ]

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web