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


Groups > comp.lang.python > #64272

Re: How to write this as a list comprehension?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <pydev@allsup.co>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'elegant': 0.07; 'expressions': 0.07; 'string': 0.09; 'dan': 0.09; 'lines.': 0.09; 'subject:skip:c 10': 0.09; 'sucks': 0.09; 'things,': 0.09; 'subject:How': 0.10; 'def': 0.12; 'jan': 0.12; 'assignments.': 0.16; 'c++.': 0.16; 'determining': 0.16; 'does,': 0.16; 'idea:': 0.16; 'mylist': 0.16; 'name)': 0.16; 'presume': 0.16; 'readability': 0.16; 'readable': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'import': 0.22; 'header:User-Agent:1': 0.23; 'question': 0.24; 'van': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'subject:list': 0.30; 'code': 0.31; 'comments': 0.31; 'easier': 0.31; 'fri,': 0.33; "i'd": 0.34; 'advice': 0.35; 'agree': 0.35; 'but': 0.35; 'version': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'list': 0.37; 'performance': 0.37; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'issue': 0.38; 'pm,': 0.38; 'little': 0.38; 'explain': 0.39; 'to:addr:python.org': 0.39; 'then,': 0.60; 'john': 0.61; 'name': 0.63; 'such': 0.63; 'become': 0.64; 'great': 0.65; 'received:212.227.17': 0.68; 'physical': 0.72; 'subject:this': 0.83
Date Sun, 19 Jan 2014 01:24:56 +0000
From John Allsup <pydev@allsup.co>
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
MIME-Version 1.0
To python-list@python.org
Subject Re: How to write this as a list comprehension?
References <m2sism42n8.fsf@cochabamba.vanoostrum.org> <CAGGBd_rC_9am48CptynB-uBOyGdzsTJwEo6R2ojKYjOtZibh=A@mail.gmail.com>
In-Reply-To <CAGGBd_rC_9am48CptynB-uBOyGdzsTJwEo6R2ojKYjOtZibh=A@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:gXt7IWMneqQ9H4xrSGhNgQ38DmjP3HW6bXA6lnEfRKn nLUkdwWXtY9iGfvvZWWXJq5BWJmDhw6GPZ7Dt6KuSI+lJZGNP8 Hbsbh18s1CFHUqsTWKUOnRswE1bSmHwp77zJ5nEKapOktyhv/c e9vx1J9QUx94nRCOe1WtPNJ8uMyzQNbfBgZ1ITc248hSkYoPIc o4qnVvLfka1JZW7JSNrDWGhZbaGGMVN1XZbKc6G5SZ74DuAGf+ TIGCJfQ6+2iqJDdDaXUg4eVZqRyRsdZTTtI2VoH+KZauu3Uxi4 /b0+qkTAd+rUPXE6aX0mGZd3865A2jC67yF1NF3MdRaJ+8ReAS IABYoo1v6/ye/z91dyQHJ1XWdR4xZodBaDic5FcBO
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5697.1390094705.18130.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1390094705 news.xs4all.nl 2938 [2001:888:2000:d::a6]:57182
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64272

Show key headers only | View raw


Hi,

I'd agree with the advice that it's not the best idea: readability sucks 
here, but consider the following:


import time

def somefunc(a,b,c,d): # dummy function
     return "{} - {} - {} : {}".format(a,b,c,d)
l = [(time.time(),"name {}".format(n)) for n in range(100)] # dummy data

# the line in question
labels = [somefunc(*(lambda t,n: 
(t.tm_mon,t.tm_mday,t.tm_wday,n))(time.localtime(x[0]),x[1])) for x in l]


print(labels) # just to see the result


If you find that hard to decipher, the consider the maintainability of 
code you write that uses such comprehensions.  You need to include 
comments that explain what this does, and it is easier to write a 
longhand version using .append() and variable assignments.  I presume
performance won't be an issue determining the right approach, since then 
you'd be using C or C++.

John

On 17/01/2014 23:49, Dan Stromberg wrote:
> On Fri, Jan 17, 2014 at 3:19 PM, Piet van Oostrum <piet@vanoostrum.org> wrote:
>> Hi,
>>
>> I am looking for an elegant way to write the following code as a list
>> comprehension:
>>
>> labels = []
>> for then, name in mylist:
>>      _, mn, dy, _, _, _, wd, _, _ = localtime(then)
>>      labels.append(somefunc(mn, day, wd, name))
>
> My recomendation: Don't use a list comprehension.  List comprehensions
> and generator expressions are great for quick little things, but
> become less readable when you have to string them over multiple
> physical lines.
>
>> labels = [somefunc(mn, day, wd, name)
>>              for then, name in mylist
>>              for _, mn, dy, _, _, _, wd, _, _ in [localtime(then)]]

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


Thread

How to write this as a list comprehension? Piet van Oostrum <piet@vanoostrum.org> - 2014-01-18 00:19 +0100
  Re: How to write this as a list comprehension? Dan Stromberg <drsalists@gmail.com> - 2014-01-17 15:49 -0800
  Re: How to write this as a list comprehension? Rustom Mody <rustompmody@gmail.com> - 2014-01-17 19:25 -0800
    Re: How to write this as a list comprehension? Piet van Oostrum <piet@vanoostrum.org> - 2014-01-18 11:00 +0100
  Re: How to write this as a list comprehension? Peter Otten <__peter__@web.de> - 2014-01-18 09:36 +0100
    Re: How to write this as a list comprehension? Rustom Mody <rustompmody@gmail.com> - 2014-01-18 07:20 -0800
      Re: How to write this as a list comprehension? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-01-18 18:00 +0200
        Re: How to write this as a list comprehension? "Rhodri James" <rhodri@wildebst.org.uk> - 2014-01-19 23:41 +0000
          Re: How to write this as a list comprehension? Piet van Oostrum <piet@vanoostrum.org> - 2014-01-20 12:02 +0100
            Re: How to write this as a list comprehension? Rustom Mody <rustompmody@gmail.com> - 2014-01-20 03:47 -0800
  Re: How to write this as a list comprehension? matej@ceplovi.cz (Matěj Cepl) - 2014-01-18 11:57 +0100
  Re: How to write this as a list comprehension? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-01-18 12:53 +0100
    Re: How to write this as a list comprehension? Piet van Oostrum <piet@vanoostrum.org> - 2014-01-18 18:40 +0100
  Re: How to write this as a list comprehension? John Allsup <pydev@allsup.co> - 2014-01-19 01:24 +0000
    Re: How to write this as a list comprehension? Piet van Oostrum <piet@vanoostrum.org> - 2014-01-19 23:06 +0100

csiph-web