简体   繁体   中英

find all common sequences of two list in python

I want to find all the common sequences in two list. for example:

list1 = [1,2,3,4,5,6,7,8,9]
list2 = [1,2,7,8,9,5,7,5,6]

I want output as:

matched_list = [[1,2],[7,8,9],[5,6]]

my code is below:

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

But I am getting the output as:

match = [[1,2] ,[7,8,9]]

If the output order is not important, a multi pass solution could do the trick. Every time you find a match, remove the sub-string/sub-lists from both the lists/strings.

Implementation

def matches(list1, list2):
    while True:
        mbs = difflib.SequenceMatcher(None, list1, list2).get_matching_blocks()
        if len(mbs) == 1: break
        for i, j, n in mbs[::-1]:
            if n > 0: yield list1[i: i + n]
            del list1[i: i + n]
            del list2[j: j + n]

Sample Output

>>> list(matches(list1, list2))
[[7, 8, 9], [1, 2], [5, 6]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM