2

Is there a more efficient way to remove the 0 from the beginning and insert the 20 at the end and retain the shape (1, 20)?

# What I have.
array = np.arange(20)[np.newaxis]
print(array.shape, array)

# Remove 0 from the beginning and add 20 to the end.
array = np.append(array[0, 1:], np.array([[20]]))
print(array)
array = array[np.newaxis]
print(array.shape, array)

Output:

(1, 20) [[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]]
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]
(1, 20) [[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]]

2 Answers 2

3

You can just select a subset of the current array excluding the first element and then add 20 or whatever scalar you want at the end.

x = np.append(array[:,1:],[[20]], axis=1)
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe like this:

 array= np.linspace(start=1, stop=20, num=20, endpoint=True, dtype=int)[np.newaxis]
 print(array.shape, array)

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.