我想通过执行以下操作来编写更具可读性的代码:
import numpy as np
SLICE_XY = slice(0, 2)
SLICE_Z = slice(2, 3)
data = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11]])
xy = data[:, SLICE_XY]
z = data[:, SLICE_Z]
但是,我遇到了执行上述操作的问题
>>> xy
array([[ 0, 1],
[ 3, 4],
[ 6, 7],
[ 9, 10]])
>>> z
array([[ 2],
[ 5],
[ 8],
[11]])
这是我所期望的xy
。但是因为z
我希望它相当于
>>> data[:, 2]
array([ 2, 5, 8, 11])
笔记:
>>> data[:, 0:2]
array([[ 0, 1],
[ 3, 4],
[ 6, 7],
[ 9, 10]])