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 - aList = [1,2,3,4,5]
-
var1xxx = aList[0]
-
var2xxx = aList[1]
-
var3xxx = aList[2]
-
var4xxx = aList[3]
-
var5xxx = aList[4]
-
print var1xxx,var2xxx,var3xxx,var4xxx,var5xxx
-
6 12931
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 - aList = [1,2,3,4,5]
-
var1xxx = aList[0]
-
var2xxx = aList[1]
-
var3xxx = aList[2]
-
var4xxx = aList[3]
-
var5xxx = aList[4]
-
print var1xxx,var2xxx,var3xxx,var4xxx,var5xxx
-
That's a fun one! I really liked writing this: -
>>> aList = [1,2,3,4,5]
-
>>> for i, value in enumerate(aList):
-
... exec("var%dxxx = value" %(i+1))
-
...
-
>>> var1xxx
-
1
-
>>>
That's a fun one! I really liked writing this: -
>>> aList = [1,2,3,4,5]
-
>>> for i, value in enumerate(aList):
-
... exec("var%dxxx = value" %(i+1))
-
...
-
>>> var1xxx
-
1
-
>>>
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. -
aList = ['abc','def','hij']
-
aabcList = [1,2,3]
-
adefList = [3,4,5]
-
ahijList = [4,5,6]
-
varabcxxx = aabcList[0]
-
vardefxxx = adefList[0]
-
varhijxxx = ahijList[0]
-
print varabcxxx,vardefxxx,varhijxxx
-
Thanks
SKN
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. -
aList = ['abc','def','hij']
-
aabcList = [1,2,3]
-
adefList = [3,4,5]
-
ahijList = [4,5,6]
-
varabcxxx = aabcList[0]
-
vardefxxx = adefList[0]
-
varhijxxx = ahijList[0]
-
print varabcxxx,vardefxxx,varhijxxx
-
Thanks
SKN
Here is one way: - >>> aList = ['abc','def','hij']
-
>>> pre = 'var'
-
>>> suf = 'xxx'
-
>>> data = [[1,2,3], [3,4,5], [4,5,6]]
-
>>> def var_dict(p,iList,s,data):
-
... dd = {}
-
... for i, item in enumerate(iList):
-
... dd['a%sList' % item] = data[i]
-
... dd['%s%s%s' % (p,item,s)] = data[i][0]
-
... globals().update(dd)
-
... return dd
-
...
-
>>> dd = var_dict(pre,aList,suf,data)
-
>>> aabcList
-
[1, 2, 3]
-
>>> varabcxxx
-
1
-
>>>
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.
Here is one way: - >>> aList = ['abc','def','hij']
-
>>> pre = 'var'
-
>>> suf = 'xxx'
-
>>> data = [[1,2,3], [3,4,5], [4,5,6]]
-
>>> def var_dict(p,iList,s,data):
-
... dd = {}
-
... for i, item in enumerate(iList):
-
... dd['a%sList' % item] = data[i]
-
... dd['%s%s%s' % (p,item,s)] = data[i][0]
-
... globals().update(dd)
-
... return dd
-
...
-
>>> dd = var_dict(pre,aList,suf,data)
-
>>> aabcList
-
[1, 2, 3]
-
>>> varabcxxx
-
1
-
>>>
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
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: - aList = ['abc','def','hij']
-
-
dd1 = dict(zip(['a%sList' % item for item in aList], [[11,12,13], [13,14,15], [14,15,16]]))
-
for item in aList:
-
dd1['v%sxxx' % item] = dd1['a%sList' % item][0]
-
globals().update(dd1)
Thanks a bunch, BV. Which approach is efficient? Dictionary approach or using exec() and eval().
Regards,
SKN
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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?
|
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...
|
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
|
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 =...
|
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: ________...
|
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...
|
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...
|
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....
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |