Paul Parneix
Paul Parneix

Reputation: 149

Python : simple array manipulation with Numpy

Sorry for this simple question, but I can't find how to figure it out :

I have a long 1D numpy array like:

[1,2,3,4,5,6,7,8,9,10,11,12, ... ,n1,n2,n3]

this array is used to store x y z position of points, like [x0,y0,z0,x1,y1,z1 etc.... ]

I would like to convert it to this form :

[ [1,2,3],[4,5,6],[7,8,9],[10,11,12],....,[n1,n2,n3] ]

It it possible with numpy without going through slow for loops ?

Thanks :)

Upvotes: 2

Views: 74

Answers (1)

Alex
Alex

Reputation: 19104

Use the reshape method.

a = np.arange(27)  # some 1-D numpy array
a.reshape(-1, 3)

Upvotes: 3

Related Questions