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] -- https://mail.python.org/mailman/listinfo/python-list

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 = expensive_calculation(x)] >>> >>> Alas! >> >

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! Or even [(tmp, tmp + 1)

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, expensive_calculation(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 in data with expensive_c

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 >>> ordinary loop is with a temporary varia

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: result = [] for x in data: tmp = e

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: > tmp = expensive_calculation(x) > r

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 here if you discuss Python, o

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 cont

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 one-argument function expensive_

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) vs py2 > > (it consumes the entir

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: > tmp = expensive_calculation(x) > r

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) leads me to avoid it in general,

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 u

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 data

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 would

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 = expensive_ca

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 cha

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 gener

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) cache[x] = a r

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) result

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 w

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 be your language... Agreed, but with

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 hack... > > A horrible hack on par with

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 function's arguments for private variables. Much

Temporary variables in list comprehensions

2017-01-08 Thread 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 = expensive_calculation(x) result.append((tmp, tmp+1)) But what if you ar

Re: Life-time of temporary variables in list comprehensions

2007-10-23 Thread beginner
On Oct 23, 12:02 pm, beginner <[EMAIL PROTECTED]> wrote: > Hi All, > > If I have a list comprehension: > > ab=["A","B"] > c = "ABC" > [1.0 if c=='A' else c='B' for c in ab] > print c > > >>"B" > > My test shows that if c is not defined before the list comprehension, > it will be created in the list

Re: Life-time of temporary variables in list comprehensions

2007-10-23 Thread Steven D'Aprano
On Tue, 23 Oct 2007 17:02:48 +, beginner wrote: > My test shows that if c is not defined before the list comprehension, it > will be created in the list comprehension; if it is defined before the > list comprehension, the value will be overwritten. In other words, temp > variables are not loca

Re: Life-time of temporary variables in list comprehensions

2007-10-23 Thread Diez B. Roggisch
beginner schrieb: > Hi All, > > If I have a list comprehension: > > ab=["A","B"] > c = "ABC" > [1.0 if c=='A' else c='B' for c in ab] > print c > >>> "B" > > My test shows that if c is not defined before the list comprehension, > it will be created in the list comprehension; if it is defined be

Re: Life-time of temporary variables in list comprehensions

2007-10-23 Thread Carsten Haese
On Tue, 2007-10-23 at 17:02 +, beginner wrote: > Hi All, > > If I have a list comprehension: > > ab=["A","B"] > c = "ABC" > [1.0 if c=='A' else c='B' for c in ab] "c='B'" is invalid syntax. Maybe you mean "c=='B'". That doesn't make much sense, but at least it's correct syntax. > print c >

Life-time of temporary variables in list comprehensions

2007-10-23 Thread beginner
Hi All, If I have a list comprehension: ab=["A","B"] c = "ABC" [1.0 if c=='A' else c='B' for c in ab] print c >>"B" My test shows that if c is not defined before the list comprehension, it will be created in the list comprehension; if it is defined before the list comprehension, the value will