print formating for matrix/table

Steve Holden steve at holdenweb.com
Mon Jan 26 20:55:47 EST 2009


Vincent Davis wrote:
> I have a list of lists....a matrix in that all sub lists are the
> same length. I there a nice why to prin these so that the columns and
> rows line up nicely?
> I have looked around for a good way to do this and haven't found one I
> am like. It seems that all involve repeating a print for each line. I
> would have thought I could find a prebuilt function to do this. Surly
> lots of people are printing matrixes and would like nice formating. So
> what am I missing?
> 
Look at the pprint module:

>>> arr = [range(10)] * 10
>>> from pprint import pprint
>>> pprint(arr)
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
>>>

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list