4

I have a numpy array, say

 ([1,2,3,4,5,6,7])

I want to split it into a 2d array such that the last element is in its own array, like this

 ([1,2,3,4,5,6],[7])

How exactly would I do this?

1
  • BTW that's not a 2D array, just two separate 1D arrays or lists. Commented Feb 7, 2017 at 20:27

1 Answer 1

5

Use np.split -

np.split(a,[-1])

Sample run -

In [105]: a
Out[105]: array([1, 2, 3, 4, 5, 6, 7])

In [106]: np.split(a,[-1])
Out[106]: [array([1, 2, 3, 4, 5, 6]), array([7])]
Sign up to request clarification or add additional context in comments.

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.