you should not do this, but you can if you really want
for j in range(1,10,1):
exec('var_%d = j'%j)
Can I stress again you should not do this, but I see no reason to hide the ability to hang your self.
Given what you are trying to do, I would look in to morphological operations. There are better algorithms for what you are trying to do. (It looks like what you want to do is already in scipy, examples).
Quoting unutbu:
Maybe I should try to convince you that having variables named variable1
, variable2
, etc. is a bad idea. Since these variables are numbered, they probably have some close relationship to each other. Perhaps they are all numbers. Suppose you wanted to increment all of them by one. You'd have to write repetitious code such as
variable1 += 1
variable2 += 1
variable3 += 1
... etc
instead of
for i in range(1,10,1):
x[i] += 1
Or perhaps you want to feed all those variables into a function. You'd have to write
func(variable1, variable2, variable3, ...)
instead of
func(**x)
Conclusion: Having numbered variables names is a pain!
Save yourself from pain by using a dict or a list.