我试图在多维数组中使用索引元素(数组)。我的代码如下:
table= [[[[ 0 for i in range(5) ] for j in range(5)] for k in range(5)] for l in range(5) ]
for d1 in range(5):
for d2 in range(5):
for d3 in range(5):
for d4 in range(5):
table[d1][d2][d3][d4]= [d1,d2+1,d3+2,d4]
print "table", table
print "table[1][1][1][1]", table[1][1][1][1]
在终端,表[1] [1] [1] [1] = [1,2,3,1]。
但是,如果有更多数据,我想保存表和读表,例如50 * 50 * 50 * 50。我尝试使用np.savetxt和其他方法如下,表[1] [1] [1] [1]是随机的,如“'”“[[”等等。
def save_array(output_file,array):
with open(output_file, 'w') as file:
for r in array:
file.write(" ".join(map(str,r)) + "\n")
def read_file(input_file):
with open(input_file, 'r') as f:
array = []
for line in f:
if line.rstrip():
data = line.split()
array.append(data)
return array
如果有人能提出一些建议我真的很感激。