【发布时间】:2013-05-21 17:06:25
【问题描述】:
我正在尝试制作一个简单的程序,该程序接受一串文本 t 和一个单词列表 l 并打印文本但带有 l 替换为对应单词中字母的多个 X。
问题:我的代码还替换了与 l 中的单词匹配的部分单词。我怎样才能让它只针对整个单词?
def censor(t, l):
for cenword in l:
number_of_X = len(cenword)
sensurliste = {cenword : ("x"*len(cenword))}
for cenword, x in sensurliste.items():
word = t.replace(cenword, x)
t = word.replace(cenword, x)
print (word)
【问题讨论】:
-
@mata 需要详细说明吗?编程新手。
-
google it :)
-
@mata 有疑问时..哈哈。
标签: python dictionary