Python:在字符串中翻译/替换不是你想要的单词

1 投票
2 回答
508 浏览
提问于 2025-04-16 06:22

简单来说,我有很多短语,但我只对包含特定单词的短语感兴趣。我想做的事情是:第一,检查这个单词是否在短语里;如果在的话,第二,删除其他所有的单词。我可以用很多if语句和for循环来实现这个,但我在想有没有更简洁、更符合Python风格的方法来做到这一点。

2 个回答

1

这是一个基于正则表达式的解决方案:

>>> import re
>>> phrase = "A lot of interesting and boring words"
>>> regex = re.compile(r"\b(?!(?:interesting|words)\b)\w+\W*")
>>> clean = regex.sub("", phrase)
>>> clean
'interesting words'

这个正则表达式的工作原理如下:

\b             # start the match at a word boundary
(?!            # assert that it's not possible to match
 (?:           # one of the following:
  interesting  # "interesting"
  |            # or
  words        # "words"
 )             # add more words if desired...
 \b            # assert that there is a word boundary after our needle matches
)              # end of lookahead
\w+\W*         # match the word plus any non-word characters that follow.
3

这里有一个建议的算法:

  • 对于每一个短语
    1. 先检查有没有那个有趣的词
    2. 如果有,就把其他的词都删掉
    3. 如果没有,就继续看下一个短语

是的,按照这个思路实现的话会需要写很多“如果”和“循环”,但你会惊讶地发现,这样的逻辑在Python中其实很简单、很干净。

还有一种更简洁的方法可以达到同样的效果,那就是使用列表推导式,这样可以让逻辑变得更简单。假设 phrases 是一个短语的列表:

phrases = [process(p) if isinteresting(p) else p for p in phrases]

这里需要对 processisinteresting 函数给出合适的定义。

撰写回答