python - 如何在没有重复的情况下找到两个数组中最接近的元素 [duplicates] 并在 Python 中返回两个数组的索引?

标签 python

我有 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)

在这个例子中重复了零值!!!如果我需要数组中值相近的两个元素的索引,则会显示其中一个数组的索引

最佳答案

您可以使用 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')

关于python - 如何在没有重复的情况下找到两个数组中最接近的元素 [duplicates] 并在 Python 中返回两个数组的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58615335/

相关文章:

python - Numpy.argsort - 看不出有什么问题

python - Pandas:从 df 的列中的值中删除第一个和最后一个元素

python - Ubuntu 安装 Boto 错误

python - 运行一个小型 HTTP 服务器来检查回调

python - 如何在Python中使用方括号的类中实现方法(如pandas DataFrame中的loc)?

python - 如何使用 requests.session 以便在后续获取请求中保留并重用 header

python - 使用 Azure 数据工厂从 REST API 获取数据

python - qt5(和 python)——如何获取用户操作系统的默认缩略图大小

python - 从 Python 中的类方法返回多个值的最有效方法是什么?

Python If Else 循环