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


Groups > comp.lang.python > #64333

Re: How to write this as a list comprehension?

Newsgroups comp.lang.python
Subject Re: How to write this as a list comprehension?
References <m2sism42n8.fsf@cochabamba.vanoostrum.org> <mailman.5671.1390034188.18130.python-list@python.org> <ff9f6177-b41c-446c-9f57-dbc9b6488b6f@googlegroups.com> <qotha91nutu.fsf@ruuvi.it.helsinki.fi>
From "Rhodri James" <rhodri@wildebst.org.uk>
Organization The Wildebestiary
Message-ID <op.w9yj2ok55079vu@gnudebeest> (permalink)
Date 2014-01-19 23:41 +0000

Show all headers | View raw


On Sat, 18 Jan 2014 16:00:45 -0000, Jussi Piitulainen  
<jpiitula@ling.helsinki.fi> wrote:

> Rustom Mody writes:
>
>> On Saturday, January 18, 2014 2:06:29 PM UTC+5:30, Peter Otten wrote:
>>
>> > What would a list-comp with `let` or `where` look like? Would it
>> > win the beauty contest against the loop?
>>
>> For me this is neat
>>
>> [somefunc(mn,day,wd,name) for (then, name) in mylist let  
>> (_,mn,dy,_,_,_,wd,_,_) = localtime(then)]
>>
>> Others may not find it so!

Count me firmly in the "others" camp.  It looks ugly, it flows appallingly  
badly as English, and its only going to get worse as you pile in more  
variables and expressions.  -100 from me.

>> See it across > 1 line (as I guess it will come after being posted!)
>> and its not so neat.
>
> I would write that on three lines anyway, properly indented:
>
>   [ somefunc(mn,day,wd,name)
>     for (then, name) in mylist
>     let (_,mn,dy,_,_,_,wd,_,_) = localtime(then) ]
>
> It could be made to use existing keywords:
>
>   [ somefunc(mn,day,wd,name)
>     for (then, name) in mylist
>     with localtime(then) as (_,mn,dy,_,_,_,wd,_,_) ]

Better, in that it's readable.  It's still storing up trouble, though.

Seriously, what's inelegant about this?

def meaningful_name(then, name):
     _,mn,dy,_,_,_,wd,_,_ = localtime(then)
     return somefunc(mn, dy, wd, name)

...

     [meaningful_name(then, name) for (then, name) in mylist]

I assume there's some good reason you didn't want somefunc() to do the  
localtime() itself?

-- 
Rhodri James *-* Wildebeest Herder to the Masses

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