【问题标题】:Index to closest coordinate最近坐标的索引
【发布时间】:2022-11-17 21:30:24
【问题描述】:

我有这个功能

A=[(1,2,3),(2,3,4)]
B=[(2,4,3),(1,8,1),(2,3,5),(1,5,3)]
def closestNew(A,B):
    C = {}
    for bp in B:
       closestDist = -1
       for ap in A:
          dist = sum(((bp[0]-ap[0])**2, (bp[1]-ap[1])**2, (bp[2]-ap[2])**2))
          if(closestDist > dist or closestDist == -1):
             C[bp] = ap
             closestDist = dist
    return C

这将返回两个列表之间最接近的坐标。

输出:

{(1, 2, 3): (2, 4, 3), (2, 3, 4): (2, 3, 5)}

但是,我想要指数阵列的(与数组 A 匹配的点(检查输出))以及在单独的列表中,有什么想法吗?

返回

idx=[0,2]

【问题讨论】:

    标签: python list indexing tuples distance


    【解决方案1】:
    A=[(1,2,3),(2,3,4)]
    B=[(2,4,3),(1,8,1),(2,3,5),(1,5,3)]
    C={(1, 2, 3): (2, 4, 3), (2, 3, 4): (2, 3, 5)}
    

    C 是一个字典,它的值对应于 B 上的点。

    idx=[]   # an empty list
    for x in C.values():
        idx.append(B.index(x))    # index function to find the index of values in B
    
    print(idx)
    #[0, 2]
    

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      • 2019-07-31
      相关资源
      最近更新 更多