如何找到两个字符串的并集并维持秩序



我有两个字符串,我想找到它们的并集。在这样做的同时,我想维持秩序。我这样做的目的是尝试几种方法来OCR图像并获得不同的结果。我想把所有不同的结果组合成一个最有内容的结果。

这至少是我想要的:

#example1
string1 = "This is a test trees are green roses are red"
string2 = "This iS a TEST trees 12.48.1952 anthony gonzalez"
finalstring = "this is a test trees are green roses are red 12.48.1952 anthony gonzalez" 
#example2
string2 = "This is a test trees are green roses are red"
string1 = "This iS a TEST trees 12.48.1952 anthony gonzalez"
finalstring = "this is a test trees are green roses are red 12.48.1952 anthony gonzalez"
#example3
string1 = "telephone conversation in some place big image on screen"
String2 = "roses are red telephone conversation in some place big image on screen"
finalstring = "roses are red telephone conversation in some place big image on screen"
#or the following - both are fine in this scenario.
finalstring = "telephone conversation in some place big image on screen roses are red "

这就是我尝试过的:

>>> string1 = "This is a test trees are green roses are red"
>>> string2 = "This iS a TEST trees 12.48.1952 anthony gonzalez"
>>> list1 = string1.split(" ")
>>> list2 = string2.split(" ")
>>> " ".join(list(set(list1) | set(list2))).lower()
'a gonzalez this is trees anthony roses green are test 12.48.1952 test is red'

您可以使用difflib.SequenceMatcher进行以下操作:

import difflib
def merge (l, r):
    m = difflib.SequenceMatcher(None, l, r)
    for o, i1, i2, j1, j2 in m.get_opcodes():
        if o == 'equal':
            yield l[i1:i2]
        elif o == 'delete':
            yield l[i1:i2]
        elif o == 'insert':
            yield r[j1:j2]
        elif o == 'replace':
            yield l[i1:i2]
            yield r[j1:j2]

像这样使用:

>>> string1 = 'This is a test trees are green roses are red'
>>> string2 = 'This iS a TEST trees 12.48.1952 anthony gonzalez'
>>> merged = merge(string1.lower().split(), string2.lower().split())
>>> ' '.join(' '.join(x) for x in merged)
'this is a test trees are green roses are red 12.48.1952 anthony gonzalez'

如果你想在字符级别上执行合并,你可以简单地修改调用,直接对字符串进行操作(而不是单词列表):

>>> merged = merge(string1.lower(), string2.lower())
>>> ''.join(merged)
'this is a test trees 12.48.1952 arenthony gronzaleen roses are redz'

此解决方案可以正确地维护字符串各个部分的顺序。因此,如果两个字符串都以一个公共部分结尾,但在结尾之前有一个不同的段,那么这两个不同的片段仍然会出现在结果中公共结尾之前。例如,合并A B DA C D将得到A B C D

因此,只需删除生成字符串的部分,就可以按正确的顺序找到每个原始字符串。如果从该示例结果中删除C,则返回第一个字符串;如果你去掉B,你会得到第二个字符串。这也适用于更复杂的合并。

不要为此使用集合。正如您所注意到的,由于set()保留了唯一的对象,因此只有一个得到了最终结果。

string1 = "This is a test trees are green roses are red"
string2 = "This iS a TEST trees 12.48.1952 anthony gonzalez"
str_lst = string1.split()
for s, t in zip(string1.split(), string2.split()):
    if s.lower() == t.lower():
        continue
    else:
        str_lst.append(t)
string = " ".join(s.lower() for s in str_lst)
#this is a test trees are green roses are red 12.48.1952 anthony gonzalez
" ".join(x if i >= len(string2.split()) or x == string2.lower().split()[i] else " ".join((x, string2.split()[i])) for i, x in enumerate(string1.lower().split()))

您可以使用生成器理解和类似的join来完成您想要的内容。这将i设置为string1中的单词的索引,将x设置为该单词。然后,它检查该单词是否在string2中,如果不是,则将i处的string2中的单词添加到x,以将两个单词都放入最后的字符串中。

最新更新