使用大型矩阵时,禁止在Pycharm输出中自动换行 [英] Prohibit automatic linebreaks in Pycharm Output when using large Matrices

查看:649
本文介绍了使用大型矩阵时,禁止在Pycharm输出中自动换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows上的PyCharm中工作.在当前正在处理的项目中,我有大"矩阵,但是当我输出它们时,Pycharm自动添加换行符,以便一行占据两行而不是仅占一行:

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?

提前谢谢!

推荐答案

PyCharm默认控制台宽度设置为80个字符. 除非您在选项中设置了soft wrap,否则打印行时不会换行: File -> Settings -> Editor -> General -> Console -> Use soft wraps in console.

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.

使用以下测试代码:

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

您可以尝试以下方法之一:

You can try one of these:

使用pprint模块,并覆盖行 width :

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

脾气暴躁

对于 numpy 版本 1.13 及更低版本:

Numpy

For numpy version 1.13 and lower:

如果使用numpy模块,请配置 arrayprint 选项:

If you use numpy module, configure arrayprint option:

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模块,请配置 display.width 选项:

Pandas

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

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

这篇关于使用大型矩阵时,禁止在Pycharm输出中自动换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆