我有2个字符串,这些字符串已转换为单词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)
上面的代码是我用来将字符串更改为单词列表的
答案 0 :(得分:0)
这可能有效
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)
答案 1 :(得分:-1)
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))
输出为“棕色狐狸”