【发布时间】:2021-11-17 11:40:40
【问题描述】:
如何在python中的下一个单词上附加否定粒子(对于所有可以是“no”的文本)?
例如,使这个字符串['This is not apple']
进入这个:['This is not_apple']
【问题讨论】:
-
您的输入是一个列表,而不是一个字符串(您可能需要更新您的问题以澄清)
如何在python中的下一个单词上附加否定粒子(对于所有可以是“no”的文本)?
例如,使这个字符串['This is not apple']
进入这个:['This is not_apple']
【问题讨论】:
你可以使用正则表达式:
\bnot\s+(?=\w) 匹配单词not(但不是其他以not 结尾的单词)后跟一个或多个空格和另一个单词。
import re
s = 'This is not apple'
s2 = re.sub(r'\bnot\s+(?=\w)', 'not_', s)
输出:'This is not_apple'
import re
l = ['This is not apple']
[re.sub(r'\bnot\s+(?=\w)', 'not_', s) for s in l]
输出:['This is not_apple']
【讨论】: