Print variable name in python [duplicate] - python

This question already has answers here:
Simpler way to create dictionary of separate variables?
(27 answers)
Closed 4 years ago.
Here bkd_train, bkd_test are dataframe.
I wanted to print dataframe name along with it's shape.
for data in [bkd_train, bkd_test]:
print("{} : {}".format(?, data.shape))
If i am using "data" then it's printing dataframe.
But I want o/p like this:
bkd_train : (10886, 12)
bkd_test : (1111,11)

you cannot get the name of a variable as string, but you can pass a string in input and query locals dictionary to get the value:
for data in ["bkd_train", "bkd_test"]:
print("{} : {}".format(data,locals()[data].shape))
it works if the variables are local. If they're global, use globals, with fallback on locals, or just eval (not a problem in that context since the strings to evaluate are constant)
also read: How to get a variable name as a string?

I would iterate over the variable names themselves and then use eval to evaluate the names which will give you the dataframe.
for dataName in ['bkd_train', 'bkd_test']:
data = eval(dataName)
print("{} : {}".format(dataName, data.shape))

Related

Obtaining a string version of the variable name in Python [duplicate]

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]}')

I want print paramater's name in a python function [duplicate]

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')

Variable of Variable in Python [duplicate]

This question already has answers here:
How can I select a variable by (string) name?
(5 answers)
Closed 3 years ago.
I've a list of variable names, how can I print value of it using for loop..
var1="First"
var2="Second"
list=["var1","var2"]
for var in list:
print(var) # I want to print value of var,
present output
var1
var2
What I want is
First
Second
list=["var1","var2"]
You've made a list of strings, not variable names. What you need is
list=[var1,var2]

How to get the value of a variable by executing a string? [duplicate]

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.

How to print the variable name [duplicate]

This question already has answers here:
How to retrieve a variable's name in python at runtime?
(9 answers)
Closed 9 years ago.
I've designed a code that calculates the percentage of 'CG' appears in a string; GC_content(DNA).
Now I can use this code so that it prints the the value of the string with the highest GC-content;
print (max((GC_content(DNA1)),(GC_content(DNA2)),(GC_content(DNA3)))).
Now how would I get to print the variable name of this max GC_content?
You can get the max of some tuples:
max_content, max_name = max(
(GC_content(DNA1), "DNA1"),
(GC_content(DNA2), "DNA2"),
(GC_content(DNA3), "DNA3")
)
print(max_name)
If you have many DNA variables you could place them in a list
DNA_list = [DNA1, DNA2, DNA3]
I would coerce them into a dictionary to associate the name with the raw data and result.
DNA_dict = dict([("DNA%i" % i, {'data': DNA, 'GC': GC_Content(DNA)}) for i, DNA in enumerate(DNA_list)])
Then list comprehension to get the data you want
name = max([(DNA_dict[key]['GC'], key) for key in DNA_dict])[1]
This has the benefit of allowing variable list length
You seem to want
max([DNA1, DNA2, DNA3], key=GC_content)
It's not what you asked for but it seems to be what you need.

Categories