漂亮的打印不工作?

tem*_*ame 2 python pretty-print python-3.x

matrix = [ [1,2,3], [4,5,6], [7,8,9] ]
import pprint
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(matrix)
Run Code Online (Sandbox Code Playgroud)

这不是很好的印刷品.它打印丑陋.

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Run Code Online (Sandbox Code Playgroud)

是什么赋予了??

小智 5

这是因为在一行上打印数组的长度小于默认值width80.您可能需要查看文档pprint.

例如,如果我们这样做:

matrix = [ [1,2,3], [4,5,6], [7,8,9] ]
import pprint
pp = pprint.PrettyPrinter(indent=4,width=20)
pp.pprint(matrix)
Run Code Online (Sandbox Code Playgroud)

我们得到:

[   [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]]
Run Code Online (Sandbox Code Playgroud)

  • @Aerovistae 并不是暗示您不只是向其他可能稍后阅读该问题的人指出这一点。有时,以另一种眼光看待这些事情会很有帮助。发生在我们所有人身上! (2认同)