5

I have an array that is 1d, and I would like to print it as a column.

r1 = np.array([54,14,-11,2])
print r1

gives me this:

 [ 54  14 -11   2]

and

 print r1.shape

gives me this:

(4L,)

Is there something I can plug into np.reshape() so that

print r1.shape

gives me this?

(,4L)

And the printed output looks something like

 54
 14
-11
 2
1
  • shape is a tuple. (4,) is standard Python syntax for a 1 element tuple. (,4) is not valid syntax. Neither is an abbreviation for 2d shapes like (1,4) or (4,1). (4,1) is the shape of an array with 4 'rows' and 1 'column', which will display as you want. Commented Feb 11, 2018 at 1:12

2 Answers 2

8

This will work:

import numpy as np

r1 = np.array([54,14,-11,2])

r1[:, None]

# array([[ 54],
#        [ 14],
#        [-11],
#        [  2]])
Sign up to request clarification or add additional context in comments.

Comments

2

No, you can't do that unless you create a vertical version of your array. But if you just want to print your items in that format you can use set_printoptions() function to set a printing format for your intended types:

In [43]: np.set_printoptions(formatter={'int':lambda x: '{}\n'.format(x)})

In [44]: print(r1)
[54
 14
 -11
 2
]

NOTE: If you want to apply this function to all types you can use 'all' keyword that applies the function on all the types.

formatter = {'all':lambda x: '{}\n'.format(x)}

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.