我是python的新手,这就是为什么我正在努力解决我认为非常基本的问题.我有两个清单:
a = [0, 1, 2, 3, 4, 5, 6, 7]
b = [1, 2, 5, 6]
Run Code Online (Sandbox Code Playgroud)
在输出上我需要得到它们之间的所有交叉点:
c = [[1, 2], [5, 6]]
Run Code Online (Sandbox Code Playgroud)
那个算法是什么?
您可以使用difflib.SequenceMatcher来实现此目的
#Returns a set of matches from the given list. Its a tuple, containing
#the match location of both the Sequence followed by the size
matches = SequenceMatcher(None, a , b).get_matching_blocks()[:-1]
#Now its straight forward, just extract the info and represent in the manner
#that suits you
[a[e.a: e.a + e.size] for e in matches]
[[1, 2], [5, 6]]
Run Code Online (Sandbox Code Playgroud)