This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 8 years ago.
x=1
code(x)=5
print(code1)
I want this to work andit print "5" could anybody help me do this in python.
myList=[1,2,3,4,5,6]
print myList[2]
will print 3. You can assign an index to a variable and do this:
index=2
print myList[index]
will print 3.
You can use "code" as a list.
x=1
code=[0,0]
code[x]=5
print(code[1])
or as a dict:
x=1
code={}
code[x]=5
print(code[1])
depends on how you will use "code".
As the other answer suggested, you'll better use dictionary or a list for this.
But for your question you could it like this:
x = 1
exec(('code' + x) + '=5')
print code1
code1 will be 5.
code=[0,0]
x=1
code[x]=5
print(code[1])
Related
This question already has answers here:
Getting the name of a variable as a string
(32 answers)
Closed 1 year ago.
Consider the following code:
x,y = 0,1
for i in [x,y]:
print(i) # will print 0,1
Suppose I wanted instead to print:
x=0
y=1
I realise f-strings can be used to print the intermediate variable name:
for i in [x,y]:
print(f"{i=}") # will print i=0, i=1
However, I am interested in the actual variable name.
There are other workarounds: using eval or using zip([x,y], ['x', 'y']), but I was wondering if an alternative approach exists.
I think this achieves what you want to do -
for i in vars():
print(f'{i}={vars()[i]}')
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 4 years ago.
I have the following code:
a=2
string="a"
b=exec(string)
print(b)
Output:
None
I want b to have the value of 'a' i.e. 2 how can I do that?
If I understand your case correctly, you want to evaluate some python source from string in context of existing variables.
Builtin eval function could be used for that.
a=2
string="a"
b=eval(string)
print(b)
Anyway. Why do you need that? There is better way to do that for sure.
Probably in your case you could use dictionary to remember values instead of separate variables. And after reading "names" from file use this names as dictionary keys.
your_dict = {}
your_dict["a"] = 2
string = "a"
b = your_dict[string]
print(b)
a=2
string="b=a"
exec(string)
print(b)
exec() just exectues the code in the given string, so the assignment must be done in the string itself.
This question already has answers here:
Is it a good idea to dynamically create variables?
(5 answers)
Closed 5 years ago.
how can I transform a string in a variable name ?
I have multiple variable value1, value2, value3 and value1 = 5
when i use
x = 1
print(str("value" + str(x)))
it return "value1" and not "5"
edit: it isn't a duplicate because i am asking how to solve my problem and not if it is good or bad to use eval(it is the link which you referred me to)
so when i use eval to change the value of the variable it doesn't work ("can't assign to function call"),
global('newValue' + str(z))
eval('newValue' + str(z)) = y + value
and if you do not recommend using eval what should you use instead ? because i have a lot of 'if value1 <' and 'if value 2 <' but i want to change it to a while
print(eval(str(“value” + str(x))))
But must not use eval even if you know it’s completely secure. There is always a better way.
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 6 years ago.
I have a list of variables in Python with below values.
I am trying to print the values of these variables in the following manner, but this is unsuccessful. Can someone please help with a solution ?
a1=1
a2=2
a3=3
for i in range(1,4):
temp="a"+str(i)
print(temp)
I want the output in 'temp' print the values(viz) 1,2,3 whereas the output seen are the variables (viz) a1,a2,a3
Try This:-
a1=1
a2=2
a3=3
for i in range(1,4):
temp="a"+str(i)
print locals()[temp]
This question already has answers here:
What is getattr() exactly and how do I use it?
(14 answers)
How can I select a variable by (string) name?
(5 answers)
Closed 6 years ago.
I need to check variables looking like this:
if name1 != "":
(do something)
Where the number right after "name" is incremented between 1 and 10.
Do I need to write the test ten times or is there a way (without using an array or a dict) to "concatenate", so to speak, variable names?
I'm thinking about something like this:
for i in range(10):
if "name" + str(i) != "":
(do something)
Edit: I can't use a list because I'm actually trying to parse results from a Flask WTF form, where results are retrieved like this:
print(form.name1.data)
print(form.name2.data)
print(form.name3.data)
etc.
Use a list, such as:
names = ['bob', 'alice', 'john']
And then iterate on the list:
for n in names:
if n != "":
(do something)
or you could have a compounded if statement:
if (name1 != "" or name2 != "" or name3 != "")
The best solution would be to use solution #1.
If you cannot use a list or a dict, you could use eval
for i in range(10):
if eval("name" + str(i)) != "":
(do something)
First of all, your app have invalid logic. You should use list, dict or your custom obj.
You can get all variable in globals. Globals is a dict.
You can do next:
for i in range(10):
if globals().get('name%d' % i):
# do something