473,388 Members | 1,215 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

string substitution for creating variable names

In the below code, I have used 5 different variables var1xxx,...var5xxx using 5 statements. But I would like to loop over the aList elements to substitute for 1 to 5 in the variable names and hence assign the value. (Here I have shown the same element value as assigned.) Can you please let me know how to use either string substitution or string template for the following to reduce the number of lines of code and hence consice.

Thanks & Regards,
SKN

Expand|Select|Wrap|Line Numbers
  1. aList = [1,2,3,4,5]
  2. var1xxx = aList[0]
  3. var2xxx = aList[1]
  4. var3xxx = aList[2]
  5. var4xxx = aList[3]
  6. var5xxx = aList[4]
  7. print var1xxx,var2xxx,var3xxx,var4xxx,var5xxx
  8.  
Aug 13 '07 #1
6 12957
bartonc
6,596 Expert 4TB
In the below code, I have used 5 different variables var1xxx,...var5xxx using 5 statements. But I would like to loop over the aList elements to substitute for 1 to 5 in the variable names and hence assign the value. (Here I have shown the same element value as assigned.) Can you please let me know how to use either string substitution or string template for the following to reduce the number of lines of code and hence consice.

Thanks & Regards,
SKN

Expand|Select|Wrap|Line Numbers
  1. aList = [1,2,3,4,5]
  2. var1xxx = aList[0]
  3. var2xxx = aList[1]
  4. var3xxx = aList[2]
  5. var4xxx = aList[3]
  6. var5xxx = aList[4]
  7. print var1xxx,var2xxx,var3xxx,var4xxx,var5xxx
  8.  
That's a fun one! I really liked writing this:
Expand|Select|Wrap|Line Numbers
  1. >>> aList = [1,2,3,4,5]
  2. >>> for i, value in enumerate(aList):
  3. ...     exec("var%dxxx = value" %(i+1))
  4. ...     
  5. >>> var1xxx
  6. 1
  7. >>> 
Aug 13 '07 #2
That's a fun one! I really liked writing this:
Expand|Select|Wrap|Line Numbers
  1. >>> aList = [1,2,3,4,5]
  2. >>> for i, value in enumerate(aList):
  3. ...     exec("var%dxxx = value" %(i+1))
  4. ...     
  5. >>> var1xxx
  6. 1
  7. >>> 
Thanks Bartonc. If aList has ['abc,'def','ghi'] in place of integers, then how to do that. Assuming that we have string substitution on right side variables also.
Expand|Select|Wrap|Line Numbers
  1. aList = ['abc','def','hij']
  2. aabcList = [1,2,3]
  3. adefList = [3,4,5]
  4. ahijList = [4,5,6]
  5. varabcxxx = aabcList[0]
  6. vardefxxx = adefList[0]
  7. varhijxxx = ahijList[0]
  8. print varabcxxx,vardefxxx,varhijxxx
  9.  
Thanks
SKN
Aug 13 '07 #3
bvdet
2,851 Expert Mod 2GB
Thanks Bartonc. If aList has ['abc,'def','ghi'] in place of integers, then how to do that. Assuming that we have string substitution on right side variables also.
Expand|Select|Wrap|Line Numbers
  1. aList = ['abc','def','hij']
  2. aabcList = [1,2,3]
  3. adefList = [3,4,5]
  4. ahijList = [4,5,6]
  5. varabcxxx = aabcList[0]
  6. vardefxxx = adefList[0]
  7. varhijxxx = ahijList[0]
  8. print varabcxxx,vardefxxx,varhijxxx
  9.  
Thanks
SKN
Here is one way:
Expand|Select|Wrap|Line Numbers
  1. >>> aList = ['abc','def','hij']
  2. >>> pre = 'var'
  3. >>> suf = 'xxx'
  4. >>> data = [[1,2,3], [3,4,5], [4,5,6]]
  5. >>> def var_dict(p,iList,s,data):
  6. ...     dd = {}
  7. ...     for i, item in enumerate(iList):
  8. ...         dd['a%sList' % item] = data[i]
  9. ...         dd['%s%s%s' % (p,item,s)] = data[i][0]
  10. ...     globals().update(dd)
  11. ...     return dd
  12. ... 
  13. >>> dd = var_dict(pre,aList,suf,data)
  14. >>> aabcList
  15. [1, 2, 3]
  16. >>> varabcxxx
  17. 1
  18. >>>
I did not need to return the dictionary or assign the function call to a variable. globals().update() assigns the values to the variable names in the global namespace of the module.
Aug 13 '07 #4
Here is one way:
Expand|Select|Wrap|Line Numbers
  1. >>> aList = ['abc','def','hij']
  2. >>> pre = 'var'
  3. >>> suf = 'xxx'
  4. >>> data = [[1,2,3], [3,4,5], [4,5,6]]
  5. >>> def var_dict(p,iList,s,data):
  6. ...     dd = {}
  7. ...     for i, item in enumerate(iList):
  8. ...         dd['a%sList' % item] = data[i]
  9. ...         dd['%s%s%s' % (p,item,s)] = data[i][0]
  10. ...     globals().update(dd)
  11. ...     return dd
  12. ... 
  13. >>> dd = var_dict(pre,aList,suf,data)
  14. >>> aabcList
  15. [1, 2, 3]
  16. >>> varabcxxx
  17. 1
  18. >>>
I did not need to return the dictionary or assign the function call to a variable. globals().update() assigns the values to the variable names in the global namespace of the module.
Thanks BV. Is it possible to use a dynamic list defined. I mean instead of defining "data" can we have aabcList[0] defined inside using the same substitute "abc" string in both left and right hand side of the assignment statement.


Regards,
SKN
Aug 14 '07 #5
bvdet
2,851 Expert Mod 2GB
Thanks BV. Is it possible to use a dynamic list defined. I mean instead of defining "data" can we have aabcList[0] defined inside using the same substitute "abc" string in both left and right hand side of the assignment statement.


Regards,
SKN
This can be done similar to what Barton proposed using exec() and eval(). Keeping with the dictionary approach:
Expand|Select|Wrap|Line Numbers
  1. aList = ['abc','def','hij']
  2.  
  3. dd1 = dict(zip(['a%sList' % item for item in aList], [[11,12,13], [13,14,15], [14,15,16]]))
  4. for item in aList:
  5.     dd1['v%sxxx' % item] = dd1['a%sList' % item][0]
  6. globals().update(dd1)
Aug 14 '07 #6
Thanks a bunch, BV. Which approach is efficient? Dictionary approach or using exec() and eval().


Regards,
SKN
Aug 14 '07 #7

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

Similar topics

0
by: Wes Batson | last post by:
I have a program that builds screens dynamically by getting all form information from a database including size, position, and variable names. I was wondering how I convert the names from a String...
15
by: Thomas Scheiderich | last post by:
I thought I read that the case for the variable names is important. For example Dim Wheel As Integer Wheel here is a different variable from WHEEL. Is this correct?
2
by: Hamish Symington | last post by:
Hello, I'm trying to re-code a site which I wrote in PHP into ASP.NET using Visual Basic .NET. Something PHP has which has proved invaluable is the concept of variable variables. I haven't...
5
by: Murali | last post by:
In Python, dictionaries can have any hashable value as a string. In particular I can say d = {} d = "Right" d = "Wrong" d = "test" In order to print "test" using % substitution I can say
16
by: John | last post by:
Does the length of my C variable names have any affect, performance-wise, on my final executable program? I mean, once compiled, etc., is there any difference between these two: number = 3; n =...
10
by: John Salerno | last post by:
If I want to have a list like this: where the first part of each tuple is a variable name and the second part is a label for the user to see, such as a form like this: First Name: ________...
7
by: Samuel | last post by:
Hi, How would you implement a simple parser for the following string: --- In this string $variable1 is substituted, while \$variable2 is not. --- I know how to write a parser, but I am...
4
by: Victor Lagerkvist | last post by:
Hello, I have the need to parse variable names from a string and save them somewhere safe for future usage. Here's my first attempt (I don't have any rules for valid names yet) - but I have a...
6
by: Academia | last post by:
I want to search for Dim and replace it with Dim That is, I want to change the first character of Dim variable names to upper case. I can't figure know to use Regular Expression to do that....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.