If the variables are in a file in valid Python syntax, they can be imported and parsed from there.
from file.py import *
puts them into the local name space. But if we don't use *
we can isolate them
In [1313]: import stack3797 as varN
In [1314]: values = {i:v for i,v in vars(varN).items() if i.startswith('var')}
In [1315]: values
Out[1315]:
{'var8': 19,
'var2': 7,
'var7': 17,
'var0': 3,
'var6': 15,
'var3': 9,
'var9': 21,
'var1': 5,
'var5': 13,
'var4': 11}
Then any other the other answers that work from locals()
can be used to convert this dictionary to an array.
If the varN
labeling is consecutive, we can figure out the size of the resulting array from the size of this dictionary. Otherwise we might have to find the maximum value first.
or we can make a list of lists right away:
values = [[int(i[3:]),v] for i,v in vars(varN).items() if i.startswith('var')]
and turn it into a array with
In [1328]: np.array(values).T
Out[1328]:
array([[ 8, 6, 0, 1, 3, 5, 2, 7, 9, 4],
[19, 15, 3, 5, 9, 13, 7, 17, 21, 11]])
That still needs a x[i] = value
mapping.