<p>当然,可以使用嵌套列表来表示多维数组,但这似乎很昂贵。。。在</p>
<pre><code>[[0, 1], [2, 3]]
</code></pre>
<p>有没有办法把坐标“编码”和“解码”成一个数字,然后用这个数字来查找相应的元素?在</p>
^{pr2}$
<p>这需要使用n维,而不仅仅是2维,我能想到的最好的编码方法是:</p>
<pre><code>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
</code></pre>
<p>可能有一些方法可以优化这一点,但更重要的是,我如何逆转这个过程?还有,这个功能有用吗?在</p>