在python中如何删除两个字符串之间的重复单词?

2024-04-27 02:22:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在和OCR合作一个项目。经过一些操作后,我有两个这样的字符串:

s1 = "This text is a test of"
s2 = "a test of the reading device"

我想知道如何删除第二个字符串的重复单词。我的想法是找出在每个列表中重复出现的单词的位置。我试过了:

^{pr2}$

现在我有了重复的单词和它们在第一和第二个列表中的位置。我需要它来逐字比较,如果它们的顺序相同。这是因为同一个单词可能会在字符串中出现两次或更多次(未来验证)。在

最后我想要一个这样的最后一个字符串:

ns2 = "the reading device"    
sf= "This text is a test of the reading device"

我在Windows7上使用Python2.7。在


Tags: ofthe项目字符串texttest列表is
2条回答

也许是这个?
' '.join([x for x in s1.split(' ')] + [y for y in s2.split(' ') if y not in s1.split(' ')]) 我没有仔细测试过,但这可能是处理此类需求的一个好主意。在

这是另一个尝试

from difflib import SequenceMatcher as sq
match = sq(None, s1, s2).find_longest_match(0, len(s1), 0, len(s2))

结果

^{pr2}$

This text is a test of the reading device

相关问题 更多 >