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

查看:29
本文介绍了使用大型矩阵时禁止 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,否则将打印行而不换行:文件->设置->编辑器->一般->控制台->在控制台中使用软包装.

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):

For numpy version 1.14 and above (thanks to @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天全站免登陆