Prohibit automatic linebreaks in Pycharm Output when using large Matrices

最后都变了- 提交于 2020-01-11 11:17:26

问题


I'm working in PyCharm on Windows. In the project I'm currently working on I have "large" matrices, but when i output them Pycharm automatically adds linebreaks so that one row occupys two lines instead of just one:

 [[ 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.        ]]

It make my results very hard to reed and to compare. The window is big enough so that everything should be displayed but it still breaks the rows. Is there any setting to prevent this?

Thanks in advance!


回答1:


PyCharm default console width is set to 80 characters. Lines are printed without wrapping unless you set soft wrap in options: File -> Settings -> Editor -> General -> Console -> Use soft wraps in console.

However both options make reading big matrices hard. You can fix this in few ways.

With this test code:

import random
m = [[random.random() for a in range(10)] for b in range(10)]
print(m)

You can try one of these:

Pretty print

Use pprint module, and override line width:

import pprint
pprint.pprint(m, width=300)

Numpy

For numpy version 1.13 and lower:

If you use numpy module, configure arrayprint option:

import numpy
numpy.core.arrayprint._line_width = 300
print(numpy.matrix(m))

For numpy version 1.14 and above (thanks to @Alex Johnson):

import numpy
numpy.set_printoptions(linewidth=300)
print(numpy.matrix(m))

Pandas

If you use pandas module, configure display.width option:

import pandas
pandas.set_option('display.width', 300)
print(pandas.DataFrame(m))


来源:https://stackoverflow.com/questions/43952715/prohibit-automatic-linebreaks-in-pycharm-output-when-using-large-matrices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!