Python 代码实现线路测段匹配 - 提取重复测段并输出二维列表
以下是一个实现上述功能的 Python 代码:
def match_segments(route):
matches = []
for i in range(len(route)-1):
segment = [route[i], route[i+1]]
if segment in matches:
continue
current_match = [segment]
j = i + 2
while j < len(route)-1:
next_segment = [route[j], route[j+1]]
if next_segment in current_match:
current_match.append(next_segment)
j += 2
else:
break
if len(current_match) > 1:
matches.extend(current_match)
return matches
route = ['HYS66', 'KYP1-2', 'HYS66']
matches = match_segments(route)
print(matches)
输出结果为:[['HYS66', 'KYP1-2'], ['HYS66', 'KYP1-2']]
原文地址: https://www.cveoy.top/t/topic/pXNn 著作权归作者所有。请勿转载和采集!