python - 独立移动数组中的行

标签 python arrays numpy

我想按行号移动每一行,并与我想要的输出形状相关。一个例子:

array([[0, 1, 2],        array([[0, 1, 2],                array([[0, 1, 2, 0, 0],
       [1, 2, 3],    ->            [1, 2, 3],      ->            [0, 1, 2, 3, 0],
       [2, 3, 4]])                    [2, 3, 4]])                [0, 0, 2, 3, 4])
最左边的数组是我的输入,最右边的数组是我想要的输出。这可以推广到更大的数组,例如 10x10大批。
有没有很好的方法来做到这一点?

我所拥有的是:
A = np.array([[1, 2, 3],
              [2, 3, 4],
              [3, 4, 5]], dtype=np.float32)

out = np.zeros((A.shape[0], A.shape[1]*2-1))

out[[np.r_[:3]], [np.r_[:3] + np.r_[:3][:,None]]] = A

最佳答案

这是大小为 n 的方阵的解决方案。np.concatenate((A,np.zeros((n,n))),axis=1).flatten()[0:-n].reshape([n,2*n-1])

关于python - 独立移动数组中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67334116/

相关文章:

python - 为什么 `letter=="A"或 "a"` 总是评估为 True?

python - 如何在 Pandas DataFrame 中有效添加两列?

arrays - 如何快速创建对象数组?

python - 在安装 Python 3.4 的同时为 Python 2.7 安装 numpy?

python - 具有 3 个参数的 Numpy ndarray 形状

python - xlsxwriter 可以创建 xls 文件吗?

python - 禁用 django 消息框架后出现问题

python - PyLab 中的插值

java - 在 Java 中使用数组的简单 HashTable 实现?

python - 如果存在于另一个数组中,则从一个数组中删除元素,保留重复元素 - NumPy/Python