Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'read.': 0.03; 'from:addr:yahoo.co.uk': 0.04; 'subject:Python': 0.06; '"""': 0.07; 'element': 0.07; 'skip:l 60': 0.07; "'')": 0.09; 'item.': 0.09; 'lawrence': 0.09; 'name?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:language': 0.09; 'def': 0.12; 'language.': 0.14; '"def"': 0.16; '(none,': 0.16; '24,': 0.16; 'elem': 0.16; 'func': 0.16; 'grounds': 0.16; 'is).': 0.16; 'magic': 0.16; 'naming': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'shortcut': 0.16; 'sorting': 0.16; 'subject: \n ': 0.16; 'tuple': 0.16; 'index': 0.16; ':-)': 0.16; 'language': 0.16; 'wrote:': 0.18; 'voted': 0.19; 'examples': 0.20; 'fit': 0.20; '>>>': 0.22; 'example': 0.22; 'tests': 0.22; 'header:User-Agent:1': 0.23; 'skip:l 30': 0.24; 'mon,': 0.24; '---': 0.24; "i've": 0.25; 'source': 0.25; 'header:X-Complaints- To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'raise': 0.29; "doesn't": 0.30; 'skip:g 30': 0.30; 'subject:list': 0.30; 'work.': 0.31; 'code': 0.31; '(unless': 0.31; '>>>>': 0.31; 'constant': 0.31; "d'aprano": 0.31; 'relies': 0.31; 'slot': 0.31; 'steven': 0.31; 'way?': 0.31; 'allows': 0.31; 'but': 0.35; 'there': 0.35; 'method': 0.36; 'unit': 0.37; 'list': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'skip:i 50': 0.60; 'free': 0.61; 'viruses': 0.61; 'protection': 0.63; 'our': 0.64; 'more': 0.64; 'chance': 0.65; 'mar': 0.68; 'antivirus': 0.68; 'importantly,': 0.68; 'safe': 0.72; 'gain': 0.79; 'skip:_ 50': 0.83; 'subject:this': 0.83; 'missing.': 0.84; 'received:2': 0.84; 'sorry.': 0.91; 'wait,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list) Date: Mon, 24 Mar 2014 15:22:50 +0000 References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <10101874-2995-4acd-9851-989603f052e3@googlegroups.com> <532d5bd9$0$29994$c3e8da3$5496439d@news.astraweb.com> <87bnwv2a5v.fsf@elektro.pacujo.net> <87r45rzsbj.fsf@elektro.pacujo.net> <533043b7$0$29994$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-2-98-198-214.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 In-Reply-To: <533043b7$0$29994$c3e8da3$5496439d@news.astraweb.com> X-Antivirus: avast! (VPS 140324-0, 24/03/2014), Outbound message X-Antivirus-Status: Clean X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395674536 news.xs4all.nl 2856 [2001:888:2000:d::a6]:42530 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68883 On 24/03/2014 14:39, Steven D'Aprano wrote: > On Mon, 24 Mar 2014 23:53:12 +1100, Chris Angelico wrote: > >> On Mon, Mar 24, 2014 at 11:36 PM, Marko Rauhamaa >> wrote: >>>> def get_oneth_element_index(item): >>>> return item[1].index >>>> L.sort(key=get_oneth_element_index) >>>> >>>> Or do you just shortcut the whole thing by inlining it? >>>> >>>> L.sort(key=lambda item:item[1].index) >>> >>> I still prefer the "def" variant. It even allows you to clarify the >>> meaning of the tuple slot by using a nicer name. >> >> It's the index of element 1. What more do you need to know? Is it >> actually any help to give that a name? All you gain is a chance for the >> name, the purpose, and the functionality to come into disagreement. > > > > # Magic constants are wicked. Never use a constant without naming it. > ELEMENT_TO_USE_FOR_INDEXING_WHEN_SORTING_L = 1 > > # > # key func used when sorting L, returns item's 1th elem index method > # > # > # _get_oneth_element_index_to_use_as_keyfunc_when_sorting_L > # > # Steven D'Aprano > # 2014-03-25 > # 2014-03-25 > # 1 > # item to be sorted > # index method of the oneth element > # NameError # FIXME can this fail any other way? > def _get_oneth_element_index_to_use_as_keyfunc_when_sorting_L(item): > """Private key function for sorting list L. > > Returns the index method of element 1 of the given item. > > Example of use: > > >>> item = (None, '') > >>> _get_oneth_element_index_to_use_as_keyfunc_when_sorting_L(item) > > > Relies on global constant ELEMENT_TO_USE_FOR_INDEXING_WHEN_SORTING_L. > May raise NameError if that constant is missing. > > Warning: do not use this for anything else. > """ > return item[ELEMENT_TO_USE_FOR_INDEXING_WHEN_SORTING_L].index > > L.sort(key=_get_oneth_element_index_to_use_as_keyfunc_when_sorting_L) > del _get_oneth_element_index_to_use_as_keyfunc_when_sorting_L > # Better to be safe than sorry. > del ELEMENT_TO_USE_FOR_INDEXING_WHEN_SORTING_L > > Definitely being paid by the line :-) > One of the finest examples of extracting the urine I've ever read. Please keep up the good work. Ah but wait, down voted on the grounds that there are no unit tests and, far more importantly, it doesn't fit on one line (unless you use gg that is). There is also no proof that it's been committed to your source control system after going through its code review. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com