Re: Local variable definition in Python list comprehension

2022-09-02 Thread Dan Stromberg
On Thu, Sep 1, 2022 at 9:16 AM Chris Angelico wrote: > On Fri, 2 Sept 2022 at 02:10, James Tsai wrote: > > > > Hello, > > > > I find it very useful if I am allowed to define new local variables in a > list comprehension. For example, I wish to have something like > > [(x, y) for x in range(10) f

Re: Local variable definition in Python list comprehension

2022-09-02 Thread James Tsai
在 2022年9月2日星期五 UTC+2 00:17:23, 写道: > On 02Sep2022 07:01, Chris Angelico wrote: > >On Fri, 2 Sept 2022 at 06:55, James Tsai wrote: > >> No but very often when I have written a neat list/dict/set > >> comprehension, I find it very necessary > >> to define local variable(s) to make it more clear

Re: Local variable definition in Python list comprehension

2022-09-01 Thread Avi Gross
Dumb question. Your y is purely a function of x. So create an f(x) where you want your y. It probably can even be anonymous inline. I mean your return values of (x, y) would be (x, f(x)) ... On Thu, Sep 1, 2022, 5:04 PM Chris Angelico wrote: > On Fri, 2 Sept 2022 at 06:55, James Tsai wrote: > >

Re: Local variable definition in Python list comprehension

2022-09-01 Thread Cameron Simpson
On 02Sep2022 07:01, Chris Angelico wrote: >On Fri, 2 Sept 2022 at 06:55, James Tsai wrote: >> No but very often when I have written a neat list/dict/set >> comprehension, I find it very necessary >> to define local variable(s) to make it more clear and concise. Otherwise I >> have to break it d

Re: Local variable definition in Python list comprehension

2022-09-01 Thread Peter J. Holzer
On 2022-09-01 13:33:16 -0700, James Tsai wrote: > 在 2022年9月1日星期四 UTC+2 18:34:36, 写道: > > On 9/1/22, James Tsai wrote: > > > > > > I find it very useful if I am allowed to define new local variables in a > > > list comprehension. For example, I wish to have something like > > > [(x, y) for x in

Re: Local variable definition in Python list comprehension

2022-09-01 Thread Chris Angelico
On Fri, 2 Sept 2022 at 06:55, James Tsai wrote: > > 在 2022年9月1日星期四 UTC+2 18:34:36, 写道: > > On 9/1/22, James Tsai wrote: > > > > > > I find it very useful if I am allowed to define new local variables in a > > > list comprehension. For example, I wish to have something like > > > [(x, y) for x in

Re: Local variable definition in Python list comprehension

2022-09-01 Thread James Tsai
在 2022年9月1日星期四 UTC+2 18:16:03, 写道: > On Fri, 2 Sept 2022 at 02:10, James Tsai wrote: > > > > Hello, > > > > I find it very useful if I am allowed to define new local variables in a > > list comprehension. For example, I wish to have something like > > [(x, y) for x in range(10) for y := x **

Re: Local variable definition in Python list comprehension

2022-09-01 Thread James Tsai
在 2022年9月1日星期四 UTC+2 18:34:36, 写道: > On 9/1/22, James Tsai wrote: > > > > I find it very useful if I am allowed to define new local variables in a > > list comprehension. For example, I wish to have something like > > [(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or > > [(x, y) f

Re: Local variable definition in Python list comprehension

2022-09-01 Thread James Tsai
way to write it already. Your working version is, to me, > clearer that the ones you want to be able to write. > > -- > Ben. By local variable definition I mean binding a variable to a single value, so it doesn't include giving an iterable that a variable can take values iterat

Re: Local variable definition in Python list comprehension

2022-09-01 Thread Eryk Sun
On 9/1/22, James Tsai wrote: > > I find it very useful if I am allowed to define new local variables in a > list comprehension. For example, I wish to have something like > [(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or > [(x, y) for x in range(10) with y := x ** 2 if x + y < 80]. >

Re: Local variable definition in Python list comprehension

2022-09-01 Thread Chris Angelico
On Fri, 2 Sept 2022 at 02:10, James Tsai wrote: > > Hello, > > I find it very useful if I am allowed to define new local variables in a list > comprehension. For example, I wish to have something like > [(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or > [(x, y) for x in range(10) wit

Re: Local variable definition in Python list comprehension

2022-09-01 Thread Ben Bacarisse
James Tsai writes: > I find it very useful if I am allowed to define new local variables in > a list comprehension. For example, I wish to have something like > [(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or > [(x, y) for x in range(10) with y := x ** 2 if x + y < 80]. > > For now

Local variable definition in Python list comprehension

2022-09-01 Thread James Tsai
Hello, I find it very useful if I am allowed to define new local variables in a list comprehension. For example, I wish to have something like [(x, y) for x in range(10) for y := x ** 2 if x + y < 80], or [(x, y) for x in range(10) with y := x ** 2 if x + y < 80]. For now this functionality can

Re: Variable definition

2010-03-01 Thread Rhodri James
On Mon, 01 Mar 2010 18:07:17 -, Raphael Mayoraz wrote: Thanks for your answer. However, your solution changes the key name in the dictionary. That's not what I want I need to do. What I want is to define a new variable which name is define as a string: 'myPrefx' + key. In the example I

Re: Variable definition

2010-03-01 Thread John Posner
On 3/1/2010 1:07 PM, Raphael Mayoraz wrote: John Posner wrote: On 2/26/2010 6:32 PM, Raphael Mayoraz wrote: Hello, I'd like to define variables with some specific name that has a common prefix. Something like this: varDic = {'red': 'a', 'green': 'b', 'blue': 'c'} for key, value in varDic.iter

Re: Variable definition

2010-03-01 Thread Raphael Mayoraz
John Posner wrote: On 2/26/2010 6:32 PM, Raphael Mayoraz wrote: Hello, I'd like to define variables with some specific name that has a common prefix. Something like this: varDic = {'red': 'a', 'green': 'b', 'blue': 'c'} for key, value in varDic.iteritems(): 'myPrefix' + key = value No trick

Re: Variable definition

2010-02-27 Thread Andreas Waldenburger
On 27 Feb 2010 03:33:57 GMT Steven D'Aprano wrote: > exec "'myPrefix_turquoise' = 42" > Not quite: In [1]: exec "'myPrefix_turquoise' = 42" File "", line 1 SyntaxError: can't assign to literal (, line 1) I think you meant: exec

Re: Variable definition

2010-02-26 Thread Dave Angel
Steven D'Aprano wrote: for key, value in varDic.iteritems(): varDic['myPrefix_' + key] = value del varDic[key] Watch out if any of the existing values already startswith 'myPrefix' You can end up with trouble just as confusing as if 'myPrefix' is an empty string DaveA -- http:/

Re: Variable definition

2010-02-26 Thread Steven D'Aprano
On Fri, 26 Feb 2010 15:32:27 -0800, Raphael Mayoraz wrote: > Hello, > > I'd like to define variables with some specific name that has a common > prefix. > Something like this: > > varDic = {'red': 'a', 'green': 'b', 'blue': 'c'} > for key, value in varDic.iteritems(): > 'myPrefix' + key = va

Re: Variable definition

2010-02-26 Thread John Posner
On 2/26/2010 10:20 PM, Steven D'Aprano wrote: On Fri, 26 Feb 2010 20:15:16 -0500, John Posner wrote: On 2/26/2010 6:32 PM, Raphael Mayoraz wrote: Hello, I'd like to define variables with some specific name that has a common prefix. Something like this: varDic = {'red': 'a', 'green': 'b', 'bl

Re: Variable definition

2010-02-26 Thread Steven D'Aprano
On Fri, 26 Feb 2010 20:15:16 -0500, John Posner wrote: > On 2/26/2010 6:32 PM, Raphael Mayoraz wrote: >> Hello, >> >> I'd like to define variables with some specific name that has a common >> prefix. >> Something like this: >> >> varDic = {'red': 'a', 'green': 'b', 'blue': 'c'} for key, value in >

Re: Variable definition

2010-02-26 Thread John Posner
On 2/26/2010 6:32 PM, Raphael Mayoraz wrote: Hello, I'd like to define variables with some specific name that has a common prefix. Something like this: varDic = {'red': 'a', 'green': 'b', 'blue': 'c'} for key, value in varDic.iteritems(): 'myPrefix' + key = value No trick, just swap a new ke

Re: Variable definition

2010-02-26 Thread alex goretoy
On Fri, Feb 26, 2010 at 6:22 PM, Alf P. Steinbach wrote: > * Raphael Mayoraz: > >> Hello, >> >> >> I'd like to define variables with some specific name that has a common >> prefix. >> Something like this: >> >> varDic = {'red': 'a', 'green': 'b', 'blue': 'c'} >> for key, value in varDic.iteritems

Re: Variable definition

2010-02-26 Thread Alf P. Steinbach
* Raphael Mayoraz: Hello, I'd like to define variables with some specific name that has a common prefix. Something like this: varDic = {'red': 'a', 'green': 'b', 'blue': 'c'} for key, value in varDic.iteritems(): 'myPrefix' + key = value I know this is illegal, but there must be a trick s

Re: Variable definition

2010-02-26 Thread Rhodri James
On Fri, 26 Feb 2010 23:32:27 -, Raphael Mayoraz wrote: I'd like to define variables with some specific name that has a common prefix. Why? No seriously, how do you think this is going to solve whatever problem you clearly think it will solve? -- Rhodri James *-* Wildebeeste Herder

Variable definition

2010-02-26 Thread Raphael Mayoraz
Hello, I'd like to define variables with some specific name that has a common prefix. Something like this: varDic = {'red': 'a', 'green': 'b', 'blue': 'c'} for key, value in varDic.iteritems(): 'myPrefix' + key = value I know this is illegal, but there must be a trick somewhere. Thanks,

Re: Nested function scope problem (- variable definition branch)

2006-07-30 Thread H J van Rooyen
"Gerhard Fiedler" <[EMAIL PROTECTED]> wrote: 8<- | I'm not sure where you're trying to go. I think that most people (and even | Bruno, who argued this issue most strongly) call Python variables | "variables" every now and then, or maybe even usually. But it was hel