Numpy打印一个1d数组作为列

时间:2018-02-10 19:51:47

标签: python numpy printing reshape

我有一个1d的数组,我想把它打印成一列。

r1 = np.array([54,14,-11,2])
print r1

给了我这个:

 [ 54  14 -11   2]

 print r1.shape

给了我这个:

(4L,)

我可以插入np.reshape()以便

print r1.shape

给了我这个?

(,4L)

打印输出看起来像

 54
 14
-11
 2

2 个答案:

答案 0 :(得分:2)

这将有效:

import numpy as np

r1 = np.array([54,14,-11,2])

r1[:, None]

# array([[ 54],
#        [ 14],
#        [-11],
#        [  2]])

答案 1 :(得分:1)

不,除非你create a vertical version of your array,否则你不能这样做。但是,如果您只想以该格式打印项目,可以使用set_printoptions()功能为您的预期类型设置打印格式:

In [43]: np.set_printoptions(formatter={'int':lambda x: '{}\n'.format(x)})

In [44]: print(r1)
[54
 14
 -11
 2
]

注意:如果要将此功能应用于所有类型,可以使用'all'关键字在所有类型上应用该功能。

formatter = {'all':lambda x: '{}\n'.format(x)}