简体   繁体   English

有没有办法通过将多维 numpy 数组与另一个 numpy 数组匹配来找到它的索引?

[英]Is there a way how i can find the index of an multidimensional numpy array by matching it to another numpy array?

Lets say i have the following case:假设我有以下情况:

array1=np.array([[1,0,0],[0,1,0],[0,0,1]])
array2=np.array([0,0,1])

now现在

array1[2] 

gives me the output给我输出

[0,0,1]

so now i want to have code that gives the index of array1 (in this case 2 ) as the output for matching array2 to array1 .所以现在我想要的代码给出array1的索引(在本例中为2 )作为将array2匹配到array1的输出。 Is there any elegant way to do this?有没有什么优雅的方法来做到这一点? i tried numpy.where but didn't get it right.我试过numpy.where但没有做对。

I am not sure I understand your question correctly.我不确定我是否正确理解您的问题。 But you could either do this:但是你可以这样做:

import numpy as np
np.where((array2 == array1).all(axis=1))

You can do this:你可以这样做:

index = np.argmax([0,0,1])

Or use this:或者使用这个:

indices = np.where(np.array([0,0,1]) == 1)

mask=np.all(array1 == array2, axis=1) will give you an array to True and False. mask=np.all(array1 == array2, axis=1)会给你一个 True 和 False 的数组。 This will flag True where the condition matches.这将在条件匹配的情况下标记 True。 You can search the index of all 'True' by using ind=np.argwhere(mask) .您可以使用ind=np.argwhere(mask)搜索所有 'True' 的索引。 In a way this is the same answer as @sehan2.在某种程度上,这与@sehan2 的答案相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM