小编Raj*_*jan的帖子

python中的二维数组操作

我有一个二维数组,我希望i每一行中的数组元素i-1被同一行中的元素减少

我试过这个代码:

data = np.arange(12).reshape(3,4)
print(data)
for row in data:
    for cell in row:
        data[cell] = data[cell]-data[cell-1]

print(data)
Run Code Online (Sandbox Code Playgroud)

我得到了这样的输出

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
Traceback (most recent call last):
  File "D:/testing/test.py", line 55, in <module>
    data[cell] = data[cell]-data[cell-1]

IndexError: index -8 is out of bounds for axis 0 with size 3
Run Code Online (Sandbox Code Playgroud)

我想要这样的输出

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy

1
推荐指数
1
解决办法
57
查看次数

标签 统计

arrays ×1

numpy ×1

python ×1