簡體   English   中英

python正則表達式去除重復單詞

[英]python regular expression to remove repeated words

我是 Python 新手

如果有重復的單詞,我想改變句子。

正確

  • 前任。 “這真是太好了”-->“這真是太好了”
  • 前任。 “這就是就是”-->“這就是”

現在我正在使用這個 reg。 但它確實在字母上發生了變化。 前任。 “我的朋友和我很高興”-->“我的朋友和我很高興”(它刪除了“我”和空格)錯誤

text = re.sub(r'(\w+)\1', r'\1', text) #remove duplicated words in row

我怎樣才能做同樣的改變,但它必須檢查單詞而不是字母?

使用itertools.groupby非正則表達式解決方案:

>>> strs = "this is just is is"
>>> from itertools import groupby
>>> " ".join([k for k,v in groupby(strs.split())])
'this is just is'
>>> strs = "this just so so so nice" 
>>> " ".join([k for k,v in groupby(strs.split())])
'this just so nice'
text = re.sub(r'\b(\w+)( \1\b)+', r'\1', text) #remove duplicated words in row

\\b匹配空字符串,但僅在單詞的開頭或結尾。

  • \\b:匹配詞邊界

  • \\w:任意單詞字符

  • \\1:用找到的第二個單詞替換匹配項

     import re def Remove_Duplicates(Test_string): Pattern = r"\\b(\\w+)(?:\\W\\1\\b)+" return re.sub(Pattern, r"\\1", Test_string, flags=re.IGNORECASE) Test_string1 = "Good bye bye world world" Test_string2 = "Ram went went to to his home" Test_string3 = "Hello hello world world" print(Remove_Duplicates(Test_string1)) print(Remove_Duplicates(Test_string2)) print(Remove_Duplicates(Test_string3))

結果:

    Good bye world
    Ram went to his home
    Hello world

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM