如何在Python中找到没有重复的两个数组中最接近的元素,并在Python中返回两个数组的索引?

nilou.mhy

我有2个数组list1和list2

list1= np.array([0.        , 0.09705882, 0.19411765, 0.29117647, 0.38823529,
       0.48529412, 0.58235294, 0.67941176, 0.77647059, 0.87352941,
       0.97058824, 1.06764706, 1.16470588, 1.26176471, 1.35882353,
       1.45588235, 1.55294118, 1.65      , 1.74705882, 1.84411765,
       1.94117647, 2.03823529, 2.13529412, 2.23235294, 2.32941176,
       2.42647059, 2.52352941, 2.62058824, 2.71764706, 2.81470588,
       2.91176471, 3.00882353, 3.10588235, 3.20294118, 3.3       ,
       3.39705882, 3.49411765, 3.59117647, 3.68823529, 3.78529412,
       3.88235294, 3.97941176, 4.07647059, 4.17352941, 4.27058824,
       4.36764706, 4.46470588, 4.56176471, 4.65882353, 4.75588235,
       4.85294118, 4.95      , 5.04705882, 5.14411765, 5.24117647,
       5.33823529, 5.43529412, 5.53235294, 5.62941176, 5.72647059,
       5.82352941, 5.92058824, 6.01764706, 6.11470588, 6.21176471,
       6.30882353, 6.40588235, 6.50294118, 6.6       ])
list2=np.array([3.3 , 3.2 , 3.1 , 3.  , 2.9 , 2.8 , 2.7 , 2.6 , 2.5 , 2.4 , 2.3 ,
       2.2 , 2.1 , 2.  , 1.9 , 1.8 , 1.7 , 1.6 , 1.5 , 1.4 , 1.3 , 1.2 ,
       1.1 , 1.05, 0.95, 0.85, 0.75, 0.7 , 0.6 , 0.5 , 0.4 , 0.3 , 0.2 ,
       0.1 , 0])


对于a中的每个元素,我想找到b中最接近的元素并返回它们两个的索引

list2aux = list(list2)
mylist = []
for idxlabel in range(0,len(list1)): 
    a = min(enumerate(list2aux), key=lambda x:abs(x[1]-list1[idxlabel]))
    list2aux[a[0]] = 0
    print(a)
    mylist.append(np.copy(a))

我的问题是,在list2中的一个元素被找到为“最佳匹配”之后,我想在值靠近的数组中打印两个元素的索引,我也想从搜索中删除它以避免在元素中找到另一个元素list1与list2中的相同元素匹配

(34, 0.0)
(33, 0.1)
(32, 0.2)
(31, 0.3)
(30, 0.4)
(29, 0.5)
(28, 0.6)
(27, 0.7)
(26, 0.75)
(25, 0.85)
(24, 0.95)
(23, 1.05)
(21, 1.2)
(20, 1.3)
(19, 1.4)
(18, 1.5)
(17, 1.6)
(16, 1.7)
(15, 1.8)
(14, 1.9)
(13, 2.0)
(12, 2.1)
(11, 2.2)
(10, 2.3)
(9, 2.4)
(8, 2.5)
(7, 2.6)
(6, 2.7)
(5, 2.8)
(4, 2.9)
(3, 3.0)
(2, 3.1)
(1, 3.2)
(0, 3.3)
(22, 1.1)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)

在此示例中,零值被重复!!!如果我需要两个值接近的数组中两个元素的索引,则会显示该数组之一的索引

Rithin Chalumuri

您可以numpy.where()用来获取numpy数组中匹配元素的索引。

给定以上两个列表,您可以尝试以下代码(找到后无需从list2中删除元素):


for i in range(len(list1)):

  temp_result = abs(list1[i] - list2) #Matrix subtraction

  min_val = np.amin(temp_result) #Getting the minimum value to get closest element
  min_val_index = np.where(temp_result == min_val) #To find index of minimum value

  closest_element = list2[min_val_index] #Actual value of closest element in list2

  print(i, list1[i], min_val_index[0][0], closest_element[0])


在list2中找到元素后立即将其删除,最终list2将为空,以便避免出现任何运行时错误,并进行安全检查。

for i in range(len(list1)):

  if (len(list2)) > 1: #When there are elements in list2

    temp_result = abs(list1[i] - list2) #Matrix subtraction

    min_val = np.amin(temp_result) #Getting the minimum value to get closest element
    min_val_index = np.where(temp_result == min_val) #To find index of minimum value

    closest_element = list2[min_val_index] #Actual value of closest element in list2

    list2 = list2[list2 != closest_element] #Remove closest element after found

    print(i, list1[i], min_val_index[0][0], closest_element[0]) #List1 Index, Element to find, List2 Index, Closest Element

  else: #All elements are already found

    print(i, list1[i], 'No further closest unique closest elements found in list2')

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档