假设我有以下两个 numpy 数组。 idxes
包含我想要从 arr
返回的元素的索引。
arr = ['a', 'b', 'c' ]
idxes = [1, 2]
// This is the result I'm after
result = ['b', 'c']
最初的想法是使用 np.where
和 bool 数组,但使用起来似乎很尴尬,并且想知道是否有更优雅的解决方案,因为我对 numpy 还很陌生。
最佳答案
使用这个简单的列表理解,它会迭代 idxes
并获取 arr 中
:idxes
(i
) 中索引的值
print([arr[i] for i in idxes])
输出:
['b', 'c']
如果它们是 numpy 数组:
print(arr[idxes])
输出:
['b' 'c']
关于python - 如何使用包含我要访问的索引号的数组获取 numpy 数组中的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57072716/