我有一个字符串:"y, i agree with u."
用另一个词替换单词的从字符串在python
而且我有数组字典[(word_will_replace, [word_will_be_replaced])]
:
[('yes', ['y', 'ya', 'ye']), ('you', ['u', 'yu'])]
我想用'来代替'Y'是'和'你''你'根据数组字典。
所以结果我想:"yes, i agree with you."
我想保持标点符号那里。
我有一个字符串:"y, i agree with u."
用另一个词替换单词的从字符串在python
而且我有数组字典[(word_will_replace, [word_will_be_replaced])]
:
[('yes', ['y', 'ya', 'ye']), ('you', ['u', 'yu'])]
我想用'来代替'Y'是'和'你''你'根据数组字典。
所以结果我想:"yes, i agree with you."
我想保持标点符号那里。
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.
这不是一本字典 - 它是一个列表,但它可以很容易地转换为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
对不起, 当我想替换:“你” 那里成为:“Yesou” –
延伸@ gnibbler的答案从Replacing substrings given a dictionary of strings-to-be-replaced as keys and replacements as values. Python与雷蒙德Hettinger在评论中实施的提示。
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.
相关:http://stackoverflow.com/questions/16516623/replacing-substrings-given-a-dictionary-of-strings-to-be-replaced-as-keys- and \ re/16516892#16516892 – jamylak
http://regular-expressions.info/reference.html'\ b'元字符与单词和空格或单词和符号之间的单词边界相匹配。例如'\ by \ b'只会自动匹配单词'y'。 – Patashu
下面的两个解决方案都是正确的,假设替换词不是替换为另一个词的词。例如'{abc < - ab,abcd < - abc}'。如果你没有这个假设,那么只有jamylak的解决方案是正确的。 – nhahtdh