1

I would like to append data in to array like m[][][] with the help of for three loop.

for i in range (4):
    for j in  range (6):
        for k in range (10):
            m[i][j][k]=i*j*k
print(m)

3 Answers 3

3
m = [[[i*j*k for k in range(10)] for j in range(6)] for i in range(4)]
Sign up to request clarification or add additional context in comments.

Comments

1

Since m is not defined when you start your loop python does not know how to access the [i][j][k]-th element.

m = [] # init the first level
for i in range (4):
    m.append([]) # init m[i]
    for j in  range (6):
        m[i].append([]) #  init m[i][j]
        for k in range (10):
            m[i][j].append( i*j*k ) # add m[i][j] the k-th element
print(m)

Comments

1

You can also use your code

import numpy
m = numpy.zeros((4,6,10))

for i in range (4):
    for j in  range (6):
        for k in range (10):
            m[i][j][k]=i*j*k

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.