且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有没有办法删除字符串中重复和连续的单词/短语?

更新时间:2023-01-12 23:13:40

你可以使用 re 模块.

>>>s = 'foo foo bar bar'>>>re.sub(r'\b(.+)\s+\1\b', r'\1', s)'富吧'>>>s = 'foo bar foo bar foo bar'>>>re.sub(r'\b(.+)\s+\1\b', r'\1', s)'foo bar foo bar'

如果要匹配任意数量的连续出现:

>>>s = 'foo bar foo bar foo bar'>>>re.sub(r'\b(.+)(\s+\1\b)+', r'\1', s)'富吧'

编辑.最后一个例子的补充.为此,您必须在有重复短语时调用 re.sub.所以:

>>>s = '这是一个句子句子句子这是一个短语短语重复的句子短语重复的句子'>>>而 re.search(r'\b(.+)(\s+\1\b)+', s):... s = re.sub(r'\b(.+)(\s+\1\b)+', r'\1', s)...>>>秒'这是一个短语重复的句子'

Is there a way to remove duplicate and continuous words/phrases in a string? E.g.

[in]: foo foo bar bar foo bar

[out]: foo bar foo bar

I have tried this:

>>> 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'

What happens when it gets a little more complicated and i want to remove phrases (let's say phrases can be made up of up to 5 words)? how can it be done? E.g.

[in]: foo bar foo bar foo bar

[out]: foo bar

Another example:

[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 .

You can use re module for that.

>>> s = 'foo foo bar bar'
>>> re.sub(r'\b(.+)\s+\1\b', r'\1', s)
'foo bar'

>>> s = 'foo bar foo bar foo bar'
>>> re.sub(r'\b(.+)\s+\1\b', r'\1', s)
'foo bar foo bar'

If you want to match any number of consecutive occurrences:

>>> s = 'foo bar foo bar foo bar'
>>> re.sub(r'\b(.+)(\s+\1\b)+', r'\1', s)
'foo bar'    

Edit. An addition for your last example. To do so you'll have to call re.sub while there're duplicate phrases. So:

>>> s = 'this is a sentence sentence sentence this is a sentence where phrases phrases duplicate where phrases duplicate'
>>> while re.search(r'\b(.+)(\s+\1\b)+', s):
...   s = re.sub(r'\b(.+)(\s+\1\b)+', r'\1', s)
...
>>> s
'this is a sentence where phrases duplicate'