Hi,

I've dabbled in some programming in the past, but nothing serious. I'm trying to create an array of objects in python 2.6 (VPython).

What I want to do is create a 2d grid of sphere()'s. But I want to be able to manipulate each one individually after they are created. So what I would like to end up with is a set of objects like:

ball1=sphere(x=x1, y=y1, z=0)
ball2=sphere(x=x2, y=y2, z=0)
..... etc.

My first thought was to do a nested pair of FOR loops

for i in range (0, 100):
     for j in range (0, 100):
          ballij=sphere(x=i, y=j, z=0)

but I can't figure out how to set up the variable so that it creates a unique variable for each iteration that I can address each sphere. Basically, I can't figure out how to set up "ballij". Not sure if it's even possible in Python. I've done it before in a specialized scripting language based in fortran though.

So someone suggested using an array. But I'm not sure how that would be set up. If it was just an array of numbers, that would be simple, but not sure how to do this.

Any ideas.

A dictionary will serve you better ...

# create a large number of spheres referencing them
# with a (x, y):sphere_object dictionary pair

import visual as vs

spheres = {}
for x in range (0, 100):
    for y in range (0, 100):
        z = 0
        spheres[(x, y)] = vs.sphere(pos=(x, y, z), radius=1)

# to select a particular sphere use
x = 45
y = 55
spheres[(x, y)].color = vs.color.red

Interesting, I've never used 2D arrays in Python but I would have hacked it to be useable in 'C-like' manner:

class sphere():
    def __init__(self, x=0.0, y=0.0, z=0.0):
        self.x = x
        self.y = y
        self.z = z

    def __str__(self):
        return "Coords: %s %s %s"%(self.x,self.y,self.z)


n = 10
m = 10
myArray = []

for i in xrange(n):
    myArray.append(map(sphere, xrange(m)))
    for j in xrange(m):
        myArray[i][j] = sphere(float(i), float(j), 0.0)

print "i=2,j=3: %s"%myArray[2][3]
print "i=6,j=1: %s"%myArray[6][1]
print "i=4,j=9: %s"%myArray[4][9]

Would this be seen as unPythonic? Or is it bad for other reasons? Just curious...

A dictionary will serve you better ...

# create a large number of spheres referencing them
# with a (x, y):sphere_object dictionary pair

import visual as vs

spheres = {}
for x in range (0, 100):
    for y in range (0, 100):
        z = 0
        spheres[(x, y)] = vs.sphere(pos=(x, y, z), radius=1)

# to select a particular sphere use
x = 45
y = 55
spheres[(x, y)].color = vs.color.red

Ah, that makes sense now. Thank you very much.

You can include coordinate z into your key tuple by simply expanding vegaseat's code:

# create a large number of spheres referencing them
# with a (x, y, z):sphere_object dictionary pair

import visual as vs

spheres = {}
for x in range (0, 100):
    for y in range (0, 100):
        z = 0
        spheres[(x, y, z)] = vs.sphere(pos=(x, y, z), radius=1)

# to select a particular sphere use
x = 45
y = 55
z = 0
spheres[(x, y, z)].color = vs.color.red

Interesting, I've never used 2D arrays in Python but I would have hacked it to be useable in 'C-like' manner:

class sphere():
    def __init__(self, x=0.0, y=0.0, z=0.0):
        self.x = x
        self.y = y
        self.z = z

    def __str__(self):
        return "Coords: %s %s %s"%(self.x,self.y,self.z)


n = 10
m = 10
myArray = []

for i in xrange(n):
    myArray.append(map(sphere, xrange(m)))
    for j in xrange(m):
        myArray[i][j] = sphere(float(i), float(j), 0.0)

print "i=2,j=3: %s"%myArray[2][3]
print "i=6,j=1: %s"%myArray[6][1]
print "i=4,j=9: %s"%myArray[4][9]

Would this be seen as unPythonic? Or is it bad for other reasons? Just curious...

You can create myarray[x][y][z], but you still have to associate with the sphere object, that is where the dictionary mapping comes in handy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.