我希望标识numpy数组中的索引,这些索引的值是一个集合中包含的值之一;例如,这个集合可以是(5,6,7,8)。
现在我正在做的是
np.where(np.isin(arr, [5,6,7,8]))
它工作得很好。我想知道是否有更好的方法来实现这一功能。
发布于 2018-06-09 09:05:22
你的方法是有效的,它也适用于多维数组。
x = np.arange(9.).reshape(3, 3)
>>> goodvalues = [3, 4, 7]
>>> ix = np.isin(x, goodvalues)
>>> ix
array([[False, False, False],
[ True, True, False],
[False, True, False]], dtype=bool)
>>> np.where(ix)
(array([1, 1, 2]), array([0, 1, 1]))
这直接来自于这里的文档:https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.where.html#numpy.where
发布于 2018-06-09 08:37:47
您拥有的代码是正确和合理的。你应该留着它。
https://stackoverflow.com/questions/50769864
复制相似问题