numpy垂直数组写入文本文件

时间:2015-07-14 03:50:01

标签: python arrays numpy vertical-text

我有两个5乘1的垂直阵列

x = [[1]
     [2]
     [3]
     [4]
     [5]]
y = [[92]
     [93]
     [94]
     [95]
     [96]]

我需要在文本文件中输出(belo)

1 92
2 93
3 94
4 95
5 96

我的脚本看起来像这样

x= numpy.vstack((z))
y= numpy.vstack((r))
numpy.savetxt('fin_lower.dat', ??, fmt='%.4e')

感谢任何帮助

1 个答案:

答案 0 :(得分:2)

制作2个数组:

In [117]: x=np.arange(1,6).reshape(-1,1) 
In [119]: y=np.arange(92,97).reshape(-1,1)

因为它们是2d,concatenate效果很好; hstackcolumn_stack

In [122]: xy=np.concatenate((x,y),axis=1)

In [123]: xy
Out[123]: 
array([[ 1, 92],
       [ 2, 93],
       [ 3, 94],
       [ 4, 95],
       [ 5, 96]])

In [124]: xy=np.hstack((x,y))

现在我有一个2d数组(5行,2列),可以以所需的格式保存:

In [126]: np.savetxt('test.txt',xy)
In [127]: cat test.txt
1.000000000000000000e+00 9.200000000000000000e+01
2.000000000000000000e+00 9.300000000000000000e+01
3.000000000000000000e+00 9.400000000000000000e+01
4.000000000000000000e+00 9.500000000000000000e+01
5.000000000000000000e+00 9.600000000000000000e+01

In [128]: np.savetxt('test.txt',xy, fmt='%.4e')
In [129]: cat test.txt
1.0000e+00 9.2000e+01
2.0000e+00 9.3000e+01
3.0000e+00 9.4000e+01
4.0000e+00 9.5000e+01
5.0000e+00 9.6000e+01

In [131]: np.savetxt('test.txt',xy, fmt='%d')    
In [132]: cat test.txt
1 92
2 93
3 94
4 95
5 96