1

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.

1
  • 4
    You seem to be rather new to Python. Please check out Numpy. It's a package for numerical stuff, and provides everything you could wish for in a multidimensional array. There's basically no way around it if you want to do numericas in Python. (By the way, you could ger your array using numpy via treedarray = numpy.zeros((3, 3, 3))) Commented Nov 5, 2015 at 18:00

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.