如何使用存储在另一个numpy数组中的一组索引访问numpy数组? [英] How to access numpy array with a set of indices stored in another numpy array?
本文介绍了如何使用存储在另一个numpy数组中的一组索引访问numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个numpy数组,该数组存储一组访问另一个numpy数组所需的索引.
I have a numpy array which stores a set of indices I need to access another numpy array.
我尝试使用for
循环,但是它没有按预期工作.
I tried to use a for
loop but it doesn't work as I expected.
情况是这样的:
>>> a
array([[1, 2],
[3, 4]])
>>> c
array([[0, 0],
[0, 1]])
>>> a[c[0]]
array([[1, 2],
[1, 2]])
>>> a[0,0] # the result I want
1
上面是我的实际代码的简化版本,其中c
数组要大得多,因此我必须使用for
循环来获取每个索引.
Above is a simplified version of my actual code, where the c
array is much larger so I have to use a for
loop to get every index.
推荐答案
将其转换为tuple
:
>>> a[tuple(c[0])]
1
因为list
和array
索引会触发高级索引. tuple
是(主要是)基本切片.
Because list
and array
indices trigger advanced indexing. tuple
s are (mostly) basic slicing.
这篇关于如何使用存储在另一个numpy数组中的一组索引访问numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文