0

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)
martineau
  • 119,623
  • 25
  • 170
  • 301
Andrea Diaz
  • 1,168
  • 4
  • 13
  • 29

2 Answers2

10

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.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
6

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.

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • Why don't you recommend it ? that was what I was looking for, thanks – Andrea Diaz Oct 27 '12 at 02:48
  • 1
    For all the reasons already given (see uubuntu's answer). It is bad style. It makes your code hard to read/maintain. You are likely going to end up using the same trick to operate on your large number of variables which, while it works, there are far better/faster/more readable ways to do this in python. – tacaswell Oct 27 '12 at 02:54
  • Andrea: This is not a good idea. At the very least, use a dictionary, like `d = {}`, then `d["var_%d" % j] = j`. What you are otherwise doing with the code is using your local namespace as a dictionary. (Note that it technically *is* a dictionary, it is just one that it shares with all your normal variables!) – David Robinson Oct 27 '12 at 02:56