以下是一种可能的解决方案:

def find_common_paths(path_a, path_b):
    common_paths = []
    
    for i in range(len(path_a)-1):
        for j in range(len(path_b)-1):
            if path_a[i] == path_b[j] and path_a[i+1] == path_b[j+1]:
                common_paths.append([path_a[i:i+2], path_b[j:j+2]])
    
    return common_paths

path_a = ['HYS66', 'KYP1-2', 'HYS66']
path_b = ['HYS66', 'KYP1-2', 'HYS66']

common_paths = find_common_paths(path_a, path_b)
print(common_paths)

输出结果为:[[['HYS66', 'KYP1-2'], ['HYS66', 'KYP1-2']], [['KYP1-2', 'HYS66'], ['KYP1-2', 'HYS66']]]

Python 找出两个列表所有头尾相同的最小公共路径

原文地址: https://www.cveoy.top/t/topic/pXNL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录