0

Creating a 10x10x10 coordinate grid

#10x10x10
x = np.arange(0,10)
y = np.arange(0,10)
z = np.arange(0,10)

coordinates= []

#makes coordinates for the 10x10x10
for i in range(len(x)):
    for j in range(len(y)):
        for k in range(len(z)):
            coordinates.append((x[i], y[j], z[k]))

So this is my code that has an coordinates from (0, 0, 0), (0, 0, 1)... to (9,9,9).

I want to be able to treat each coordinate as an object that has information. Example would be (0,0,0) has 1 apples, 2 oranges, 3 lemons. (0,0,1) has 1 apples, 0 oranges 0 lemons, so on so forth. Is there a way to store information linked to each coordinate?

user8843
  • 11
  • 1

2 Answers2

1

You could create a dictionary with the coordinates, and make each entry a dictionary with the information you want:

#10x10x10
x = np.arange(0,10)
y = np.arange(0,10)
z = np.arange(0,10)

coordinates = {}
#makes coordinates for the 10x10x10
for i in range(len(x)):
    for j in range(len(y)):
        for k in range(len(z)):
            obj_in_coord = {
                'apples': 4,
                'oranges': 2,
                'lemons': 6
            }
            coordinates[(x[i], y[j], z[k])] = obj_in_coord
print coordinates[(2,3,7)]['apples']  # outputs 4

If you leave it like this, of course, every coordinate will have 4 apples, 2 oranges and 6 lemons. You should apply your logic when creating obj_in_coord.

To access to the information with integers, simply change obj_in_coord to a list:

#10x10x10
x = np.arange(0,10)
y = np.arange(0,10)
z = np.arange(0,10)

coordinates = {}
#makes coordinates for the 10x10x10
for i in range(len(x)):
    for j in range(len(y)):
        for k in range(len(z)):
            info_in_coord = [4, 2, 6]
            coordinates[(x[i], y[j], z[k])] = info_in_coord
print coordinates[(2,3,7)][0]  # outputs 4
francisco sollima
  • 7,952
  • 4
  • 22
  • 38
  • That's very close to what I want. However, is there a way to replace the strings such as 'apples' to integer variables? – user8843 Oct 10 '17 at 18:57
  • Say you want to know the number of applpes in the square (2,3,7). How do you want to access it. With `coordinates[(2,3,7)]['apples']` (you access the property apple) or `coordinates[(2,3,7)][0]` (you access the first propery) – francisco sollima Oct 10 '17 at 19:02
1

you could maintain a dict to have fruits and key is coordinates

fruits = {(0,0,0):{'apples':1, 'oranges':2, 'lemons':3},
          }

or

fruits = {(0,0,0):[1, 2, 3],
          }
galaxyan
  • 5,944
  • 2
  • 19
  • 43