找到路径之间的公共子路径。
subpaths的Python项目详细描述
子路径
查找公共子路径并计算路径之间所有子路径的出现次数。
示例
查找路径之间的公共子路径
from pprint import pprint
from subpaths.subpaths import find_all_common_subpaths_between
path_a = [1, 2, 3, 4, 5]
path_b = [3, 4, 5, 7]
path_c = [2, 3, 4, 8, 9, 7]
pprint(find_all_common_subpaths_between(path_a, path_b, path_c))
将返回:
{(3, 4)}
计算路径之间的所有子路径
from pprint import pprint
from subpaths.subpaths import find_all_subpath_occurrences
path_a = [1, 2, 3, 4, 5]
pprint(find_all_subpath_occurrences(path_a))
path_b = [3, 4, 5, 7]
path_c = [2, 3, 4, 8, 9, 7]
pprint(find_all_subpath_occurrences(path_a, path_b, path_c))
将返回:
defaultdict(<class 'int'>,
{(1, 2): 1,
(1, 2, 3): 1,
(1, 2, 3, 4): 1,
(1, 2, 3, 4, 5): 1,
(2, 3): 1,
(2, 3, 4): 1,
(2, 3, 4, 5): 1,
(3, 4): 1,
(3, 4, 5): 1,
(4, 5): 1})
defaultdict(<class 'int'>,
{(1, 2): 1,
(1, 2, 3): 1,
(1, 2, 3, 4): 1,
(1, 2, 3, 4, 5): 1,
(2, 3): 2,
(2, 3, 4): 2,
(2, 3, 4, 5): 1,
(2, 3, 4, 8): 1,
(2, 3, 4, 8, 9): 1,
(2, 3, 4, 8, 9, 7): 1,
(3, 4): 3,
(3, 4, 5): 2,
(3, 4, 5, 7): 1,
(3, 4, 8): 1,
(3, 4, 8, 9): 1,
(3, 4, 8, 9, 7): 1,
(4, 5): 2,
(4, 5, 7): 1,
(4, 8): 1,
(4, 8, 9): 1,
(4, 8, 9, 7): 1,
(5, 7): 1,
(8, 9): 1,
(8, 9, 7): 1,
(9, 7): 1})
下载
pip install subpaths
未来改进
- 将短路径和最长路径绑定以生成和计数