【发布时间】:2015-07-29 09:50:19
【问题描述】:
这看起来应该是直截了当的,其实不然单词。
例如:
x='hello world'
x.replace('llo','ll)
返回:
'hell world'
但我不希望这种情况发生。
在空格上拆分字符串适用于单个单词(unigrams),但我也想替换 n-grams
所以:
'this world is a happy place to be'
要转换为:
'this world is a miserable cesspit to be'
并且在空白处拆分不起作用。
Python3 中是否有一个内置函数允许我这样做?
我能做到:
if len(new_string.split(' '))>1:
x.replace(old_string,new_string)
else:
x_array=x.split(' ')
x_array=[new_string if y==old_string else y for y in x_array]
x=' '.join(x_array)
【问题讨论】:
-
用单词
\boundaries 编写一个正则表达式并使用re.sub而不是str.replace(参见re.sub(r'\bllo\b', 'll', 'hello world'))? -
比我的解决方案更简洁,谢谢
标签: python-3.x