2013-03-04 44 views
27

我正在嘗試做一些很可能非常簡單的事情。我想三個陣列將文件保存爲使用「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 

回答

26

使用numpy.transpose()

np.savetxt('myfile.txt', np.transpose([x,y,z])) 

我覺得這是更直觀比使用np.c_[]

5

我發現numpy.column_stack()最直觀:

np.savetxt('myfile.txt', np.column_stack([x,y,z]))