How to write this as a list comprehension?

Alain Ketterlin alain at dpt-info.u-strasbg.fr
Sat Jan 18 06:53:12 EST 2014


Piet van Oostrum <piet at vanoostrum.org> writes:

[...]
> 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]

[...]

> 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.

"let x = v in e" really is (lambda x:e)(v)

In your case:

[ (lambda na,ti : somefunc(ti[1],ti[2],ti[6],na))(name,localtime(then))
  for then,name in mylist ]

Warning: absolutely untested (not even syntax-checked).

You may also use *localtime(...) and keep underscores.

-- Alain.



More information about the Python-list mailing list