Creating multiple variables [duplicate]
I would like to create 10 variables with one for
in Python (not an array).
Something like this:
for i in range(1,10,1)
variable i = 100 / i
print variable i
I want to create variable names, in this case:
variable1
, variable2
, variable3
,..., variable10
I don't want an array
I have a map of coordinates (640 x 480). I am identifying the coordinates where the pixel color is white. There are 3 balls in the ground, I would like to get the central coordinate of each ball, so if the coordinate that is been evaluated is close from the last one, the x and y coordinate will be averaged, if the coordinate belongs to a new ball the coordinate belongs to a new group of coordinates will be evaluated.
p=0
h=0
for j in range(1,480,1):
for i in range(1,640,1):
c=cv.Get2D(image,j,i)
if c[0] == 255:
old_coord X = new_coord x
new_coord x += [(i,j)]
if (old_coord x - 5 < new_coord x < old_coord x + 5)
averx x = averx x + i
avery x = averx x + j
count x = count x + 1
else
p = p + 1
new_coord x += [(i,j)]
average(averx1 , count 1)
average(avery1 , count 1)
average(averx2 , count 2)
average(avery2 , count 2)
Never create numbered variable names (1 or 2 maybe, but never 9.) Instead use a list or a dict:
x = dict()
for i in range(1,10,1)
x[i] = 100 / i
print x[i]
(I used a dict in this case because i
starts at 1.) However, depending on how you wish to use x
further, you may want to use a list instead.
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.
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.