Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 820
lokesh Avatar asked Mar 16 '13 11:03

lokesh


4 Answers

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

like image 127
amaurea Avatar answered Nov 07 '22 11:11

amaurea


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
like image 27
HYRY Avatar answered Nov 07 '22 12:11

HYRY


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
like image 3
Andrew Walker Avatar answered Nov 07 '22 11:11

Andrew Walker


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
like image 3
Wayne Workman Avatar answered Nov 07 '22 12:11

Wayne Workman