堆叠数组并将其放入dat文件中

时间:2013-06-09 21:02:52

标签: python-2.7 numpy

到目前为止,在我的代码中:

a = np.array((1,2,3,4))
b = np.array((11,21,31,41))
np.column_stack((a,b))

array([[1, 11],

      [2, 21], 

      [3, 31],

      [4. 41],

我想知道如何设置代码以便将列写入dat文件?

1 个答案:

答案 0 :(得分:2)

看看np.savetxt

http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html

In [2]: a = np.array((1,2,3,4))

In [3]: b = np.array((11,21,31,41))

In [5]: c = np.column_stack((a,b))

In [7]: np.savetxt('test.dat', c)

In [8]: !cat test.dat
1.000000000000000000e+00 1.100000000000000000e+01
2.000000000000000000e+00 2.100000000000000000e+01
3.000000000000000000e+00 3.100000000000000000e+01
4.000000000000000000e+00 4.100000000000000000e+01

您始终可以使用fmt中的np.savetxt选项指定格式:

In [9]: np.savetxt('test.dat', c, fmt='%d')

In [10]: !cat test.dat
1 11
2 21
3 31
4 41