Help with pprint

Terry Reedy tjreedy at udel.edu
Wed Nov 25 15:22:08 EST 2009


Nadav Chernin wrote:
> Hello, I want to print list of lists in matrix format. So I use pprint 
> with parameter ‘width’ for this target.
> 
> For example :
>  >>> data=[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
>  >>> pprint(data,width=20)
> [[1, 1, 1], 
>  [1, 1, 1],
>  [1, 1, 1]]

> 
> The problem that I don’t know how to select width value, because if:
>   >>>data=[['one', 'one', 'one'], ['one', 'one', 'one'], ['one', 'one', 
> 'one']]  
>  >>> pprint(data,width=20)
> [['one',
>   'one',
  [snip]

1. Cpp.pprint(data, width=100)alculate a width that is big enough but 
not too big. In your example, the interval is [24,69]
2. Write a loop, which gives you finer control.

 >>> for line in data: print(line)
['one', 'one', 'one']
['one', 'one', 'one']
['one', 'one', 'one']

To me, this is more like a matrix and possibly better, depending on your 
particular application. If the items have different default printed 
widths and you want column lined up, you will need to custom format each 
  field in a column to a uniform width anyway. Print and pprint are for 
quick, take-what-you-get output, not for customj-designed production output.

Terry Jan Reedy




More information about the Python-list mailing list