使用np.savetxt将数组保存为列
我想做一件可能非常简单的事情。我想用'np.savetxt'把三个数组保存到一个文件里,按列的形式保存。当我这样做的时候
x = [1,2,3,4]
y = [5,6,7,8]
z = [9,10,11,12]
np.savetxt('myfile.txt', (x,y,z), fmt='%.18g', delimiter=' ', newline=os.linesep)
数组保存成了这样
1 2 3 4
5 6 7 8
9 10 11 12
但我想要的是这样的格式
1 5 9
2 6 10
3 7 11
4 8 12
5 个回答
19
我觉得 numpy.column_stack()
是最容易理解的:
np.savetxt('myfile.txt', np.column_stack([x,y,z]))
50
使用 numpy.transpose()
:
np.savetxt('myfile.txt', np.transpose([x,y,z]))
我觉得这个方法比用 np.c_[]
更容易理解。
70
使用 numpy.c_[]
:
np.savetxt('myfile.txt', np.c_[x,y,z])