Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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]
share|improve this question
    
in for loop vari should be var !!? – d-coder Oct 31 '14 at 6:25
    
Oh thanks, corrected – user3440489 Oct 31 '14 at 6:26
1  
@user3440489 show the output what you want. you are not clear yet. Better use i+'_true' in for loop remove k. – Vishnu Upadhyay Oct 31 '14 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. – BrenBarn Oct 31 '14 at 6:54
up vote 2 down vote accepted

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
share|improve this answer
    
Thanks, it works exactly how I wanted it to! – user3440489 Oct 31 '14 at 6:39

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))
share|improve this answer

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.

share|improve this answer
    
Why did you downmark me? – CyanogenCX Oct 31 '14 at 6:50

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

share|improve this answer
    
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 6:33
    
check the update – user3378649 Oct 31 '14 at 6:36
    
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 1 '14 at 10:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.