0

I have the following code:

import numpy as np
from numpy import matrix

MM = matrix([[1.00,0,0],[0,1.50,0],[0,0,2.00]])
M = np.array(MM)
print("matrix M: \n", M)

which output looks like this:

matrix M: 
 [[1.  0.  0. ]
 [0.  1.5 0. ]
 [0.  0.  2. ]]

This is very aesthetic, but I want it to look more organized. Something like this hopefully:

[1.  0.   0.]
[0.  1.5  0.]
[0.  0.   2.]
5
  • You can create a custom 'to_string()' method. Regretfully, python print in this fashion the n-dimensional array..... Commented Dec 18, 2019 at 14:55
  • 1
    You can fix the alignment of the first line of M in the output with print("matrix M: \n", M, sep=''). But you'll still have the outer square brackets. Commented Dec 18, 2019 at 14:58
  • @WarrenWeckesser right, but it won't remove the double square brackets like OP apparently wants Commented Dec 18, 2019 at 15:06
  • 1
    After having read the answers I'm now not sure if I should've edited the title. What is your exact question? How to remove the brackets? How to align the values? How to remove the space before the matrix? Commented Dec 18, 2019 at 15:24
  • I'd suggest a test case that involves a wider range of values. For example a '12.3' in one row could mess up the row-by-row formatting. A big plus for the default matrix format is that it keeps columns aligned. Getting rid of the outer set of [] might not be worth the effort. A np.matrix is always 2d, but other numpy arrays may be 1d or 3d. Commented Dec 18, 2019 at 16:49

5 Answers 5

2

If you print M on its own it actually looks as you expect.

The reason why the first line gets messed up is because print inserts a space between its arguments by default. You can suppress that by providing sep=''.

See https://docs.python.org/3/library/functions.html#print for reference.

Or better yet, omit \n from the first print argument and use sep='\n' instead:

>>> print("matrix M:", M, sep='\n')
matrix M: 
[[1.  0.  0. ]
 [0.  1.5 0. ]
 [0.  0.  2. ]]
Sign up to request clarification or add additional context in comments.

2 Comments

Looks like OP does not want the the double square brackets at all
thank you! the double square brackets don't matter, it looks nice anyway.
0

Pretty close:

'\n'.join('[{}]'.format(' '.join(str(n) for n in row)) for row in M)

Outputs

[1.0 0.0 0.0]
[0.0 1.5 0.0]
[0.0 0.0 2.0]

This removes the indentation and the double square brackets.

However, with very large matrices this might be slow due to the nested for loops.

Comments

0

As describes here Pretty print 2D Python list , a very nice option is the following:

s = [[str(e) for e in row] for row in M]
lens = [max(map(len, col)) for col in zip(*s)]
fmt = '\t'.join('{{:{}}}'.format(x) for x in lens)
table = [fmt.format(*row) for row in s]
print('\n'.join(table))

1.0     0.0     0.0
0.0     1.5     0.0
0.0     0.0     2.0

Comments

0

The exact desired output is this:

print('\n'.join('[{}]'.format('. '.join(str(float(n)) for n in row)) for row in M))

[1.0. 0.0. 0.0]
[0.0. 1.5. 0.0]
[0.0. 0.0. 2.0]

Comments

0

Depending on how much extra code/complexity you want for a solution here, you can do a few things. You can use a custom function in the print statement if you really want fine-grained control over the output (using code offered up in some of the other proposed solutions), but a less cumbersome approach that still gives you a decent amount of flexibility is to use the array2string function already included in Numpy.

A few examples using your scenario:

  • Without any arguments:
>>> print(np.array2string(a=M))
[[1.  0.  0. ]
 [0.  1.5 0. ]
 [0.  0.  2. ]]
  • Using the separator argument to add a bit of padding around elements:
>>> print(np.array2string(a=M, separator='   '))
[[1.    0.    0. ]
 [0.    1.5   0. ]
 [0.    0.    2. ]]
  • If the leading and trailing brackets are bothersome, you can go a bit further with the print statement (array2string doesn't give you a way to change this) itself, doing a bit of manipulation to account for the character spacing with the brackets being removed:
>>> print(' ' + np.array2string(a=M, separator='   ')[1:-1])
 [1.    0.    0. ]
 [0.    1.5   0. ]
 [0.    0.    2. ]

Note that the last scenario still leaves a space at the beginning of each line. You could use regexes to clean this up.

I realize that this may not be exactly what you are looking for in terms of output, but I thought I might offer up a lighter weight alternative that gets you mostly there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.