0
我想找到兩個列表中的所有常見序列。 例如:在Python中找到兩個列表的所有常見序列
list1 = [1,2,3,4,5,6,7,8,9]
list2 = [1,2,7,8,9,5,7,5,6]
我想輸出:
matched_list = [[1,2],[7,8,9],[5,6]]
我的代碼如下:
import difflib
def matches(first_string,second_string):
s = difflib.SequenceMatcher(None, first_string,second_string)
match = [first_string[i:i+n] for i, j, n in s.get_matching_blocks() if n > 0]
return match
但我得到的輸出:
match = [[1,2] ,[7,8,9]]
應該是什麼'list1的=輸出[1,2]','列表2 = [1,2,1, 2]'? –