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
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!
>>
>
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)
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
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
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
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
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
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
>
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)
>
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
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
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:
[...
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
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)
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_
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
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
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
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,
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
>
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
39 matches
Mail list logo