[英]The 2D Array Index access, with step value
I have an array like:我有一个像这样的数组:
a =np.array([[1,2,3,5,8,7,2,1,3,5,1,2,20,21],[3,8,3,9,8,7,10,1,3,5,1,2,20,21]])
requirement:要求:
need to access index value with given step.
需要使用给定的步骤访问索引值。 For an example step value 3. Only index value not an elements value.
例如,步长值 3。只有索引值而不是元素值。
Required Output:所需 Output:
[[0,3,6,9,12],[0,3,6,9,12]]
Use a list comprehension to repeat the range for each row.使用列表推导来重复每一行的范围。
a =np.array([[1,2,3,5,8,7,2,1,3,5,1,2,20,21],[3,8,3,9,8,7,10,1,3,5,1,2,20,21]])
indexes = [list(range(0, a.shape[1], 3)) for _ in range(a.shape[0])]
You can do:你可以做:
a = np.array([[1,2,3,5,8,7,2,1,3,5,1,2,20,21],[3,8,3,9,8,7,10,1,3,5,1,2,20,21]])
step = 3
indices = [np.arange(0, arr.size, step) for arr in a]
print(indices):打印(索引):
[array([ 0, 3, 6, 9, 12]), array([ 0, 3, 6, 9, 12])]
If you want lists instead of np arrays, just use .tolist()
like:如果您想要列表而不是 np arrays,只需使用
.tolist()
如下:
[np.arange(0, arr.size, step).tolist() for arr in a]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.