Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64330
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!news-1.dfn.de!news.dfn.de!news.informatik.hu-berlin.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Piet van Oostrum <piet@vanoostrum.org> |
| Newsgroups | comp.lang.python |
| Subject | Re: How to write this as a list comprehension? |
| Date | Sun, 19 Jan 2014 23:06:05 +0100 |
| Lines | 46 |
| Message-ID | <m2d2jn4ofm.fsf@cochabamba.vanoostrum.org> (permalink) |
| References | <m2sism42n8.fsf@cochabamba.vanoostrum.org> <CAGGBd_rC_9am48CptynB-uBOyGdzsTJwEo6R2ojKYjOtZibh=A@mail.gmail.com> <mailman.5697.1390094705.18130.python-list@python.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| X-Trace | individual.net 9najgMjW057KaRoZWjzYygXQGtpNiFQRthjEbQlpa8vUQVWATX |
| Cancel-Lock | sha1:0DhqDfs20phF1oGtGTSEa1kGreI= sha1:WwW+u+ODk2LUL/eRD2/lW0odOeA= |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (darwin) |
| Xref | csiph.com comp.lang.python:64330 |
Show key headers only | View raw
John Allsup <pydev@allsup.co> writes:
> 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++.
I'll stay with
labels = [somefunc(mn, day, wd, name)
for then, name in mylist
for _, mn, dy, _, _, _, wd, _, _ in [localtime(then)]]
where (sic!) the last part means as much as
where _, mn, dy, _, _, _, wd, _, _ = localtime(then)
I find the list comprehension preferable because it makes it more clear
that a new list is constructed from an existing list, something that is
not as immediately clear with the append construction.
--
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll 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