返回一个新向量,其中每个元素由 4 个连续的零分隔。试图实现 [4,2,1] --> [4,0,0,0,0,2,0,0,0,0,1]
def zero_insert(x):
y = np.zeros((5*(len(x)-1))+1, dtype=np.int)
for i in range(len(x)):
y[5*i] = x[i]
return y
最佳答案
初始化并赋值-
x = np.asarray(x) # convert to array
n = 4 # number of zeros to be inserted
N = n+1
out = np.zeros((len(x)-1)*N+1,dtype=x.dtype)
out[::N] = x
关于python - 高效插入数字 - NumPy/Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46635231/