I am writing some code that uses matrices, and in some processes I want to display the matrix to console. I am storing the matrices as multidimensional lists. An assignment may look like:
mat = [[1242,2.01],[10,42.1]]
My aim is to have a function that returns a string of the array that would look like:
1242 2.01
10 42.1
The issue is that I do not know how large the matrix will be, i.e., it will not always be 2x2
, it may be a 34x84
. The below code shows how I have approached this, but I feel like there must be a better way to do it.
size = len(mat)
for i in range(size):
print(('{:4d} '*(size)).format(*mat[i]))
Where mat
is the matrix stored as show above.
Is there a better way to do this, or is this the most pythonic way to do it?