Sure, you can have nested lists to represent multidimensional arrays, but that seems costly...
[[0, 1], [2, 3]]
Is there some way to "encode" and "decode" the coordinate into a single number, and use that number to lookup the corresponding element?
[0, 1, 2, 3]
This needs to work with n-dimensions, not just two, and the best I could come up with for encoding is:
def getcellindex(self, location):
cindex = 0
cdrop = self.gridsize # where self.gridsize is the number of cells
for index in xrange(self.numdimensions): # where self.numdimensions is the number of dimensions
# where self.dimensions is a tuple of the different sizes of the corresponding dimension
cdrop /= self.dimensions[index]
cindex += cdrop * location[index]
return cindex
There're probably ways to optimize this, but more importantly, how do I reverse the process? And, does this function work?