2015-11-05 52 views
1

我想使一个3维阵列这样一个:蟒3D阵列类似于C++

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] 

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

在此先感谢。

+4

你似乎对Python比较陌生。请查看[Numpy](http://www.numpy.org/)。这是一个数字内容包,并提供了多维数组中的所有内容。如果你想在Python中进行数字化,基本上没有办法。 (顺便说一下,你可以通过'treedarray = numpy.zeros((3,3,3)''')使用numpy来开启你的数组。 – Carsten

回答

1

使用Numpynumpy.zeros()您可以定义具有值列表的形状作为第一个参数。例如

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]