使用大型矩阵时禁止在 Pycharm 输出中自动换行
Prohibit automatic linebreaks in Pycharm Output when using large Matrices
我在 Windows PyCharm 工作。在我目前正在处理的项目中,我有 "large" 矩阵,但是当我输出它们时 Pycharm 会自动添加换行符,以便一行占用两行而不是一行:
[[ 3. -1.73205081 0. 0. 0. 0. 0.
0. 0. 0. ]
[-1.73205081 1. -1. -2. 0. 0. 0.
0. 0. 0. ]
[ 0. -1. 1. 0. -1.41421356 0. 0.
0. 0. 0. ]
[ 0. -2. 0. 1. -1.41421356 0.
-1.73205081 0. 0. 0. ]
[ 0. 0. -1.41421356 -1.41421356 0. -1.41421356
0. -1.41421356 0. 0. ]
[ 0. 0. 0. 0. -1.41421356 0. 0.
0. -1. 0. ]
[ 0. 0. 0. -1.73205081 0. 0. 3.
-1.73205081 0. 0. ]
[ 0. 0. 0. 0. -1.41421356 0.
-1.73205081 1. -2. 0. ]
[ 0. 0. 0. 0. 0. -1. 0.
-2. 0. -1.73205081]
[ 0. 0. 0. 0. 0. 0. 0.
0. -1.73205081 0. ]]
这让我的结果很难被读取和比较。 window 足够大,可以显示所有内容,但它仍然会打断行。有什么设置可以防止这种情况吗?
提前致谢!
PyCharm 默认控制台宽度设置为 80 个字符。
除非您在选项中设置 soft wrap
,否则打印的行不会换行:
File -> Settings -> Editor -> General -> Console -> Use soft wraps in console
.
然而,这两种选择都会使读取大矩阵变得困难。
您可以通过几种方式解决此问题。
用这个测试代码:
import random
m = [[random.random() for a in range(10)] for b in range(10)]
print(m)
您可以尝试其中之一:
印刷精美
使用 pprint
模块,并覆盖行 width:
import pprint
pprint.pprint(m, width=300)
Numpy
对于 numpy 版本 1.13 及更低版本:
如果使用numpy
模块,配置arrayprint选项:
import numpy
numpy.core.arrayprint._line_width = 300
print(numpy.matrix(m))
对于 numpy 版本 1.14 及更高版本 (感谢@Alex Johnson):
import numpy
numpy.set_printoptions(linewidth=300)
print(numpy.matrix(m))
Pandas
如果你使用pandas
模块,配置display.width选项:
import pandas
pandas.set_option('display.width', 300)
print(pandas.DataFrame(m))
我在 Windows PyCharm 工作。在我目前正在处理的项目中,我有 "large" 矩阵,但是当我输出它们时 Pycharm 会自动添加换行符,以便一行占用两行而不是一行:
[[ 3. -1.73205081 0. 0. 0. 0. 0.
0. 0. 0. ]
[-1.73205081 1. -1. -2. 0. 0. 0.
0. 0. 0. ]
[ 0. -1. 1. 0. -1.41421356 0. 0.
0. 0. 0. ]
[ 0. -2. 0. 1. -1.41421356 0.
-1.73205081 0. 0. 0. ]
[ 0. 0. -1.41421356 -1.41421356 0. -1.41421356
0. -1.41421356 0. 0. ]
[ 0. 0. 0. 0. -1.41421356 0. 0.
0. -1. 0. ]
[ 0. 0. 0. -1.73205081 0. 0. 3.
-1.73205081 0. 0. ]
[ 0. 0. 0. 0. -1.41421356 0.
-1.73205081 1. -2. 0. ]
[ 0. 0. 0. 0. 0. -1. 0.
-2. 0. -1.73205081]
[ 0. 0. 0. 0. 0. 0. 0.
0. -1.73205081 0. ]]
这让我的结果很难被读取和比较。 window 足够大,可以显示所有内容,但它仍然会打断行。有什么设置可以防止这种情况吗?
提前致谢!
PyCharm 默认控制台宽度设置为 80 个字符。
除非您在选项中设置 soft wrap
,否则打印的行不会换行:
File -> Settings -> Editor -> General -> Console -> Use soft wraps in console
.
然而,这两种选择都会使读取大矩阵变得困难。 您可以通过几种方式解决此问题。
用这个测试代码:
import random
m = [[random.random() for a in range(10)] for b in range(10)]
print(m)
您可以尝试其中之一:
印刷精美
使用 pprint
模块,并覆盖行 width:
import pprint
pprint.pprint(m, width=300)
Numpy
对于 numpy 版本 1.13 及更低版本:
如果使用numpy
模块,配置arrayprint选项:
import numpy
numpy.core.arrayprint._line_width = 300
print(numpy.matrix(m))
对于 numpy 版本 1.14 及更高版本 (感谢@Alex Johnson):
import numpy
numpy.set_printoptions(linewidth=300)
print(numpy.matrix(m))
Pandas
如果你使用pandas
模块,配置display.width选项:
import pandas
pandas.set_option('display.width', 300)
print(pandas.DataFrame(m))