How do I convert a string name into a variable name?
example
L=['a','b']
I want to create two variables from L so that I can assign values to them.
say
a=4
b=5
4 48001 bvdet 2,851
Recognized Expert Moderator Specialist
Following are a couple of ways: - >>> L=['a','b']
-
>>> V=[4,5]
-
>>> for var in zip(L,V):
-
... exec "%s=%s" % (var[0], var[1])
-
...
-
>>> a
-
4
-
>>> b
-
5
-
>>> V=[6,7]
-
>>> dd = dict(zip(L,V))
-
>>> dd
-
{'a': 6, 'b': 7}
-
>>> globals().update(dd)
-
>>> a
-
6
-
>>> b
-
7
-
>>>
Why would you want to do this?
It was the %s=%s %(a,z) that I over looked
Problem solved!
Actually I need something like this:
L=['G123','G452', 'Had3'......] #Not sure whats going to be in L until run
into
G123= [112,234,113,..] #Values in list determined during run.
G452=[456, 789,0,0...]
and then:
L2=[G123,G452,Had3...]
then send L2 off to another def with the named variables G123 ....
Is this a little clearer??
bvdet 2,851
Recognized Expert Moderator Specialist
Something like this: - L=['G123','G452', 'Had3']
-
for item in L:
-
globals().update({item: return_a_list()})
-
-
for item in L:
-
print eval(item)
I do not understand what you are doing, but I think you should consider organizing and passing data with dictionaries.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Stewart Midwinter |
last post by:
I'd like to do something like the following:
a = 1; b = 2; c = None
mylist =
for my in mylist:
if my is None:
print 'you have a problem with %s' % my #this line is problematic
>>>...
|
by: Konrad Mathieu |
last post by:
How do I - within in a function - make Javascript recognize a string as a
variable name?
Thanks and cheers,
Konrad
|
by: cpp |
last post by:
I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:
string s = "whatever";
How can I obtain the following expression:...
|
by: paladin.rithe |
last post by:
I have no clue if this can be done, but I thought I'd ask anyway. I
have a modular system where I store module information in a database.
One of the types of modules that I have is one that,...
|
by: encoad |
last post by:
Hi everyone,
I've run into another road block that google.com isn't able to solve
since I don't even know where to start when searching.
In my webapp, a certain number of table rows containing...
| |
by: massdeletion101 |
last post by:
Is there anyway to assign a value of type string to be the name of an integer, or convert the value or vise versa?
Example:
int Q = 1;
string member = "member" + Q;
//some kind of type name or...
|
by: rohdej |
last post by:
Hello -
I have been all over the web and found a few posts that are somewhat related to what I'm trying to do, but none that provided me a concise answer.
I want to prompt the user to input the...
|
by: rrocket |
last post by:
I have a bunch of params:
InputParam1, InputParam2, InputParam3, etc...
I would like to loop through them instead of writing out code for each one, but am having some issues getting it to work...
|
by: anon538 |
last post by:
Is it possible to reference a string to a variable name? For instance:
public class test
{
int somevariable = 1;
public int getInt(String val)
{
//by some means, returns a value bases...
|
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: 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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |