从蟒蛇字符串用另一个词替换词(replacing word with another word fr

2019-10-18 03:52发布

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

我有阵列字典[(word_will_replace, [word_will_be_replaced])]

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

我想根据数组字典与“你” 与“是”“U”代替“Y”。

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

我想保持标点符号那里。

Answer 1:

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.


Answer 2:

扩展从@ gnibbler的答案给出的字典替换子串到被替换为键和替代的价值。 蟒蛇与雷蒙德赫廷杰在评论中实施的提示。

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.


Answer 3:

这不是一本词典-这是一个清单,但它可以被转换成一个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



文章来源: replacing word with another word from the string in python