My code has variables mri1_tasks
and mri2_tasks
. Is there a function to have python interpret 'mri%d_tasks'%num
(where num
is equal to either 1 or 2) as the stored value of mri1_tasks
or mri2_tasks
based on the value of num
?
Asked
Active
Viewed 599 times
0

G Warner
- 1,309
- 1
- 15
- 27
-
2Use a single list instead of multiple variables. – Kevin Sep 30 '15 at 19:09
-
Is there any reason you can't use a list with both strings at separate indices and use the index as the switch? – J. da Silva Sep 30 '15 at 19:10
1 Answers
0
Instead of having two variables, you can store the variables in a dictionary, with variable name as the key.
vars = {
'mri1_tasks': 0,
'mri2_tasks': 1,
}
num = 1
print vars['mri%d_tasks'%num]

jh314
- 27,144
- 16
- 62
- 82