如何映射两个数组的值并返回映射值的索引

时间:2018-06-21 21:55:15

标签: python arrays numpy

我有2个数组,其中一个是Large_arr,另一个是sub_arr(是它的一个子集)。我要映射数组的值,并且数组的匹配值将返回大型数组的索引。我有我的代码,但抛出错误

    Traceback (most recent call last):
  File "C:/Users/hp/AppData/Local/Programs/Python/Python36/stack.py", line 36, in <module>
    if(sub_arr[i]==Large_arr[j]):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我的代码是

import numpy as np 

Large_arr=np.array([[0.12046862, 0.64457892],
 [0.47522597, 0.12350968],
 [0.1649585 , 0.50135502],
 [0.20104755 ,0.15218623],
 [0.03772419 ,0.42482297],
 [0.51633778 ,0.61209002],
 [0.24848648 ,0.99651906],
 [0.47374345, 0.09990318],
 [0.58390815 ,0.19781604],
 [0.9613725  ,0.45975827],
 [0.99008266 ,0.13487207],
 [0.14410988 ,0.36196475],
 [0.81349573 ,0.55896232],
 [0.72841399 ,0.02263056],
 [0.8692731  ,0.9754183 ],
 [0.87142787 ,0.66163271],
 [0.24342035 ,0.95821073],
 [0.94218857 ,0.7220602 ],
 [0.66716105 ,0.96875209]])

sub_arr=np.array([[0.12046862, 0.64457892],
                 [0.51633778 ,0.61209002],
                 [0.99008266 ,0.13487207],
                 [0.72841399 ,0.02263056],
                 [0.24342035 ,0.95821073],
                 [0.47374345, 0.09990318],
                 [0.9613725  ,0.45975827]])



s=[]
for i in range(0,len(Large_arr)):
    for j in range(0,len(sub_arr)):
        if(sub_arr[i]==Large_arr[j]):
            s.append(j)
            print("Value of s is\n",s)
        else:
            print("Value is none\n")

可以简化此方法吗?所以我的输出很有价值

S=[0,5,10,13,7,10,1] (example, index of large array where sub_array value stored)

3 个答案:

答案 0 :(得分:0)

首先,在循环中交换i和j索引。确保将它们用于索引到相应的数组。

第二,您正在执行的代码将子数组按元素进行比较,这将返回一个布尔数组,这是一个数组,而不是布尔本身,因此必须添加.all()以将其简化为单个布尔值。如果所有元素都为true,则.all()将为true,否则为false。

第三,您应该可以减少它。我在下面包含了代码,以显示所有这三点。

此外,您的换行符没有执行应有的操作。请确保在发布代码之前先对其进行测试,并提供详细信息,例如所使用的python版本。

import numpy as np                                                                                             

Large_arr=np.array([[0.12046862, 0.64457892],                                                                  
 [0.47522597, 0.12350968],                                                                                     
 [0.1649585 , 0.50135502],                                                                                     
 [0.20104755 ,0.15218623],                                                                                     
 [0.03772419 ,0.42482297],                                                                                     
 [0.51633778 ,0.61209002],                                                                                     
 [0.24848648 ,0.99651906],                                                                                     
 [0.47374345, 0.09990318],                                                                                     
 [0.58390815 ,0.19781604],                                                                                     
 [0.9613725  ,0.45975827],                                                                                     
 [0.99008266 ,0.13487207],                                                                                     
 [0.14410988 ,0.36196475],                                                                                     
 [0.81349573 ,0.55896232],                                                                                     
 [0.72841399 ,0.02263056],                                                                                     
 [0.8692731  ,0.9754183 ],                                                                                     
 [0.87142787 ,0.66163271],                                                                                     
 [0.24342035 ,0.95821073],                                                                                     
 [0.94218857 ,0.7220602 ],                                                                                     
 [0.66716105 ,0.96875209]])                                                                                    

sub_arr=np.array([[0.12046862, 0.64457892],                                                                    
                 [0.51633778 ,0.61209002],                                                                     
                 [0.99008266 ,0.13487207],                                                                     
                 [0.72841399 ,0.02263056],                                                                     
                 [0.24342035 ,0.95821073],                                                                     
                 [0.47374345, 0.09990318],                                                                     
                 [0.9613725  ,0.45975827]])                                                                    



s=[]                                                                                                           
for i in range(0,len(Large_arr)):                                                                              
    for j in range(0,len(sub_arr)):                                                                            
        if((sub_arr[j]==Large_arr[i]).all()):                                                                  
            s.append(i)                                                                                        
            print("Value of s is\n",s)                                                                         
        else:                                                                                                  
            #print("Value is none\n")                                                                          
            pass                                                                                               

opt = np.isin(Large_arr,sub_arr)                                                                               

print                                                                                                          
print "Bool array"                                                                                             
print opt                                                                                                      
print                                                                                                          
print "indexes"                                                                                                
print np.where(opt[:,0])[0]

答案 1 :(得分:0)

您可以尝试以下方法:

result_indices = np.argwhere(np.in1d(Large_arr, sub_arr))

那应该首先应用一个布尔掩码,然后为您提供匹配元素的索引。

如果这最终不是您想要的,请纠正我。

答案 2 :(得分:0)

我认为问题可能出在您的

if(sub_arr[i]==Large_arr[j]):

实际上,您似乎有两个二维数组,因此不能仅指定一个。