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 be overwritten. In other words,
temp variables are not local to list comprehensions.
My question is why is this and is there any way to make c local to
list comp?
Thanks,
Geoffrey 4 3026
On Tue, 2007-10-23 at 17:02 +0000, 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
>"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 be overwritten. In other words,
temp variables are not local to list comprehensions.
My question is why is this and is there any way to make c local to
list comp?
The only way to keep c from being overwritten currently is to avoid
using a list comprehension. Generator expressions don't "leak" their
iteration variable to the outside, so you can write this instead:
list(1.0 if c=='A' else c=='B' for c in ab)
HTH,
--
Carsten Haese http://informixdb.sourceforge.net
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 before
the list comprehension, the value will be overwritten. In other words,
temp variables are not local to list comprehensions.
My question is why is this and is there any way to make c local to
list comp?
Unfortunately, not as such. They do leak their variable names.
But what you can do ist to create generator expressions (which don't
leak) and put a list around them:
list(1.0 if c == 'A' else c="B" for c in ab)
See also http://www.python.org/dev/peps/pep-0289/
Diez
On Tue, 23 Oct 2007 17:02:48 +0000, 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 local to list comprehensions.
That's right.
My question is why is this and is there any way to make c local to list
comp?
It was a design decision that is now regretted. There is no way for you
to make it local to the list comp except to wait for a new version of
Python that behaves in the way you want.
Or you can look at generator expressions, which are written just like
list comprehensions except with round brackets (). They are not quite the
same thing, but you can often use one in place of the other.
--
Steven.
On Oct 23, 12:02 pm, beginner <zyzhu2...@gmail.comwrote:
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 be overwritten. In other words,
temp variables are not local to list comprehensions.
My question is why is this and is there any way to make c local to
list comp?
Thanks,
Geoffrey
I see.
Thanks for everyone's help! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: cognite |
last post by:
This venue would surely appreciate the cool stuff being done in
python on bioinformatics and python's tools for info parsing and
extraction (like fulltext indexing, xml tools, parser builders,...
|
by: Johnny A. Stoa |
last post by:
PRESS RELEASE
I am pleased to forward to you our official press release related to the
launch of our new software product Life*TYPE.
Life*TYPE is an XML/SGML formatting and paginating COTS...
|
by: Paul |
last post by:
In real life situation, do we ever come across a situation where we
would need two base objects in an object.
A snippet is worth 1000 words (: so...
class Base
{
};
class Derived1:public Base...
|
by: jim |
last post by:
Hi All,
I like to know the life cycle of an ASP .NET Application( incudieng server
application, such as .NET Web Service). That means from initialization to
fully running and how to reboot it or...
|
by: CapMaster |
last post by:
I'm having some trouble programming the game of life. In the version my teacher gave us, it involves a class with a private grid variable for the game. Here's the class she gave us:
..
const int...
|
by: Sef |
last post by:
On another forum I am working on getting a mod working for Half Life 2. It involves adding the gun from Portal to Half Life 2 however there is many differences between the two games. We have...
|
by: blackslither |
last post by:
I had to implement Game of Life in a 3D matrix (int a) , but the complexity of my implementation is O(n^6) (6 imbricated for) and it runs very slow . Can anyone help me with an optimized version of...
|
by: Let U Know All |
last post by:
Life happy life
http://143life.blogspot.com/
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |