print formating for matrix/table
Robert Kern
robert.kern at gmail.com
Mon Jan 26 20:58:29 EST 2009
On 2009-01-26 19:55, Steve Holden wrote:
> 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]]
But this doesn't line up the columns, except by accident:
[[1, 10, 200],
[300, 4000, 50000],
[1, 10, 200],
[300, 4000, 50000],
[1, 10, 200],
[300, 4000, 50000],
[1, 10, 200],
[300, 4000, 50000],
[1, 10, 200],
[300, 4000, 50000],
[1, 10, 200],
[300, 4000, 50000],
[1, 10, 200],
[300, 4000, 50000],
[1, 10, 200],
[300, 4000, 50000],
[1, 10, 200],
[300, 4000, 50000],
[1, 10, 200],
[300, 4000, 50000]]
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list