Say I have a thousand variables. v1,v2,v3,...,v1000, but they are just variables not in a list.
Is there any way I can loop them.
I know in c I can use Marcos.
But how can I do it in python? Any hints will be helpful.
Say I have a thousand variables. v1,v2,v3,...,v1000, but they are just variables not in a list.
Is there any way I can loop them.
I know in c I can use Marcos.
But how can I do it in python? Any hints will be helpful.
Alternative way is to use vars(). for example:
v1='dd';
v2=32;
v4=12;
import re
re_p = re.compile('^v\d+')
var_list = vars().copy()
for a_var in (v for v in var_list if re_p.match(v)):
print(a_var, var_list[a_var])
Prints:
v1 dd
v4 12
v2 32
v1 = 'foo'
v2 = 'bar'
v3 = '42'
for i in range(1,4):
print vars()['v' + str(i)]