473,480 Members | 2,967 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Convert String Name to a variable name

4 New Member
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
Dec 17 '08 #1
4 48001
bvdet
2,851 Recognized Expert Moderator Specialist
Following are a couple of ways:
Expand|Select|Wrap|Line Numbers
  1. >>> L=['a','b']
  2. >>> V=[4,5]
  3. >>> for var in zip(L,V):
  4. ...     exec "%s=%s" % (var[0], var[1])
  5. ...     
  6. >>> a
  7. 4
  8. >>> b
  9. 5
  10. >>> V=[6,7]
  11. >>> dd = dict(zip(L,V))
  12. >>> dd
  13. {'a': 6, 'b': 7}
  14. >>> globals().update(dd)
  15. >>> a
  16. 6
  17. >>> b
  18. 7
  19. >>> 
Why would you want to do this?
Dec 17 '08 #2
Gunnar Hurtig
4 New Member
It was the %s=%s %(a,z) that I over looked
Problem solved!
Dec 18 '08 #3
Gunnar Hurtig
4 New Member
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??
Dec 18 '08 #4
bvdet
2,851 Recognized Expert Moderator Specialist
Something like this:
Expand|Select|Wrap|Line Numbers
  1. L=['G123','G452', 'Had3']
  2. for item in L:
  3.     globals().update({item: return_a_list()})
  4.  
  5. for item in L:
  6.     print eval(item)
I do not understand what you are doing, but I think you should consider organizing and passing data with dictionaries.
Dec 18 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
21701
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 >>>...
3
18724
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
10
3454
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:...
2
2093
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,...
6
2609
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...
10
2794
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...
5
16669
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...
10
5460
rrocket
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...
2
8979
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...
0
7039
marktang
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,...
0
7037
Oralloy
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,...
0
7080
jinu1996
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...
0
6895
tracyyun
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...
0
5326
agi2029
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,...
1
4770
isladogs
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...
0
4476
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...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
176
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.