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]}')
Related
This question already has answers here:
How does a lambda function refer to its parameters in python?
(4 answers)
Closed 1 year ago.
I wrote a Python code like:
fun_list = []
for i in range(10):
fun_list.append(lambda : f(i))
for j in range(10):
fun_list[j]()
I want it to output numbers from 0 to 9, but actually it outputs 9 for ten times!
I think the question is that the variable is be transported into function f only it was be called. Once it was be called it will globally find variable named 'i'.
How to modify the code so that it can output numbers from 0 to 9?
Your Code doesn't make sensefun_list[j]() how are you calling a List Element?
If You Want to append the numbers mentioned above into your array then Correct Code is:-
fun_list = []
for i in range(10): ### the range has to be from 0 to 10 that is [0,10)
fun_list.append(i) ### You dont need lambda function to append i
What This does is also if of No need because you first of all havent initialized what is f?... Is it a function?. You here are not appending the value but instead appending the called function statement. You dont need that. Just do it simply like above....:-
fun_list.append(lambda : f(i))
This question already has answers here:
How to print actual name of variable class type in function?
(4 answers)
Getting the name of a variable as a string
(32 answers)
Closed 3 years ago.
I want print paramater's name in a python function
First, a variable is assigned a value.Then, pass the variable to a function.
Last,I want to print str(variable) inner the function.
varibale = 1234
def f(x):
print(....)
return
f(varibale)
Expected output is print out the 'varibale' whatever variable is.
if a = 1 ==> f(x), expected output is 'a';if b = 2 ==> f(x), expected output is 'b'.....
Why do you want to print out the value using a function?
If you know the name of the variable, you can simply write your requested function output yourself by just putting 'variable'
So in your example: instead of using f(x), just use print('x')
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:
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:
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])