2

Well, this is a very quick question. I want to slice an array (numpy array), and save it in another array (define it in another numpy array). I used the following codes which is wrong. I'd be happy if you help me correct my codes.

below, aa is a new numpy one-dimensional array that I wanna define. cc is a numpy 2-dimensional array.

aa = cc[1:,3]

Thanks in advance for your supports.

2
  • Can't you just use something like: aa = numpy.array(cc[1:,3])? Commented Oct 23, 2018 at 10:22
  • I did try it, but it seems it's not working as well. Commented Oct 23, 2018 at 10:44

1 Answer 1

1

If you use aa like that, you will create a new variable.

You need to do:

aa[:] = cc[1:,3]

This way you tell to replace the content of aa and not the variable itself.

Sign up to request clarification or add additional context in comments.

3 Comments

This answer definitely helped me understand how numpy arrays are assigned. However, here, I need to define aa before running this code. Actually don't know the dimension of aa and I'm running this code to save this slice of cc in order to run len(aa) to find its length.
I tried doing len(cc[1:, 3]), it didn't work as well. How do you think I can find its length. Thank you again for your support.
you can use cc.shapeto get the shape: aa = np.zeros((cc.shape[0]-1,))

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.