小编JY *_*Won的帖子

我可以用掩码分割 numpy 数组吗?

我想将数组拆分为带有掩码和索引的数组,
如下所示

a = array([ 0,  1,  2,  3,  4, 5]))  
b = [0,2,3]  
Run Code Online (Sandbox Code Playgroud)

进入

c = array([[0, 2, 3], [1, 3, 4], [2, 4, 5]])  
Run Code Online (Sandbox Code Playgroud)

我可以在没有循环的情况下执行此操作吗?

编辑:

更多例子...

说,我们有一个a形状的数组,[10, 10, 10]
其中a[x, y, :] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

现在给了面具 b = [0, 3, 7]

我希望输出是一个c具有形状的数组,[10, 10, 3, 3]
其中c[x, y, :, :] = [[0, 3, 7], [1, 4, 8], [2, 5, 9]]

python numpy python-3.x numpy-slicing numpy-ndarray

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

标签 统计

numpy ×1

numpy-ndarray ×1

numpy-slicing ×1

python ×1

python-3.x ×1