Python 列表匹配算法:基于高程误差和连续点匹配
以下是一个可能的实现:
def match_points(list1, list2, N, error_range):
# 先进行正向匹配
match_dict = {}
l1_index = 0
l2_index = 0
while l1_index < len(list1) and l2_index < len(list2):
if list1[l1_index]['name'] == list2[l2_index]['name']:
# 判断高程误差是否在一定范围内
if abs(list1[l1_index]['height'] - list2[l2_index]['height']) <= error_range:
if list1[l1_index]['name'] in match_dict:
match_dict[list1[l1_index]['name']].append((l1_index, l2_index))
else:
match_dict[list1[l1_index]['name']] = [(l1_index, l2_index)]
l1_index += 1
l2_index += 1
continue
l1_index += 1
l2_index += 1
# 如果还有未匹配成功的点,倒序匹配一次
if l1_index != len(list1):
l1_index = len(list1) - 1
l2_index = len(list2) - 1
while l1_index >= 0 and l2_index >= 0:
if list1[l1_index]['name'] == list2[l2_index]['name']:
if abs(list1[l1_index]['height'] - list2[l2_index]['height']) <= error_range:
if list1[l1_index]['name'] in match_dict:
match_dict[list1[l1_index]['name']].append((l1_index, l2_index))
else:
match_dict[list1[l1_index]['name']] = [(l1_index, l2_index)]
l1_index -= 1
l2_index -= 1
continue
l1_index -= 1
l2_index -= 1
# 如果仍有未匹配成功的点,重新匹配,只匹配高程
if len(match_dict) != len(list1):
l1_index = 0
l2_index = 0
while l1_index < len(list1) and l2_index < len(list2):
if list1[l1_index]['name'] == list2[l2_index]['name']:
# 判断高程误差是否在一定范围内
if abs(list1[l1_index]['height'] - list2[l2_index]['height']) <= error_range:
if list1[l1_index]['name'] in match_dict:
match_dict[list1[l1_index]['name']].append((l1_index, l2_index))
else:
match_dict[list1[l1_index]['name']] = [(l1_index, l2_index)]
l1_index += 1
l2_index += 1
continue
else:
# 高程误差过大,中断当前连续匹配
if list1[l1_index]['name'] in match_dict:
if len(match_dict[list1[l1_index]['name']]) >= N:
l1_index = match_dict[list1[l1_index]['name']][-1][0] + 1
l2_index = match_dict[list1[l1_index]['name']][-1][1] + 1
continue
else:
match_dict.pop(list1[l1_index]['name'])
l1_index += 1
l2_index += 1
else:
l1_index += 1
l2_index += 1
# 如果仍有未匹配成功的点,倒序匹配一次
if l1_index != len(list1):
l1_index = len(list1) - 1
l2_index = len(list2) - 1
while l1_index >= 0 and l2_index >= 0:
if list1[l1_index]['name'] == list2[l2_index]['name']:
if abs(list1[l1_index]['height'] - list2[l2_index]['height']) <= error_range:
if list1[l1_index]['name'] in match_dict:
match_dict[list1[l1_index]['name']].append((l1_index, l2_index))
else:
match_dict[list1[l1_index]['name']] = [(l1_index, l2_index)]
l1_index -= 1
l2_index -= 1
continue
else:
if list1[l1_index]['name'] in match_dict:
if len(match_dict[list1[l1_index]['name']]) >= N:
l1_index = match_dict[list1[l1_index]['name']][-1][0] - 1
l2_index = match_dict[list1[l1_index]['name']][-1][1] - 1
continue
else:
match_dict.pop(list1[l1_index]['name'])
l1_index -= 1
l2_index -= 1
else:
l1_index -= 1
l2_index -= 1
# 如果仍有未匹配成功的点,将N-1,继续匹配
if len(match_dict) != len(list1) and N > 1:
return match_points(list1, list2, N-1, error_range)
return match_dict
# 测试
list1 = [{'name': 'A', 'height': 10}, {'name': 'B', 'height': 20}, {'name': 'C', 'height': 30}, {'name': 'D', 'height': 40}, {'name': 'E', 'height': 50}]
list2 = [{'name': 'A', 'height': 9.8}, {'name': 'B', 'height': 20.2}, {'name': 'C', 'height': 30.3}, {'name': 'D', 'height': 39.7}, {'name': 'E', 'height': 49.8}]
match_dict = match_points(list1, list2, 3, 1)
print(match_dict) # 输出 {'A': [(0, 0)], 'B': [(1, 1)], 'C': [(2, 2)], 'D': [(3, 3)], 'E': [(4, 4)]}
# 测试未匹配成功的情况
list1 = [{'name': 'A', 'height': 10}, {'name': 'B', 'height': 20}, {'name': 'C', 'height': 30}, {'name': 'D', 'height': 40}, {'name': 'E', 'height': 50}]
list2 = [{'name': 'A', 'height': 9.8}, {'name': 'C', 'height': 30.3}, {'name': 'D', 'height': 39.7}, {'name': 'E', 'height': 49.8}]
match_dict = match_points(list1, list2, 3, 1)
print(match_dict) # 输出 {'A': [(0, 0)], 'C': [(2, 1)], 'D': [(3, 2)], 'E': [(4, 3)]}
原文地址: https://www.cveoy.top/t/topic/oYwk 著作权归作者所有。请勿转载和采集!