Re: Temporary variables in list comprehensions

2017-04-11 Thread Piet van Oostrum
Vincent Vande Vyvre writes: > final = [(x, y+1) for x, y in zip(e, e)] final = [(x, x+1) for x in e] -- Piet van Oostrum WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] --

Re: Temporary variables in list comprehensions

2017-04-07 Thread Jussi Piitulainen
Roel Schroeven writes: > Lele Gaifax schreef op 6/04/2017 20:07: >> Piet van Oostrum 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 =

Re: Temporary variables in list comprehensions

2017-04-07 Thread Roel Schroeven
Lele Gaifax schreef op 6/04/2017 20:07: Piet van Oostrum 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!

Re: Temporary variables in list comprehensions

2017-04-06 Thread Tim Chase
On 2017-04-06 14:56, Vincent Vande Vyvre wrote: > With two passes > > e = [expensive_calculation(x) for x in data] > final = [(x, y+1) for x, y in zip(e, e)] Using a generator it can be done in one pass: final = [ (value, tmp, tmp+1) for value, tmp in ( (x,

Re: Temporary variables in list comprehensions

2017-04-06 Thread Lele Gaifax
Piet van Oostrum 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

Re: Temporary variables in list comprehensions

2017-04-06 Thread Jussi Piitulainen
Vincent Vande Vyvre writes: > Le 06/04/17 à 14:25, Piet van Oostrum a écrit : >> Steven D'Aprano 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 >>>

Re: Temporary variables in list comprehensions

2017-04-06 Thread Vincent Vande Vyvre
Le 06/04/17 à 14:25, Piet van Oostrum a écrit : Steven D'Aprano 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:

Re: Temporary variables in list comprehensions

2017-04-06 Thread Piet van Oostrum
Steven D'Aprano 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: >

Re: Temporary variables in list comprehensions

2017-04-02 Thread breamoreboy
On Sunday, April 2, 2017 at 1:08:17 AM UTC+1, Robert L. wrote: > I don't believe in western morality, i.e. don't kill civilians or children > The only way to fight a moral war is the Jewish way: Destroy their holy sites. > Kill men, women, and children (and cattle). --- Rabbi Manis Friedman >

Re: Temporary variables in list comprehensions

2017-04-02 Thread Robert L.
On 1/8/2017, Steven D'Aprano wrote: > 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) >

Re: Temporary variables in list comprehensions

2017-04-01 Thread Chris Angelico
On Sun, Apr 2, 2017 at 11:53 AM, Steve D'Aprano wrote: > Robert, I've asked you once to stop posting anti-Semitic signatures in your > posts. You've now posted four times, and each one has included racist > material in the signature. > > You are welcome to participate

Re: Temporary variables in list comprehensions

2017-04-01 Thread Steve D'Aprano
Robert, I've asked you once to stop posting anti-Semitic signatures in your posts. You've now posted four times, and each one has included racist material in the signature. You are welcome to participate here if you discuss Python, or even to discuss general programming techniques, but if you

Re: Temporary variables in list comprehensions

2017-01-10 Thread Steven D'Aprano
On Tuesday 10 January 2017 00:12, Antoon Pardon wrote: > Op 09-01-17 om 04:53 schreef Steven D'Aprano: >> 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:

Re: Temporary variables in list comprehensions

2017-01-10 Thread Paul Moore
On Monday, 9 January 2017 03:53:37 UTC, Steven D'Aprano wrote: > Suppose you have an expensive calculation that gets used two or more times > in a loop. [...] > [(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

Re: Temporary variables in list comprehensions

2017-01-10 Thread jmp
On 01/09/2017 04:53 AM, Steven D'Aprano wrote: 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)

Re: Temporary variables in list comprehensions

2017-01-09 Thread Serhiy Storchaka
On 09.01.17 12:46, Paul Rubin wrote: Serhiy Storchaka writes: gen = (expensive_calculation(x) for x in data) result = [(tmp, tmp + 1) for tmp in gen] result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)] Yes, of course, but only in the case of

Re: Temporary variables in list comprehensions

2017-01-09 Thread Paul Rubin
Ben Bacarisse writes: > [(lambda tmp: (tmp, tmp+1))(expensive_calculation(x)) for x in data] Nice. The Haskell "let" expression is implemented as syntax sugar for that, I believe. -- https://mail.python.org/mailman/listinfo/python-list

Re: Temporary variables in list comprehensions

2017-01-09 Thread Tim Chase
On 2017-01-09 13:16, Paul Rubin wrote: > Tim Chase writes: > >> result = [(tmp, tmp+1) for tmp in map(expensive_calculation, > >> data)] > > > > As charmingly expressive as map() is, the wildly different > > behavior in py3 (it's a generator that evaluates lazily)

Re: Temporary variables in list comprehensions

2017-01-09 Thread Ben Bacarisse
Steven D'Aprano 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: >

Re: Temporary variables in list comprehensions

2017-01-09 Thread Paul Rubin
Tim Chase writes: >> result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)] > > As charmingly expressive as map() is, the wildly different behavior in > py3 (it's a generator that evaluates lazily) vs py2 (it consumes the > entire iterable in one go)

Re: Temporary variables in list comprehensions

2017-01-09 Thread Christian Gollwitzer
Am 09.01.17 um 04:53 schrieb Steven D'Aprano: 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... I think this is quite clear, and a useful feature, only that Python makes it

Re: Temporary variables in list comprehensions

2017-01-09 Thread Antonio Caminero Garcia
On Sunday, January 8, 2017 at 7:53:37 PM UTC-8, Steven D'Aprano wrote: > 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

Re: Temporary variables in list comprehensions

2017-01-09 Thread Tim Chase
On 2017-01-09 04:59, Rustom Mody wrote: > What happens when the expensive is on an inner generator? > Something like: > > [expensive₂(y) for x in data for y in foo(x)] > > [The ₂ representing the 2 or more occurrences in Steven's eg] Well, if I understand your question correctly, the goal

Re: Temporary variables in list comprehensions

2017-01-09 Thread Antoon Pardon
Op 09-01-17 om 04:53 schreef Steven D'Aprano: > 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 =

Re: Temporary variables in list comprehensions

2017-01-09 Thread Rustom Mody
On Monday, January 9, 2017 at 5:54:15 PM UTC+5:30, Tim Chase wrote: > On 2017-01-09 02:46, Paul Rubin wrote: > > > gen = (expensive_calculation(x) for x in data) > > > result = [(tmp, tmp + 1) for tmp in gen] > > > > result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)] > > As

Re: Temporary variables in list comprehensions

2017-01-09 Thread Tim Chase
On 2017-01-09 02:46, Paul Rubin wrote: > > gen = (expensive_calculation(x) for x in data) > > result = [(tmp, tmp + 1) for tmp in gen] > > result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)] As charmingly expressive as map() is, the wildly different behavior in py3 (it's a

Re: Temporary variables in list comprehensions

2017-01-09 Thread Paul Rubin
Serhiy Storchaka writes: > gen = (expensive_calculation(x) for x in data) > result = [(tmp, tmp + 1) for tmp in gen] result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)] -- https://mail.python.org/mailman/listinfo/python-list

Re: Temporary variables in list comprehensions

2017-01-09 Thread Paul Rubin
Steven D'Aprano writes: > [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data] def memoize(f): cache = {} def m(x): if x in cache: return cache[x] a = f(x)

Re: Temporary variables in list comprehensions

2017-01-09 Thread Serhiy Storchaka
On 09.01.17 05:53, Steven D'Aprano wrote: 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)

Re: Temporary variables in list comprehensions

2017-01-08 Thread Rustom Mody
On Monday, January 9, 2017 at 10:19:31 AM UTC+5:30, Steven D'Aprano wrote: > On Monday 09 January 2017 15:09, Chris Angelico wrote: > > > On Mon, Jan 9, 2017 at 2:53 PM, Steven D'Aprano wrote: > >> [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]] > >> > >> > >> I can't decide

Re: Temporary variables in list comprehensions

2017-01-08 Thread Chris Angelico
On Mon, Jan 9, 2017 at 3:49 PM, Steven D'Aprano wrote: > Helper functions are good. Helper functions that are only used > *once* are a code smell. *LOTS* of helper functions that are only used once > are > a sign that something is horrible, and it might just

Re: Temporary variables in list comprehensions

2017-01-08 Thread Steven D'Aprano
On Monday 09 January 2017 15:09, Chris Angelico wrote: > On Mon, Jan 9, 2017 at 2:53 PM, Steven D'Aprano > wrote: >> [(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

Re: Temporary variables in list comprehensions

2017-01-08 Thread Chris Angelico
On Mon, Jan 9, 2017 at 2:53 PM, Steven D'Aprano wrote: > [(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... A horrible hack on par with abusing a recursive