我想在三个循环的帮助下将数据附加到像m[][][]
这样的数组中。
for i in range (4):
for j in range (6):
for k in range (10):
m[i][j][k]=i*j*k
print(m)
答案 0 :(得分:3)
m = [[[i*j*k for k in range(10)] for j in range(6)] for i in range(4)]
答案 1 :(得分:1)
由于在启动循环时未定义m
,因此python不知道如何访问[i][j][k]
- 元素。
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)
答案 2 :(得分:1)
您也可以使用您的代码
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