user3440489
user3440489

Reputation: 259

Creating variables in loop and initialize them in Python

I have got 10 parameters to initialize. Following a convention, they are named as a_true, b_true etc. They are not a list or array but independent variables.They need to be initialized from an array of length 1X10.

I intend to do something like this which I know has got its shortcomings:

param=[34,65,...,234] # Contains initialization values
var=['a','b','c','d','e','f','g','h','i','j']
gvalues=[] # Array intended to contain variable names
k=0
for i in var:
    gvalues.append(var[k]+'_true')
    k+=1

This creates an array of elements like a_true, b_true etc. I want to take them as variables rather than as elements of an array and finally initialize them with the values from param. Any possibilities? Sorry from a newbie if it seems trivial.

Output_intended:

[a_true, b_true, ..., j_true]=[34, 65, ... , 234]

Upvotes: 5

Views: 12605

Answers (4)

Chris Martin
Chris Martin

Reputation: 30756

You can use locals() and globals() to dynamically assign variables.

>>> param = range(10)
>>> var = 'abcdefghij'
>>> locals().update({'{}_true'.format(k): v for k, v in zip(var, param)})
>>> c_true
2
>>> f_true
5

Upvotes: 5

user3378649
user3378649

Reputation: 5354

You can through list of letters and concatenate every letter with '_true'

import string

gvalues =[x+'_true' for x in string.ascii_lowercase]
print gvalues

Output:

['a_true', 'b_true', 'c_true', 'd_true', 'e_true', 'f_true', 'g_true', 'h_true', 'i_true', 'j_true', 'k_true', 'l_true', 'm_true', 'n_true', 'o_true', 'p_true', 'q_true', 'r_true', 's_true', 't_true', 'u_true', 'v_true', 'w_true', 'x_true', 'y_true', 'z_true']

In case you need to concatenate it with all letter (Upper case + Lower case.)

import string

gvalues =[x+'_true' for x in string.ascii_letters]
print gvalues

That would give you:

['a_true', 'b_true', 'c_true', 'd_true', 'e_true', 'f_true', 'g_true', 'h_true', 'i_true', 'j_true', 'k_true', 'l_true', 'm_true', 'n_true', 'o_true', 'p_true', 'q_true', 'r_true', 's_true', 't_true', 'u_true', 'v_true', 'w_true', 'x_true', 'y_true', 'z_true', 'A_true', 'B_true', 'C_true', 'D_true', 'E_true', 'F_true', 'G_true', 'H_true', 'I_true', 'J_true', 'K_true', 'L_true', 'M_true', 'N_true', 'O_true', 'P_true', 'Q_true', 'R_true', 'S_true', 'T_true', 'U_true', 'V_true', 'W_true', 'X_true', 'Y_true', 'Z_true']

UPDATE

If you wanna create variable with name 'a_true'. This is not the best to do it. However, you can use Dictionaries. It's a way to map variable using keys to get values.

In this example. we add 'a_true' as a key, to get a value.

d= {'a_true':1, 'b_true':2, 'c_true':3, 'd_true':3}

print d['a_true']

would give you: 1

print d['b_true']

would give you: 2

Upvotes: -1

CyanogenCX
CyanogenCX

Reputation: 404

You can try using a dictionary, with the keys being a_true, ect and the values being the numbers.

dict={}

dict['a_true']=35

etc

or you can do the whole thing in a loop.

Upvotes: 0

Ishamael
Ishamael

Reputation: 12795

It was already discussed here:

Using a string variable as a variable name

In particular, something like this should work:

for k, v in zip(gvalues, params):
    exec('%s = %s' % (k, v))

Upvotes: 2

Related Questions