I feel silly, because this is such a simple thing, but I haven't found the answer either here or anywhere else.
我觉得很傻,因为这是一件很简单的事情,但我在这里或其他任何地方都找不到答案。
Is there no straightforward way of indexing a numpy array with another?
有没有直接的方法索引numpy数组与另一个?
Say I have a 2D array
说我有一个2D数组
>> A = np.asarray([[1, 2], [3, 4], [5, 6], [7, 8]])
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
if I want to access element [3,1] I type
如果我想访问元素[3,1]我输入
>> A[3,1]
8
Now, say I store this index in an array
现在,假设我将此索引存储在数组中
>> ind = np.array([3,1])
and try using the index this time:
并尝试使用索引这次:
>> A[ind]
array([[7, 8],
[3, 4]])
the result is not A[3,1]
结果不是A [3,1]
The question is: having arrays A and ind, what is the simplest way to obtain A[3,1]?
问题是:拥有数组A和ind,获得A [3,1]的最简单方法是什么?
2 个解决方案
#1
4
Just use a tuple:
只需使用元组:
>>> A[(3, 1)]
8
>>> A[tuple(ind)]
8
The A[]
actually calls the special method __getitem__
:
A []实际上调用了特殊方法__getitem__:
>>> A.__getitem__((3, 1))
8
and using a comma creates a tuple:
并使用逗号创建一个元组:
>>> 3, 1
(3, 1)
Putting these two basic Python principles together solves your problem.
将这两个基本的Python原则放在一起可以解决您的问题。
You can store your index in a tuple in the first place, if you don't need NumPy array features for it.
如果您不需要NumPy数组功能,则可以首先将索引存储在元组中。
#2
4
That is because by giving an array you actually ask
那是因为通过给出一个实际问的数组
A[[3,1]]
Which gives the third and first index of the 2d array instead of the first index of the third index of the array as you want.
它给出了2d数组的第三个和第一个索引,而不是你想要的数组的第三个索引的第一个索引。
You can use
您可以使用
A[ind[0],ind[1]]
You can also use (if you want more indexes at the same time);
您也可以使用(如果您想同时获得更多索引);
A[indx,indy]
Where indx
and indy
are numpy arrays of indexes for the first and second dimension accordingly.
其中indx和indy是第一维和第二维的numpy索引数组。
See here for all possible indexing methods for numpy arrays: http://docs.scipy.org/doc/numpy-1.10.1/user/basics.indexing.html
请参阅此处了解numpy数组的所有可能的索引方法:http://docs.scipy.org/doc/numpy-1.10.1/user/basics.indexing.html
#1
4
Just use a tuple:
只需使用元组:
>>> A[(3, 1)]
8
>>> A[tuple(ind)]
8
The A[]
actually calls the special method __getitem__
:
A []实际上调用了特殊方法__getitem__:
>>> A.__getitem__((3, 1))
8
and using a comma creates a tuple:
并使用逗号创建一个元组:
>>> 3, 1
(3, 1)
Putting these two basic Python principles together solves your problem.
将这两个基本的Python原则放在一起可以解决您的问题。
You can store your index in a tuple in the first place, if you don't need NumPy array features for it.
如果您不需要NumPy数组功能,则可以首先将索引存储在元组中。
#2
4
That is because by giving an array you actually ask
那是因为通过给出一个实际问的数组
A[[3,1]]
Which gives the third and first index of the 2d array instead of the first index of the third index of the array as you want.
它给出了2d数组的第三个和第一个索引,而不是你想要的数组的第三个索引的第一个索引。
You can use
您可以使用
A[ind[0],ind[1]]
You can also use (if you want more indexes at the same time);
您也可以使用(如果您想同时获得更多索引);
A[indx,indy]
Where indx
and indy
are numpy arrays of indexes for the first and second dimension accordingly.
其中indx和indy是第一维和第二维的numpy索引数组。
See here for all possible indexing methods for numpy arrays: http://docs.scipy.org/doc/numpy-1.10.1/user/basics.indexing.html
请参阅此处了解numpy数组的所有可能的索引方法:http://docs.scipy.org/doc/numpy-1.10.1/user/basics.indexing.html