5

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]
user3440489
  • 259
  • 1
  • 4
  • 13

4 Answers4

5

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
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
2

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))
Community
  • 1
  • 1
Ishamael
  • 12,583
  • 4
  • 34
  • 52
0

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.

CyanogenCX
  • 414
  • 6
  • 17
-1

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

user3378649
  • 5,154
  • 14
  • 52
  • 76
  • Thanks for help. But this array of variable names is created using for loop as well using append (though this more elegant). Now what if I want to use these elements as variables and initialize them like a_true=45? – user3440489 Oct 31 '14 at 06:33
  • why u gave (-1) for all other answers. This solution solve the problem, but it's not the best. it should be ranked (0) in case u don't like it, (-x) for really unrelated soltion (which is not the case for this one or any of the others) – user3378649 Nov 01 '14 at 10:30