python两组列表每组列表中的每个元素是一个字典该字典包含点名和高程两个属性两个列表进行匹配;第一遍时当满足连续N个点的高程误差在一定范围内且点名也相同时匹配成功。否则只匹配高程当满足连续N个点的高程误差在一定范围内时认为匹配对应的点名是同一个点。当匹配结束时还有未匹配成功的点时倒序匹配一次仍有未匹配是将N-1继续匹配。
以下是一个简单的实现:
def match_points(points1, points2, n, error): """ 匹配两组点,当满足连续N个点的高程误差在一定范围内,且点名也相同时,匹配成功。 否则,只匹配高程,当满足连续N个点的高程误差在一定范围内时,认为匹配对应的点名是同一个点。 当匹配结束时,还有未匹配成功的点时,倒序匹配一次,仍有未匹配是,将N-1,继续匹配。 :param points1: 第一组点列表,每个元素是一个字典,包含点名和高程两个属性 :param points2: 第二组点列表,每个元素是一个字典,包含点名和高程两个属性 :param n: 连续匹配的点数 :param error: 高程误差范围 :return: 匹配结果,是一个元组列表,每个元组包含两个字典,分别表示匹配的两个点 """ result = [] matched1 = set() matched2 = set() for i in range(len(points1)): for j in range(len(points2)): if j in matched2: continue if points1[i]['name'] == points2[j]['name']: if i in matched1: break k = 1 while i + k < len(points1) and j + k < len(points2) and k < n: if abs(points1[i + k]['elevation'] - points2[j + k]['elevation']) > error: break if points1[i + k]['name'] != points2[j + k]['name']: break k += 1 if k == n: result.append((points1[i], points2[j])) matched1.add(i) matched2.add(j) break elif abs(points1[i]['elevation'] - points2[j]['elevation']) <= error: if i in matched1: break k = 1 while i + k < len(points1) and j + k < len(points2) and k < n: if abs(points1[i + k]['elevation'] - points2[j + k]['elevation']) > error: break k += 1 if k == n: result.append((points1[i], points2[j])) matched1.add(i) matched2.add(j) break if len(matched1) < len(points1) or len(matched2) < len(points2): if n > 1: return match_points(points1[::-1], points2[::-1], n - 1, error)[::-1] return result
示例用法
points1 = [{'name': 'A', 'elevation': 100}, {'name': 'B', 'elevation': 110}, {'name': 'C', 'elevation': 120}, {'name': 'D', 'elevation': 130}, {'name': 'E', 'elevation': 140}] points2 = [{'name': 'A', 'elevation': 95}, {'name': 'X', 'elevation': 115}, {'name': 'B', 'elevation': 105}, {'name': 'C', 'elevation': 125}, {'name': 'D', 'elevation': 130}, {'name': 'E', 'elevation': 145}] result = match_points(points1, points2, 3, 5) for r in result: print(r
原文地址: https://www.cveoy.top/t/topic/huCS 著作权归作者所有。请勿转载和采集!