像Matlab那样在numpy中打印子数组

时间:2013-09-09 14:30:34

标签: python matlab numpy

如何以与Matlab相同的方式打印numpy中的子阵列?我有一个3乘10000的数组,我想查看前20列。在Matlab中你可以写

a=zeros(3,10000);
a(:,1:20)
  Columns 1 through 15

 0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 0     0     0     0     0     0     0     0     0     0     0     0     0     0     0

  Columns 16 through 20

 0     0     0     0     0
 0     0     0     0     0
 0     0     0     0     0

然而在Numpy

import numpy as np
set_printoptions(threshold=nan)
a=np.zeros((3,10000))
print a[:,0:20]
[[  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.]]

你可以看到numpy打印第一行,然后是第二行,然后是第三行。我希望它保持列结构而不是行结构

非常感谢

PS:例如,一种解决方案是

print a[:,0:20].T
[[  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]]

但是,屏幕上的空间会比预期多得多。如果numpy有这个选项会很棒

1 个答案:

答案 0 :(得分:1)

这会给你想要的吗?

>>> for item in a[:,0:20].T:
    print '\t'.join(map(str,item.tolist()))

还是这个?

>>> for item in a[:,0:20]:
    print '\t'.join(map(str,item.tolist()))