【发布时间】:2013-10-05 01:39:32
【问题描述】:
我有一本字典,所有键都是三个字母长:threeLetterDict={'abc': 'foo', 'def': 'bar', 'ghi': 'ha' ...}
现在我需要将句子abcdefghi 翻译成foobarha。我正在使用re.sub 尝试以下方法,但不知道如何将字典放入其中:
p = re.compile('.{3}') # match every three letters
re.sub(p,'how to put dictionary here?', "abcdefghi")
谢谢! (无需检查输入长度是否为三的倍数)
【问题讨论】:
标签: python regex dictionary