Hi,
If I got a list is it possible to declare a variable from the items in that list?
Code Sample:
Blob = ['Var1', 'Var2', 'vAR3']
i = 5
for listitems in Blob:
i += 1
listitems = i
print Var1
6
print Var2
7
print vAR3
8
Something like that? This doesn't work (obviously) but is there a way to do this?
TIA,
Cacti 6 1629
"Cactus" wrote: If I got a list is it possible to declare a variable from the items in that list?
Code Sample: Blob = ['Var1', 'Var2', 'vAR3'] i = 5 for listitems in Blob: i += 1 listitems = i
print Var1 6 print Var2 7 print vAR3 8
Something like that? This doesn't work (obviously) but is there a way to do this?
why?
if you want a dictionary, use a dictionary (see the tutorial for details).
</F>
Python has a builtin function called locals which returns the local
context as a dictionary locals = locals() locals["a"] = 5 a
5 locals["a"] = "changed" a
'changed'
On 8 Apr 2005 13:55:39 -0700, Cactus <le*******@msn.com> wrote: Hi,
If I got a list is it possible to declare a variable from the items in that list?
Code Sample: Blob = ['Var1', 'Var2', 'vAR3'] i = 5 for listitems in Blob: i += 1 listitems = i
print Var1 6 print Var2 7 print vAR3 8
Something like that? This doesn't work (obviously) but is there a way to do this?
TIA, Cacti -- http://mail.python.org/mailman/listinfo/python-list
-- http://blogs.applibase.net/sidharth
On Sat, Apr 09, 2005 at 03:15:01AM +0530, Sidharth Kuruvila wrote: Python has a builtin function called locals which returns the local context as a dictionary
locals = locals() locals["a"] = 5 a 5 locals["a"] = "changed" a 'changed'
From Python lib reference:
"""
locals()
...
Warning: The contents of this dictionary should not be
modified; changes may not affect the values of local variables used
by the interpreter.
"""
What I gave was a bad solution. Something that works right now, but
probably shouldn't be done.
On Apr 9, 2005 3:37 AM, Inyeol Lee <in********@siimage.com> wrote: On Sat, Apr 09, 2005 at 03:15:01AM +0530, Sidharth Kuruvila wrote: Python has a builtin function called locals which returns the local context as a dictionary
>> locals = locals() >> locals["a"] = 5 >> a 5>> locals["a"] = "changed" >> a 'changed'
From Python lib reference:
""" locals() ... Warning: The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the interpreter. """ -- http://mail.python.org/mailman/listinfo/python-list
-- http://blogs.applibase.net/sidharth
You can use the built-in statement exec
( http://www.python.org/doc/2.4.1/ref/exec.html) :
# Blob = ['Var1', 'Var2', 'vAR3']
# i = 5
# for listitems in Blob:
# i += 1
# exec('%s = i' %listitems)
#
# print Var1, Var2, vAR3
Regards,
Pierre
"Fredrik Lundh" <fr*****@pythonware.com> wrote in message news:<ma**************************************@pyt hon.org>... "Cactus" wrote:
If I got a list is it possible to declare a variable from the items in that list?
Code Sample: Blob = ['Var1', 'Var2', 'vAR3'] i = 5 for listitems in Blob: i += 1 listitems = i
print Var1 6 print Var2 7 print vAR3 8
Something like that? This doesn't work (obviously) but is there a way to do this?
why?
if you want a dictionary, use a dictionary (see the tutorial for details).
</F>
Thanks,
I'll look in to that. Seems like that will work....
Cacti This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Stefanie |
last post by:
Hi,
I have a question about declaring variables for writing data to a file for
random access.
I have two string variables declared in a module (user-defined types).
The point is that for one of...
|
by: Oliver Corona |
last post by:
I am wondering if anyone has any insights on the performance benefit (or
detriment) of declaring local variables instead of referencing members.
Is allocating memory for a new variable more...
|
by: Joe |
last post by:
Is it generally considered "better" (i.e. clearer) to declare all variables
at the start of a function or as you need them within it?
Joe
|
by: ColinWard |
last post by:
Hi guys.
I have a question about declaring variables. I do a lot of re-querying
of controls in my database and I use the Set statement with a variable
set to the name of the control to tell the...
|
by: jbeteta |
last post by:
Hello,
I have a problem declaring variables. I need to create an object oRpte
as ReportClass on WebForm1.aspx and be able to use its value on
WebForm2.aspx.
For declaring the property oRpte()...
| |
by: vlsidesign |
last post by:
Here is my a portion of my program:
#include <stdio.h>
main()
{
int fahr, celsius;
int lower, upper, step;
int fc_conv(int fahr);
... snip ...
}
|
by: SM |
last post by:
I've always wonder if there is diference when declaring and
initializing a varible inside/outside a loop.
What's a better practice?
Declaring and initializing variables inside a loop routine,...
|
by: =?Utf-8?B?QUw=?= |
last post by:
Hi
I usually stick to the convention of not declaring variables in my bodies of
"loops" (including foreach)
ie
int x;
for (int i = 0; i < 10; i++) {
|
by: AZRebelCowgirl73 |
last post by:
This is what I have so far:
My program!
import java.util.*;
import java.lang.*;
import java.io.*;
import ch06.lists.*;
public class UIandDB {
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |