Prohibit automatic linebreaks in Pycharm Output when using large Matrices

前端 未结 1 1324
失恋的感觉
失恋的感觉 2021-01-22 04:21

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

相关标签:
1条回答
  • 2021-01-22 05:08

    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))
    
    0 讨论(0)
提交回复
热议问题