Temporary variables in list comprehensions
Roel Schroeven
roel at roelschroeven.net
Fri Apr 7 17:29:49 EDT 2017
Lele Gaifax schreef op 6/04/2017 20:07:
> Piet van Oostrum <piet-l at pietvanoostrum.com> writes:
>
>> It is a poor man's 'let'. It would be nice if python had a real 'let'
>> construction. Or for example:
>>
>> [(tmp, tmp + 1) for x in data with tmp = expensive_calculation(x)]
>>
>> Alas!
>
> It would be nice indeed!
>
> Or even
>
> [(tmp, tmp + 1) for x in data
> with expensive_calculation(x) as tmp
> if tmp is not None]
>
Perhaps this:
[(tmp, tmp + 1) for tmp in
(expensive_calculation(x) for x in data)
if tmp is not None]
A bit less elegant, but works right now.
--
Roel Schroeven
More information about the Python-list
mailing list