Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy: print array with indentation

I would like to print numpy array with indentation for debugging.

Say I have an array a = numpy.array([[1,2,3,4], [5,6,7,8]]), then simple print(a) will give

[[ 63 903 942 952]
 [185 332 511 893]]

Now if I put in \t in print("\t" + str(a)), then I get

    [[ 63 903 942 952]
 [185 332 511 893]]

while I want to have

    [[ 63 903 942 952]
     [185 332 511 893]]
like image 660
Lelouch Avatar asked Mar 17 '14 23:03

Lelouch


People also ask

How do I print a NumPy array without brackets?

To print a NumPy array without enclosing square brackets, the most Pythonic way is to unpack all array values into the print() function and use the sep=', ' argument to separate the array elements with a comma and a space.

What is set_printoptions NumPy?

In Python, we use the set_printoptions() function to set the way floating-point number(s), array(s), and other NumPy objects are displayed.


1 Answers

This should do it :

print('\t' + str(a).replace('\n', '\n\t'))
like image 55
Unda Avatar answered Sep 21 '22 00:09

Unda