Temporary variables in list comprehensions
Jussi Piitulainen
jussi.piitulainen at helsinki.fi
Thu Apr 6 10:16:12 EDT 2017
Vincent Vande Vyvre writes:
> Le 06/04/17 à 14:25, Piet van Oostrum a écrit :
>> Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
>>
>>> Suppose you have an expensive calculation that gets used two or more
>>> times in a loop. The obvious way to avoid calculating it twice in an
>>> ordinary loop is with a temporary variable:
>>>
>>> result = []
>>> for x in data:
>>> tmp = expensive_calculation(x)
>>> result.append((tmp, tmp+1))
>>>
>>>
>>> But what if you are using a list comprehension? Alas, list comps
>>> don't let you have temporary variables, so you have to write this:
>>>
>>>
>>> [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data]
>>>
>>>
>>> Or do you? ... no, you don't!
>>>
>>>
>>> [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]]
>>>
>>>
>>> I can't decide whether that's an awesome trick or a horrible hack...
>> 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!
>
> With two passes
>
> e = [expensive_calculation(x) for x in data]
> final = [(x, y+1) for x, y in zip(e, e)]
>
> Vincent
Imagine some crazy combinatory question - how many ways can one choose
two subsets of the ten decimal digits so that the size of the first is
the minimum of the second and the size of the second is the maximum of
the first _or_ the minima and maxima of the two are the same?
Comprehensions lend themselves readily to such explorations. It happens
that some expensively computed value is needed twice, like the minima
and maxima of the two combinations in this exercise (because this
exercise was carefully crafted to be just so, but anyway), and then it
saves time to do the computations once: let the values have names.
from itertools import combinations as choose
print(sum(1 for m in range(1,10) for n in range(1,10)
for a in choose(range(1,10), m)
for b in choose(range(1,10), n)
if ((len(a) == min(b) and len(b) == max(a)) or
(min(a) == min(b) and max(a) == max(b)))))
print(sum(1 for m in range(1,10) for n in range(1,10)
for a in choose(range(1,10), m)
for b in choose(range(1,10), n)
for lena, mina, maxa in [[len(a), min(a), max(a)]]
for lenb, minb, maxb in [[len(b), min(b), max(b)]]
if ((lena == minb and lenb == maxa) or
(mina == minb and maxa == maxb))))
I realized afterwards that the sizes, len(a) and len(b), already had
names, m and n, and were only used once in the condition anyway, but let
that illustrate the point: this kind of expression lends itself to
analysis and modification, which is what one wants in explorative code.
(But the "for x in [foo(u,w)]" works, so, shrug, I guess? I'd welcome a
proper let construction, but then I find that I can live without.)
More information about the Python-list
mailing list