Create 3D array using Python

280,762

Solution 1

You should use a list comprehension:

>>> import pprint
>>> n = 3
>>> distance = [[[0 for k in xrange(n)] for j in xrange(n)] for i in xrange(n)]
>>> pprint.pprint(distance)
[[[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]]]
>>> distance[0][1]
[0, 0, 0]
>>> distance[0][1][2]
0

You could have produced a data structure with a statement that looked like the one you tried, but it would have had side effects since the inner lists are copy-by-reference:

>>> distance=[[[0]*n]*n]*n
>>> pprint.pprint(distance)
[[[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]]]
>>> distance[0][0][0] = 1
>>> pprint.pprint(distance)
[[[1, 0, 0], [1, 0, 0], [1, 0, 0]],
 [[1, 0, 0], [1, 0, 0], [1, 0, 0]],
 [[1, 0, 0], [1, 0, 0], [1, 0, 0]]]

Solution 2

numpy.arrays are designed just for this case:

 numpy.zeros((i,j,k))

will give you an array of dimensions ijk, filled with zeroes.

depending what you need it for, numpy may be the right library for your needs.

Solution 3

The right way would be

[[[0 for _ in range(n)] for _ in range(n)] for _ in range(n)]

(What you're trying to do should be written like (for NxNxN)

[[[0]*n]*n]*n

but that is not correct, see @Adaman comment why).

Solution 4

d3 = [[[0 for col in range(4)]for row in range(4)] for x in range(6)]

d3[1][2][1]  = 144

d3[4][3][0]  = 3.12

for x in range(len(d3)):
    print d3[x]



[[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, 144, 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, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [3.12, 0, 0, 0]]
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Solution 5

"""
Create 3D array for given dimensions - (x, y, z)

@author: Naimish Agarwal
"""


def three_d_array(value, *dim):
    """
    Create 3D-array
    :param dim: a tuple of dimensions - (x, y, z)
    :param value: value with which 3D-array is to be filled
    :return: 3D-array
    """

    return [[[value for _ in xrange(dim[2])] for _ in xrange(dim[1])] for _ in xrange(dim[0])]

if __name__ == "__main__":
    array = three_d_array(False, *(2, 3, 1))
    x = len(array)
    y = len(array[0])
    z = len(array[0][0])
    print x, y, z

    array[0][0][0] = True
    array[1][1][0] = True

    print array

Prefer to use numpy.ndarray for multi-dimensional arrays.

Share:
280,762
Laís Minchillo
Author by

Laís Minchillo

Updated on March 14, 2021

Comments

  • Laís Minchillo
    Laís Minchillo about 3 years

    I would like to create a 3D array in Python (2.7) to use like this:

    distance[i][j][k]
    

    And the sizes of the array should be the size of a variable I have. (nnn)

    I tried using:

    distance = [[[]*n]*n]
    

    but that didn't seem to work.

    I can only use the default libraries, and the method of multiplying (i.e.,[[0]*n]*n) wont work because they are linked to the same pointer and I need all of the values to be individual

  • Amadan
    Amadan almost 12 years
    Not good. It will contain references to same array. Try this: a = [[0] * 3] * 3; a[0][0] = 1; print a
  • Bruno Kim
    Bruno Kim almost 12 years
    Don't do that, or all of them will point to the same reference! Just try distance[1][2][0].append(1)
  • Laís Minchillo
    Laís Minchillo almost 12 years
    Oh, I thought this was one of the deafult libraries. I can't use anything other than that.
  • mata
    mata almost 12 years
    no, unfortunately it's an external library. but usually extremely well suited if you need to process (large) arrays of numeric data. Specially if speed is an issue.
  • Laís Minchillo
    Laís Minchillo almost 12 years
    Yes, my problem with that is if I change one of them, it will change all of them too. I need them to be separate elements.
  • csteel
    csteel about 4 years
    For Python3, you can change xrange to range.
  • gocen
    gocen about 4 years
    I define an array like this: new_array= numpy.zeros((6340,200,200)) but it tooks a space of 1.9GB at memory. Is it normal?
  • Filip Franik
    Filip Franik over 3 years
    @gocen Yup sounds about right. Default value returned by numpy zeroes is numpy.float64 and 6340*200*200*64bits = 2.0288 gigabytes. You can provide an extra argument dtype to zeros to change the type it returns and save some RAM this way.
  • Mihaela Grigore
    Mihaela Grigore almost 3 years
    I used the 'copy-by-reference' method without knowing this is what actually happened on the back-end. Therefore, I ended up having the 'side-effects' (all fields in my variable ended up having the same value) and did not know why. Thank you for the explanation.