有没有方法可以删除字符串中重复和连续的单词/短语?E、 g
[in]:foo foo bar bar foo bar
[out]:foo bar foo bar
我试过了:
>>> s = 'this is a foo bar bar black sheep , have you any any wool woo , yes sir yes sir three bag woo wu wool'
>>> [i for i,j in zip(s.split(),s.split()[1:]) if i!=j]
['this', 'is', 'a', 'foo', 'bar', 'black', 'sheep', ',', 'have', 'you', 'any', 'wool', 'woo', ',', 'yes', 'sir', 'yes', 'sir', 'three', 'bag', 'woo', 'wu']
>>> " ".join([i for i,j in zip(s.split(),s.split()[1:]) if i!=j]+[s.split()[-1]])
'this is a foo bar black sheep , have you any wool woo , yes sir yes sir three bag woo wu'
当它变得更复杂一点,我想删除短语(比如短语最多可以由5个单词组成)时会发生什么?怎么能做到呢?E、 g
[in]:foo bar foo bar foo bar
[out]:foo bar
另一个例子:
[in]:this is a sentence sentence sentence this is a sentence where phrases phrases duplicate where phrases duplicate . sentence are not prhases .
[out]:this is a sentence where phrases duplicate . sentence are not prhases .
我爱
itertools
。好像每次我想写东西的时候,itertools已经有了。在本例中,groupby
获取一个列表,并将该列表中重复的、连续的项分组到(item_value, iterator_of_those_values)
的元组中。在这里使用它就像:因此,让我们用一个函数来扩展一下,这个函数返回一个删除了重复值的列表:
^{pr2}$这对一个词的短语很好,但对较长的短语没有帮助。怎么办?好吧,首先,我们要通过大步跳过原始短语来检查较长的短语:
现在我们在做饭!好 啊。所以我们的策略是首先删除所有的单字重复。接下来,我们将删除两个单词的重复项,从偏移量0开始,然后从1开始。然后,从偏移量0、1和2开始,依次类推,直到找到5个单词重复:
综合起来:
你可以使用re模块。在
如果要匹配任何连续出现的次数:
^{pr2}$编辑。最后一个例子的补充。为此你得打电话re.sub公司虽然有重复的短语。所以:
就我个人而言,我不认为我们需要为此使用任何其他模块(尽管我承认其中一些模块非常棒)。我只是通过简单的循环来管理这个问题,首先将字符串转换成一个列表。我在上面列出的所有例子上都试过了。它工作得很好。在
相关问题 更多 >
编程相关推荐