我有一个字典,其中包含要替换的字符串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])