我有一个词典,其中要替换的字符串为keys
,其替换为值。除了通过令牌查看字符串令牌之外,是否有更好/更快的方式进行替换?
我一直在这样做:
segmenter = {'foobar':'foo bar', 'withoutspace':'without space', 'barbar': 'bar bar'}
sentence = "this is a foobar in a barbar withoutspace"
for i in sentence.split():
if i in segmenter:
sentence.replace(i, segmenter[i])
答案 0 :(得分:5)
字符串在python中是不可变的。因此,str.replace
返回一个新字符串,而不是修改原始字符串。您可以在此处使用str.join()
和列表理解:
>>> segmenter = {'foobar':'foo bar', 'withoutspace':'without space', 'barbar': 'bar bar'}
>>> sentence = "this is a foobar in a barbar withoutspace"
>>> " ".join( [ segmenter.get(word,word) for word in sentence.split()] )
'this is a foo bar in a bar bar without space'
str.replace
的另一个问题是,它还会将"abarbarb"
等字词替换为
"abar barb"
。
答案 1 :(得分:4)
re.sub
可以调用返回替换的函数
segmenter = {'foobar':'foo bar', 'withoutspace':'without space', 'barbar': 'bar bar'}
sentence = "this is a foobar in a barbar withoutspace"
import re
def fn(match):
return segmenter[match.group()]
print re.sub('|'.join(re.escape(k) for k in segmenter), fn, sentence)