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]
3
  • in for loop vari should be var !!? Commented Oct 31, 2014 at 6:25
  • 1
    @user3440489 show the output what you want. you are not clear yet. Better use i+'_true' in for loop remove k. Commented Oct 31, 2014 at 6:29
  • Just a note: You are probably better off making them into a list or dict or other structure, instead of using separate variables. Commented Oct 31, 2014 at 6:54

4 Answers 4

6

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

-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

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.