如何像 Matlab 一样在 numpy 中打印子数组?我有一个 3 x 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 有这个选项就好了
最佳答案
这能满足您的要求吗?
>>> 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()))
关于python - 像 Matlab 一样在 numpy 中打印子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18700620/