像C ++一样的python 3d数组

时间:2015-11-05 17:57:27

标签: python arrays multidimensional-array

我想制作一个像这样的三维数组:

treedarray = [[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]]

在此表中,使用以下内容(可轻松)访问每个值:

treedarray [a] [b] [c]

我想知道是否有命令更容易做到这一点。

提前致谢。

1 个答案:

答案 0 :(得分:1)

使用Numpynumpy.zeros(),您可以使用值列表定义形状作为第一个参数。 e.g。

import numpy as np

treedarray = np.zeros([3,3,3])
print treedarray

输出:

[[[ 0.  0.  0.]
  [ 0.  0.  0.]
  [ 0.  0.  0.]]

 [[ 0.  0.  0.]
  [ 0.  0.  0.]
  [ 0.  0.  0.]]

 [[ 0.  0.  0.]
  [ 0.  0.  0.]
  [ 0.  0.  0.]]]

并且可以使用treedarray[a][b][c]

访问假装值