3

Im aware that this may come up as a duplicate but so far I haven't found (or should that be understood) an answer to what Im looking for.

I have a list of strings and want to convert each one into a variable name which I then assign something to. I understand that I may need a dict for this but I am unfamiliar with them as I am relatively new to python and all the examples I have seen so far deal with values whilst I'm trying something different.

Im after something like:

list = ['spam', 'eggs', 'ham']

for i in range(len(list)):
    list[i] = rat.readColumn(ratDataset, list[i])

where the first list[i] is a variable name and not a string. The second list[i] is a string (and for context is the name of a column Im reading from a raster attribute table (rat))

Essentially I want each string within the list to be set as a variable name.

The idea behind this is that I can create a loop without having to write out the line for each variable I want, with matching rat column name (the string). Maybe there is a beer way of doing this than I am suggesting?

3

3 Answers 3

3

Try the following:

lst = ['spam', 'eggs', 'ham']
d = {}   # empty dictionary
for name in lst:
    d[name] = rat.readColumn(ratDataset, name)

Do not use list for your identifiers as it is a type identifier and you would mask its existence. The for loop can iterate directly for the elements inside -- no need to construct index and use it aganist the list. The d['spam'] will be one of your variables.

Although, it is also possible to create the real variable names like spam, eggs, ham, you would not probably do that as the effect would be useless.

Sign up to request clarification or add additional context in comments.

1 Comment

:) Arthur was 14 seconds faster.
1

Here comes a simple dictionary use :

variables = ['spam', 'eggs', 'ham']
data = {}

datum = 0

for variable in variables:
    data[variable] = datum
    datum+=1

print(data)
print("value : ",data[variables[2]])

It gives as result :

{'eggs': 1, 'ham': 2, 'spam': 0}
value : 2

NB : don't use list as a variable name, list is a type identifier that you can use to transform an object into a list if possible (list("abc")==['a', 'b', 'c']) and you are overriding it with your value list right now.

Comments

0

one way is setting the variable name as a string and changing a part or all of it via format() method and then using the string as a varibale via vars()[STRING]

import numpy as np
X1= np.arange(1,10)                                                                                                                                
y1=[i**2 for i in X1]                                                                                                                                
X2= np.arange(-5,5)                                                                                                                                
y2=[i**2 for i in X2]                                                                                                                          
for i in range(1,3): 
    X = 'X{}'.format(i) 
    y = 'y{}'.format(i) 
    print('X_{}'.format(i) , vars()[X])
    print('y_{}'.format(i) , vars()[y])

Output:


X_1 [1 2 3 4 5 6 7 8 9]
y_1 [1, 4, 9, 16, 25, 36, 49, 64, 81]
X_2 [-5 -4 -3 -2 -1  0  1  2  3  4]
y_2 [25, 16, 9, 4, 1, 0, 1, 4, 9, 16]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.