python - 高效插入数字 - NumPy/Python

标签 python numpy

返回一个新向量,其中每个元素由 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/

相关文章:

python - 执行操作时恢复列

python - 在 SqlAlchemy SQLite 中按联合查询中的列排序

python - 在 python 中将 numpy、list 或 float 转换为字符串

python - 抑制可忽略不计的复杂 numpy 特征值?

python - setup.py 中 extras_require 的依赖链接

python - Numpy.array 索引问题

python - 从二维数组中删除数组

python - VS Code 未发现 Python 单元测试测试

python - gcloud ml-engine IOError : file does not exist

python - 计算图像中的单元格数