1

我有一个字符串:"y, i agree with u."

我有数组字典[(word_will_replace, [word_will_be_replaced])]

[('yes', ['y', 'ya', 'ye']), ('you', ['u', 'yu'])]

我想根据数组字典将'y' 替换为 'yes''u' 替换为 'you' 。

所以我想要的结果:"yes, i agree with you."

我想把标点符号保留在那里。

4

3 回答 3

3
import re
s="y, i agree with u. yu."
l=[('yes', ['y', 'ya', 'ye']), ('you', ['u', 'yu'])] 
d={ k : "\\b(?:" + "|".join(v) + ")\\b" for k,v in l}
for k,r in d.items(): s = re.sub(r, k, s)  
print s

输出

yes, i agree with you. you.
于 2013-05-17T03:22:25.410 回答
2

扩展@gnibbler's answer from Replacing substrings given a dictionary of strings-to-be-replaced as keys and replacement as values。在评论中使用由 Raymond Hettinger 实现的技巧的Python 。

import re
text = "y, i agree with u."
replacements = [('yes', ['y', 'ya', 'ye']), ('you', ['u', 'yu'])]
d = {w: repl for repl, words in replacements for w in words}
def fn(match):
    return d[match.group()]

print re.sub('|'.join(r'\b{0}\b'.format(re.escape(k)) for k in d), fn, text)

>>> 
yes, i agree with you.
于 2013-05-17T03:23:56.403 回答
0

那不是字典——它是一个列表,但它可以dict很容易地转换为一个列表。但是,在这种情况下,我会更明确一点:

d = {}
replacements = [('yes', ['y', 'ya', 'ye']), ('you', ['u', 'yu'])]
for value,words in replacements:
    for word in words:
        d[word] = value

现在您有了字典映射响应来替换它们:

{'y':'yes', 'ya':'yes', 'ye':'yes',...}

一旦你有了它,你可以使用正则表达式从这里弹出我的答案: https ://stackoverflow.com/a/15324369/748858

于 2013-05-17T03:17:59.093 回答