比较单词列表,将相似的单词删除并添加到新字符串中



我有两个字符串,我已经将它们转换为单词a和B的列表。我正在尝试制作一个算法,从左边开始选择一个单词。如果该单词在第二个字符串中作为单词或单词的一部分出现,则将该单词添加到新的公共字符串中,并删除在第二字符串中发现的单词的第一次出现的全部内容。大写字母和小写字母被认为是不同的字母。我称这种算法为Diff.

示例:

List A: " The quick brown fox did jump over a log"
List B: " The brown rabbit quickly did outjump the fox"

如果我把A和B区分开来,我会得到"那只敏捷的棕色狐狸确实跳了一个">

如果我把B和A区分开来,我会得到"棕色狐狸">

到目前为止我拥有的代码:

import re
a = 'The quick brown fox did jump over a log'
aa = re.sub("[^w]", " ",  a).split()
b = 'The brown rabbit quickly did outjump the fox'
bb = re.sub("[^w]", " ",  b).split()
print (aa)
print (bb)

上面的代码是我用来将字符串更改为单词列表的代码。

这可以在中工作

common_str = ""
a =  " The quick brown fox did jump over a log"
b = " The brown rabbit quickly did outjump the fox"
alist = a.split(" ")
blist = b.split(" ")
for item in blist:
if item in alist:
#add word to common string
common_str += item + " "
#remove from second string
i = item + " " #item plus trailing space
a.replace(i, "")
print(common_str)
print(a)
print(b)
a = 'The quick brown fox did jump over a log'
aa = a.split(" ")
b = 'The brown rabbit quickly did outjump the fox'
bb = b.split(" ")
common = []
for i in aa:
for j in bb:
if (i == j):
common.append(j)
break
print(" ".join(common))

这个输出将是"棕色狐狸做的">

a = 'The quick brown fox did jump over a log'
aa = a.split(" ")
b = 'The brown rabbit quickly did outjump the fox'
bb = b.split(" ")
common = []
for i in bb:
for j in aa:
if (i == j):
common.append(j)
break
print(" ".join(common))

输出是"棕色狐狸">

最新更新