4

I ran the following code in order to obtain a 2-by-3 array:

import numpy as np
a = np.arange(6).resize(2,3)
print(a)

However, the output is

None

Why is this? Many thanks

2
  • 2
    All operations in Python that operate in-place return None. Commented Nov 30, 2016 at 11:34
  • Many thanks! This makes it clear. Commented Nov 30, 2016 at 11:42

1 Answer 1

4

What you want to do is

import numpy as np
a = np.arange(6).reshape((2,3))
print(a)

to use resize :

a = np.arange(6)
a = np.resize(a, (2,3))
Sign up to request clarification or add additional context in comments.

3 Comments

reshape doesn't change the base content. But resize should return a new array which is with the requested dimension. Is this right? I just wondered why my code doesn't return a new array.
I just checked the reference. What ndarray.resize() returns is exactly None. Thank you for your comment.
reshape need that the array have the same number of element. With resize, if the new array is larger than the original array, then the new array is filled with repeated copies of a.

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.