python - 像 Matlab 一样在 numpy 中打印子数组

标签 python matlab numpy

如何像 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/

相关文章:

python - 如何使用 Sklearn.preprocessing 对包含列表的 pandas.DataFrame 列进行编码

Python TypeError 必须是 str 而不是 int

string - 这两种 MATLAB 字符串连接方法哪一种更快?

python - Django 全局变量

matlab - 在不可见属性上同步多个轴

matlab - 如何在matlab中向量化双重求和

python-3.x - Python Pandas 字典与 numpy 数组

python - numpy 数组行主要和列主要

python - Numpy 数组错误设置带有序列的数组元素

python - 使用 Python 通过独特的 header 打印技术(.inp 扩展名)从文件中解析 header 字符串