【发布时间】:2021-06-13 05:39:42
【问题描述】:
我有一个简单的 numpy 数组,我想创建一个单独的数组,每两个索引取两个元素
例如:
x = np.arange(0,20)
print(x)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
我的目标是得到一个数组
[2 3 6 7 10 11 14 15 18 19]
我该怎么做?我试过这个:
print(x[1:len(x)-1:2])
[ 1 3 5 7 9 11 13 15 17]
但我只得到其他所有索引。
【问题讨论】:
-
您可以使用传统的
start:stop:step约定简单地做到这一点,无需任何模数,方法是重塑您的数组、索引,然后将其展平。查看我的解决方案了解详情。 -
将
x重塑为[[0,1],[2,3],[4,5],[6,7],...]。然后选择每隔一个“行”。x.reshape(-1,2)[::2,:]
标签: python arrays numpy jupyter slice