TheStrangeQuark
TheStrangeQuark

Reputation: 2405

How to make list variable names into strings

I think this should be simple but I'm not sure how to do it. I have a tuple of list variables:

A = (a,b,c)

where

a = [1,2,3,...]
b = [2,4,6,4,...]
c = [4,6,4,...]

And I want to make a tuple or list where it is the names of the variables. So,

A_names = ('a','b','c')

How could I do this? My tuple will have more variables and it is not always the same variables. I tried something like

A_names = tuple([str(var) for var in A])

but this did not work.

Upvotes: 2

Views: 140

Answers (5)

Woodsy
Woodsy

Reputation: 3377

My connection was messed up so I couldn't post this earlier but I believe this solves your problem with out using a dictionary.

import inspect

def retrieve_name(var):
    local_vars = inspect.currentframe().f_back.f_locals.items()
    return [var_name for var_name, var_val in local_vars if var_val is var]

a = [1,2,3]
b = [2,4,6,4]
c = [4,6,4]

a_list = (a,b,c)
a_names = []

for x in a_list:
    a_names += (retrieve_name(x)[0])

print a_names

outputs ['a', 'b', 'c']

Upvotes: 1

Ernesto
Ernesto

Reputation: 295

A = {'a' : [1,2,3,...],
     'b' : [2,4,6,4,...],
     'c' : [4,6,4,...]}

A_names = A.keys()

for name in A_names:
    print(A[name])

Then you can always add a new value to the dictionary by saying:

A.update({'d' : [3,6,3,8,...], 'e' : [1,7,2,2,...]})

Alternatively, you can change the value of an item by going:

A.update({'a' : [1,3,2,...]})

To print a specific value, you can just type:

print(A['c'])

Upvotes: 0

NDevox
NDevox

Reputation: 4086

Assuming you want dynamic/accessible names, you need to use a dictionary.

Here is an implementation with a dictionary:

my_variables = {'a': [1,2,3,...],
                'b': [2,4,6,4,...],
                'c': [4,6,4,...]}

my_variable_names = my_variables.keys()

for name in my_variable_names:
    print(my_variables[name])

Upvotes: 1

jcoppens
jcoppens

Reputation: 5440

Just out of academic interest:

dir() will give you a list of the variables currently visible,
locals() gives the list of local variables
globals() (guess)

Note that some unexpected variables will show up (starting and ending in __), which are already defined by Python.

Upvotes: 0

paidhima
paidhima

Reputation: 2392

The problem with what you are asking is that doing A = (a, b, c) does not assign the variables "a", "b" and "c" to the tuple A. Rather, you are creating a new reference to each of the objects referred to by those names.

For example, if I did A = (a,), a tuple with a single object. I haven't assigned the variable "a". Instead, a reference is created at position 0 in the tuple object. That reference is to the same object referred to by the name a.

>>> a = 1
>>> b = 2
>>> A = (a, b)
>>> A
(1, 2)
>>> a = 3
>>> A
(1, 2)

Notice that assigning a new value to a does not change the value in the tuple at all.

Now, you could use the locals() or globals() dictionaries and look for values that match those in A, but there's no guarantee of accuracy since you can have multiple names referring to the same value and you won't know which is which.

>>> for key, val in locals().items():
    if val in A:
        print(key, val)


('a', 1)
('b', 2)

Upvotes: 1

Related Questions