matrix = [ [1,2,3], [4,5,6], [7,8,9] ]
import pprint
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(matrix)
这个不是漂亮的打印,而是丑陋的打印。
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
什么情况?
matrix = [ [1,2,3], [4,5,6], [7,8,9] ]
import pprint
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(matrix)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
原因是当一行上打印的数组长度小于默认的80个字符的width
时,会出现这种情况。您可能需要查看pprint
的文档。
例如,如果我们执行以下操作:
matrix = [ [1,2,3], [4,5,6], [7,8,9] ]
import pprint
pp = pprint.PrettyPrinter(indent=4,width=20)
pp.pprint(matrix)
[ [1, 2, 3],
[4, 5, 6],
[7, 8, 9]]