PikalaxALT
PikalaxALT

Reputation: 337

Loop through variables from a module

My goal is to produce a matrix from a series of 18090 lists of size 256 each. Each list is referenced as newmodule.xlist#####. How do I loop through the variables in newmodule.py to create the super matrix?

Upvotes: 3

Views: 193

Answers (1)

shx2
shx2

Reputation: 64318

The name of the variable can be constructed like this:

'xlist%05d' % i

You can grab the variable from the module like this:

getattr(newmodule, 'xlist%05d' % i)

To create a "2dim" list, i.e. a list of lists, do:

mat = [ getattr(newmodule, 'xlist%05d' % i) for in range(18090) ]

You'd probably want to convert that to a numpy 2dim array:

mat = numpy.array(mat)

or a numpy matrix:

mat = numpy.mat(mat)

Upvotes: 2

Related Questions