numpy数组:删除并附加值

时间:2020-08-23 10:03:04

标签: python numpy numpy-ndarray

我有一个形状为(1, 60, 1)的3D numpy数组。现在,我需要删除第二维的第一个值,而在末尾附加一个新值。

如果它是一个列表,则代码将看起来像这样:

x = [1, 2, 3, 4]
x = x[1:]
x.append(5)

此列表中的结果:[2, 3, 4, 5]

用numpy做到这一点最简单的方法是什么?

我以前基本上从未真正使用过numpy,所以这可能是一个非常琐碎的问题,但是感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

import numpy as np

arr = np.arange(60)   #creating a nd array with 60 values  
arr = arr.reshape(1,60,1)   # shaping it as mentiond in question
arr = np.roll(arr, -1)   # use np.roll to circulate the array left or right (-1 is 1 step to the left)
#Now your last value is in the second last position, the second last value in the third last pos and so on (Your first value moves to the last position)  
arr[:,-1,:] = 1000  # index the last location and add the values you want  
print(arr)
相关问题