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)?
JavaScript
x
11
11
1
# What I have.
2
array = np.arange(20)[np.newaxis]
3
print(array.shape, array)
4
5
# Remove 0 from the beginning and add 20 to the end.
6
array = np.append(array[0, 1:], np.array([[20]]))
7
print(array)
8
array = array[np.newaxis]
9
print(array.shape, array)
10
11
Output:
JavaScript
1
4
1
(1, 20) [[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]]
2
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
3
(1, 20) [[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]]
4
Advertisement
Answer
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.
JavaScript
1
2
1
x = np.append(array[:,1:],[[20]], axis=1)
2