我想在循环中减去3D数组

时间:2019-01-10 05:37:44

标签: python numpy

我想用当前值减去上一个值,但是我卡在3d数组中?

print(rain.shape) #(73,80,97)
for j in range(1,72):
      diffrain=np.array(rain[j+1,:,:]-rain[j,:,:])

print(diffrain.shape) #(80,97) only 2d array what about j, here j.shape=73 after subtrcation the value of j will be 72.

我想要3d(72,80,97)中的diffrain.shape但显示2d数组。

1 个答案:

答案 0 :(得分:0)

您为每个diffrain重新分配了j。而是

diffrain = []
for j in range(1,72):
      diffrain.append(rain[j+1,:,:]-rain[j,:,:])

diffrain = np.stack(diffrain)
print(diffrain.shape) # now (72,80,97)