Del*_*ief 2 python matrix line-breaks pycharm output
我正在 Windows 上的 PyCharm 中工作。在我目前正在处理的项目中,我有“大”矩阵,但是当我输出它们时,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. ]]
Run Code Online (Sandbox Code Playgroud)
这使我的结果很难被查阅和比较。窗口足够大,所以应该显示所有内容,但它仍然打破行。是否有任何设置可以防止这种情况?
提前致谢!
PyCharm 默认控制台宽度设置为 80 个字符。除非您soft wrap在 options: 中设置,
否则打印行时不会换行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)
Run Code Online (Sandbox Code Playgroud)
您可以尝试以下方法之一:
使用pprint模块,并覆盖线宽:
import pprint
pprint.pprint(m, width=300)
Run Code Online (Sandbox Code Playgroud)
对于numpy 1.13及更低版本:
如果使用numpy模块,请配置arrayprint选项:
import numpy
numpy.core.arrayprint._line_width = 300
print(numpy.matrix(m))
Run Code Online (Sandbox Code Playgroud)
对于numpy 1.14及更高版本(感谢@Alex Johnson):
import numpy
numpy.set_printoptions(linewidth=300)
print(numpy.matrix(m))
Run Code Online (Sandbox Code Playgroud)
如果使用pandas模块,请配置display.width选项:
import pandas
pandas.set_option('display.width', 300)
print(pandas.DataFrame(m))
Run Code Online (Sandbox Code Playgroud)