小编Ais*_*sha的帖子

替换所有连续重复的字母,忽略特定单词

我看到很多建议使用 re (正则表达式)或 python 中的 .join 删除句子中连续重复的字母,但我想对特殊单词有例外。

例如:

我想要这句话>sentence = 'hello, join this meeting heere using thiis lllink'

像这样>'hello, join this meeting here using this link'

知道我有这个单词列表要保留并忽略重复的字母检查:keepWord = ['Hello','meeting']

我发现有用的两个脚本是:

我有一个解决方案,但我认为还有一个更紧凑、更高效的解决方案。我现在的解决方案是:

import itertools

sentence = 'hello, join this meeting heere using thiis lllink'
keepWord = ['hello','meeting']

new_sentence = ''

for word in sentence.split():
    if word not in keepWord:
        new_word = …
Run Code Online (Sandbox Code Playgroud)

python regex text preprocessor

5
推荐指数
1
解决办法
300
查看次数

标签 统计

preprocessor ×1

python ×1

regex ×1

text ×1