Create And Use Multiple Variables In A Loop In Python
Solution 1:
First of all, do not use globals()
for this (or in fact almost never); create your own dictionary to store your dynamically created values:
vars = {}
vars['blabla'] = 123
The reason you are getting the KeyError
though is that you're trying to read a variable from globals()
that doesn't exist (but this would also happen with a custom dict). This line:
globals()['x%s' % j] += P[i][j]*globals()['x%s' % i]
is actually short for:
globals()['x%s' % j] = globals()['x%s' % j] + P[i][j]*globals()['x%s' % i]
but globals()['x%s' % j]
is not defined yet. So you're trying to add something to something that doesn't exist.
Instead, you need to do something like the following before you do the +=
operation:
if'x%s' % j notinglobals():
globals()['x%s' % j] = 0
BUT, if you do it properly and use a dict
, and some other enhancements, it would look like this:
# it's nicer to use floats uniformly, not a mix of ints and floats;# it's also safer because in Python 2.x, int divided by float will yield a floored down int
P = [[0.0, 0.0, 0.0, 0.5, 0.0, 0.5],
[0.1, 0.1, 0.0, 0.4, 0.0, 0.4],
[0.0, 0.2, 0.2, 0.3, 0.0, 0.3],
[0.0, 0.0, 0.3, 0.5, 0.0, 0.2],
[0.0, 0.0, 0.0, 0.4, 0.6, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.4, 0.6]]
vars = {}
for j inrange(len(P)):
vars['x%s' % j] = 0for i inrange(len(P[0])):
vars.setdefault('x%s' % j, 0.0) # shorthand for the `if` I describedvars['x%s' % j] += P[i][j] * vars['x%s' % i]
Also, since vars
seems to end up just containing the keys x0
to x5
, I would rather use a list
instead;
X = [0] * len(P) # creates [0, 0, 0, 0, 0, 0]for j inrange(len(P)):
for i inrange(len(P[0])):
X[j] += P[i][j] * X[i]
This way, you don't even need the setdefault
call.
Keep in mind, though, that I'm not familiar with the mathematical algorithm/computation you're trying to implement, so I'm also not able to spot any non-technical bugs or shortcomings in your code.
Post a Comment for "Create And Use Multiple Variables In A Loop In Python"