简体   繁体   English

通过与另一个列表匹配,从列表中检索最长的匹配值[Python 2.7]

[英]Retrieve the longest matching value from a list by matching with another list [Python 2.7]

There are two lists to be matched, li_a is the given list consists of sequence of characters of a sentence, whereas li_b is the collection of words. 有两个要匹配的列表, li_a是给定的列表,由句子的字符序列组成,而li_b是单词的集合。

li_a = ['T','h','e','s','e','45','a','r','e','c','a','r','s']


li_b = ['T','Th','The','Thes','These','a','ar','are','c','ca','car','cars']

The process is to match items of li_a iteratively, with li_b items. 这个过程是相匹配的项目li_a迭代,用li_b项目。 If the first character of li_a is similar to li_b items, the first character of li_a joins with next character, and redo the process until it reaches to its longest match. 如果第一个字符li_a类似于li_b项目的第一个字符li_a与下一个字符连接,直到它达到其最长匹配重做过程。 Then, the longest term should be split, and the process will continue till the end. 然后,应将最长的期限分开,该过程将持续到结束。 As the unknown characters and words of li_a which do not appear in li_b will be appended as they were before. 因为li_a中没有出现的li_b未知字符和单词将像以前一样被追加。

The final work should be like this: 最后的工作应该是这样的:

new_li = ['These','45','are','cars']

The attempt getting so far, but this works for two strings not for Lists , and it doesn't retrieve unidentified words. 到目前为止的尝试,但这对于两个字符串(不适用于List)有效 ,并且不会检索未识别的单词。

def longest_matched_substring(s1, s2):
    m = [[0] * (1 + len(s2)) for i in xrange(1 + len(s1))]
    longest, x_longest = 0, 0
    for x in xrange(1, 1 + len(s1)):
       for y in xrange(1, 1 + len(s2)):
           if s1[x - 1] == s2[y - 1]:
               m[x][y] = m[x - 1][y - 1] + 1
               if m[x][y] > longest:
                   longest = m[x][y]
                   x_longest = x
           else:
               m[x][y] = 0

    return s1[x_longest - longest: x_longest]

You can do it using two for loops and a temp variable as the following: 您可以使用两个for loops和一个temp variable来做到这一点,如下所示:

def longest_matched_substring(li1, li2):
    new_li = []
    tmp = ''
    for a in li1:
        tmp += a
        count = 0
        for b in li2:
            if tmp == b:
                count += 1
        if count == 0:
            tmp1 = tmp.replace(a, '')
            new_li.append(tmp1)
            tmp = a
    if li2.__contains__(tmp):
        new_li.append(tmp) 
    return new_li

INPUT: INPUT:

li_a = ['T','h','e','s','e','45','a','r','e','c','a','r','s']
li_b = ['T','Th','The','Thes','These','a','ar','are','c','ca','car','cars']
print longest_matched_substring(li_a, li_b)

OUTPUT: OUTPUT:

['These', '45', 'are', 'cars']

as for the new scenario, you can modify the function as the following: 对于新方案,可以将功能修改为以下内容:

def longest_matched_substring(li1, li2):
    new_li = []
    tmp = ''
    for a in li1:
        tmp += a
        count = 0
        for b in li2:
            if tmp == b:
                count += 1
        if count == 0:
            tmp1 = tmp.replace(a, '')

            new_li.append(tmp1)
            tmp = a
    if li_b.__contains__(tmp):
        new_li.append(tmp) 
    for e1 in new_li:
        tmp2 = e1
        rm = []
        for e2 in new_li:
            if e1 != e2:
                tmp2 += e2
                rm.append(e2)
                if tmp2 in li2:
                    new_li.insert(new_li.index(e1), tmp2) # if order matters
                    #new_li.append(tmp2) if order doesn't matter
                    for r in rm:
                        new_li.remove(r)
                    new_li.remove(e1)
                    rm = []
                    break
    return new_li

INPUT: INPUT:

li_a = ['T','h','e','s','e','45','a','r','e','c','a','r','s']
li_b = ['T','Th','The','These','a','ar','are','c','ca','car','cars']
print longest_matched_substring(li_a, li_b)

OUTPUT: OUTPUT:

['These', '45', 'are', 'cars']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从列表 python2.7 中检索值 - Retrieve value from list python2.7 通过在Python中进行匹配从列表中查找特定值 - Finding particular value from a list by matching in Python 从 Python 中的列表中删除最后一个匹配值 - Remove last matching value from a list in Python 匹配两个列表以从 Python 中的第三个列表中检索值 - Matching two lists to retrieve values from a third list in Python Python:将一个列表中的值与另一个列表中的值序列进行匹配 - Python: matching values from one list to the sequence of values in another list Python:将一个列表中的值匹配到另一个列表中的值序列 - Python: matching values from one list to the sequences of values in another list 将字典中的值列表中的元素与python中相同列表中的另一个元素进行匹配 - Matching element from value list in a dictionary to another element in the same list in python 如何基于Python中的匹配键将键值对添加到另一个字典列表中的现有字典列表中 - How to append key value pair to an existing list of dict from another list of dict based on matching Key in Python 如何更新列表值,匹配另一个列表 - how update a list value, matching another list 将列表的项目与 python 中的另一个列表匹配 - Matching a list's item with another list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM