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


Groups > comp.lang.python > #64218

Re: How to write this as a list comprehension?

From Peter Otten <__peter__@web.de>
Subject Re: How to write this as a list comprehension?
Date 2014-01-18 09:36 +0100
Organization None
References <m2sism42n8.fsf@cochabamba.vanoostrum.org>
Newsgroups comp.lang.python
Message-ID <mailman.5671.1390034188.18130.python-list@python.org> (permalink)

Show all headers | View raw


Piet van Oostrum 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))
> 
> So mylist is a list of tuples, the first member of the tuple is a time
> (as epoch offset) and I neeed to apply a function on some fields of the
> localtime of it.
> 
> I could define a auxiliary function like:
> 
> def auxfunc(then, name):
>     _, mn, dy, _, _, _, wd, _, _ = localtime(then)
>     return somefunc(mn, day, wd, name)
> 
> and then use
> [auxfunc(then, name) for then, name in mylist]
> 
> or even
> [auxfunc(*tup) for tup in mylist]
> 
> But defining the auxfunc takes away the elegance of a list comprehension.
> I would like to integrate the unpacking of localtime() and calling
> somefunc within the list comprehension, but I don't see a simple way to do
> that.
> 
> somefunc(mn, day, wd, name) for _, mn, dy, _, _, _, wd, _, _ in
> [localtime(then)] (i.e. using a list comprehension on a one element list
> to do the variable shuffling) works but I don't find that very elegant.
> 
> labels = [somefunc(mn, day, wd, name)
>             for then, name in mylist
>             for _, mn, dy, _, _, _, wd, _, _ in [localtime(then)]]
> 
> Python misses a 'where' or 'let'-like construction as in Haskell.
> 
> Anybody has a more elegant solution?

Options I can think of: 

You could do it in two steps...

time_name_pairs = ((localtime(then), name) for then, name in mylist)
labels = [somefunc(t.tm_mon, t.tm_mday, t.tm_wday, name) 
          for t, name in time_name_pairs]

...or you could inline the helper function...

mon_mday_wday = operator.attrgetter("tm_mon", "tm_day", "tm_wday")
labels = [somefunc(*mon_mday_wday(localtime(then)), name=name) 
          for then, name in mylist]

-- but both seem less readable than the classical for-loop.

What would a list-comp with `let` or `where` look like? Would it win the 
beauty contest against the loop?

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