【发布时间】:2019-06-13 13:52:53
【问题描述】:
我想通过多个布尔数组索引一个带有布尔掩码的数组,而无需循环。
这是我想要实现的,但没有循环,只有 numpy。
import numpy as np
a = np.array([[0, 1],[2, 3]])
b = np.array([[[1, 0], [1, 0]], [[0, 0], [1, 1]]], dtype=bool)
r = []
for x in b:
print(a[x])
r.extend(a[x])
# => array([0, 2])
# => array([2, 3])
print(r)
# => [0, 2, 2, 3]
# what I would like to do is something like this
r = some_fancy_indexing_magic_with_b_and_a
print(r)
# => [0, 2, 2, 3]
【问题讨论】:
标签: python arrays numpy matrix-indexing