Léo Pourcelot
Léo Pourcelot

Reputation: 35

python 3d array like in C ++

I'd like to make a 3 dimensional array like this one :

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]]]

in this table, every value is (easily) accessible by using :

treedarray [a] [b] [c]

I'd like to know if there is a command to do that more easily.

Thanks in advance.

Upvotes: 1

Views: 78

Answers (1)

Shapi
Shapi

Reputation: 5593

Using Numpy and numpy.zeros() you can define the shape with a list of values as the first param. e.g.

import numpy as np

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

Outputs:

[[[ 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.]]]

And can access the pretended value using, treedarray[a][b][c].

Upvotes: 1

Related Questions