Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 写作及;读恩达雷_Python_Arrays_Numpy - Fatal编程技术网

Python 写作及;读恩达雷

Python 写作及;读恩达雷,python,arrays,numpy,Python,Arrays,Numpy,我想用形状(5,2)保存一个数组,这个数组名为sorted_cube_station_list 在打印中看起来还可以,但是当我用numpy.tofile保存它,然后用numpy.fromfile读取它时,它变成了一个1d数组 你能帮我吗? 将numpy导入为num nx=5 ny=5 nz=5 stations=['L001','L002','L003','L004','L005'] for x in range(nx): for y in range (ny): f

我想用形状(5,2)保存一个数组,这个数组名为sorted_cube_station_list

在打印中看起来还可以,但是当我用numpy.tofile保存它,然后用numpy.fromfile读取它时,它变成了一个1d数组

你能帮我吗? 将numpy导入为num

nx=5
ny=5
nz=5
stations=['L001','L002','L003','L004','L005']

for x in range(nx):
    for y in range (ny):
         for z in range (nz):
             cube_station_list = []
             i=-1
             for sta in stations:
                 i=i+1
                 cube=[int(i), num.random.randint(2500, size=1)[0]]
                 cube_station_list.append(cube)
             cub_station_list_arr=num.asarray(cube_station_list)
             sorted_cube_station_list_arr=cub_station_list_arr[cub_station_list_arr[:, 1].argsort()]
             print x,y,z, sorted_cube_station_list_arr
             num.ndarray.tofile(sorted_cube_station_list_arr,str(x)+'_'+str(y)+'_'+str(z)

我建议您使用
np.save

a = np.ones(16).reshape([8, 2])
np.save("fileName.npy", a)
请参阅:第一个参数不能是要保存的变量,而是要保存它的文件的路径。因此,使用
np.save(yourray)


您可以使用
np.load(pathToArray)

加载保存的数组。不使用np.save()的任何原因?此外,缩进(8个空格)使代码难以读取,您能否仅使用4个?numpy.ndarray“对象没有属性“write”,这就是我没有使用它的原因@M.massaoff topic,但是您可以将嵌套的for循环替换为np.ndindex((nx,ny,nz))中x,y,z的