lokesh
lokesh

Reputation: 330

How to add elements to 3 dimensional array in python

I am trying to store data in three-dimensional array i.e, x[0][0][0] in Python. How to initialize x, and add values to it? I have tried this:

x=[]
x[0][0][0]=value1 
x[0][0].append(value1)

both lines are giving out of range error. How to do it? I want it like: x[0][0][0]=value1, x[1][0][0]=value2, x[0][1][0]=value3 etc. How to achieve this in Python?

I am looking to generate this kind of array:

x=[[[11,[111],[112]],[12],[13]],[[21,[211],[212]],[22],[23],[24]],[[31],[32]]]
x[0][0][0] will give 11
x[1][0][0]  21
x[0][0][1] 111

etc.

Upvotes: 13

Views: 114283

Answers (7)

amaurea
amaurea

Reputation: 5067

I recommend using numpy for multidimensional arrays. It makes it much more convenient, and much faster. This would look like:

import numpy as np
x = np.zeros((10,20,30)) # Make a 10 by 20 by 30 array
x[0,0,0] = value1

Still, if you don't want to use numpy, or need non-rectangular multi-dimensional arrays, you will need to treat it as a list of lists of lists, and initialize each list:

x = []
x.append([])
x[0].append([])
x[0][0].append(value1)

Edit: Or you could use the compact notation shown in ndpu's answer (x = [[[value1]]]).

Upvotes: 31

Wayne Workman
Wayne Workman

Reputation: 541

I just came up with this, it's more dynamic and simple.

# Define how large to make the object.
size = 3

# Build the three dimensional list.
memory = []
for x in range(0,size):
    memory.append([])
    for y in range(0,size):
        memory[x].append([])
        for z in range(0,size):
           memory[x][y].append(0) # Fill with zeros.

# Iterate through all values.
for x in range(0,size):
    for y in range(0,size):
        for z in range(0,size):
            print 'memory[' + str(x) + '][' + str(y) + '][' + str(z) + ']=' + str(memory[x][y][z])

# Example access.
print 'Example access:'
print 'memory[0][1][2]=' + str(memory[0][1][2])

Output:

memory[0][0][0]=0
memory[0][0][1]=0
memory[0][0][2]=0
memory[0][1][0]=0
memory[0][1][1]=0
memory[0][1][2]=0
memory[0][2][0]=0
memory[0][2][1]=0
memory[0][2][2]=0
memory[1][0][0]=0
memory[1][0][1]=0
memory[1][0][2]=0
memory[1][1][0]=0
memory[1][1][1]=0
memory[1][1][2]=0
memory[1][2][0]=0
memory[1][2][1]=0
memory[1][2][2]=0
memory[2][0][0]=0
memory[2][0][1]=0
memory[2][0][2]=0
memory[2][1][0]=0
memory[2][1][1]=0
memory[2][1][2]=0
memory[2][2][0]=0
memory[2][2][1]=0
memory[2][2][2]=0
Example access:
memory[0][1][2]=0

Upvotes: 3

Interator. I use function lambda for create matrix with [ [ 0 for j in range(n)] for i in range(m) ] in python 3. In the version 2 a used map functions. In this moment think like use array module (pure python) to create matrixes.

>>> arr = lambda m,n,l : [ [ [0 for k in range(l)] for j in range(n)] for i in range(m) ]
>>> m = arr(2,3,4)
>>> m
    [[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]

Upvotes: 0

user2099484
user2099484

Reputation: 4559

A compilation of techniques above with respect to effectively creating a two-dimensional dict is:

from collections import defaultdict
x = defaultdict(lambda :defaultdict())
x["a"]["b"] = 123
x["a"]["c"] = 234
x["b"]["a"] = 234    
x["b"]["c"] = 234    
x["c"]["a"] = 234
x["c"]["b"] = 234
for i in x: 
    for j in x[i]: print i, j, x[i][j]

Produces:

a c 234
a b 123
c a 234
c b 234
b a 234
b c 234

To increase the number of dimensions (e.g., to three-dimensions), simply increase the lambda "nest" as HYRY shows:

x = defaultdict(lambda :defaultdict(lambda :defaultdict(int)))

Upvotes: 1

HYRY
HYRY

Reputation: 97261

If you are creating some 3D sparse array, you can save all the data in a dict:

x={}
x[0,0,0] = 11
x[1,0,0] = 21
x[0,1,1] = 111

or:

from collections import defaultdict
x = defaultdict(lambda :defaultdict(lambda :defaultdict(int)))

x[0][0][0] = 11
x[1][0][0] = 21
x[0][0][1] = 111

Upvotes: 7

ndpu
ndpu

Reputation: 22561

>>> x=[[[[]]]]
>>> x[0][0][0]=0
>>> x
[[[0]]]
>>> x[0][0].append(1)
>>> x
[[[0, 1]]]

Upvotes: 2

Andrew Walker
Andrew Walker

Reputation: 42450

If you can use numpy, you can initialize a fixed size array as:

import numpy
x = numpy.zeros((i, j, k))

where i, j and k are the dimensions required.

You can then index into that array using slice notation:

x[0, 0, 0] = value1
x[1, 0, 0] = value2

Upvotes: 3

Related Questions