为了执行这个代码,它基本上从句子中删除了一个单词,我们必须使用split.string来访问单词。如何遍历字符串数组Python
def censor(text, word):
splitstring=text.split()
for elem in range(len(splitstring)):
if splitstring[elem]==word:
splitstring[elem]='*'*len(word)
return " ".join(splitstring)**strong text**
为什么我们不能用使用“传统”的循环,仿佛穿越一个词循环的信?
for word in text:
for elem in word:
if elem==word:
word='*'*len(word) """etc"""
因为没有分裂,'在text'字分配'word'到'text'中的每个*字符*。大概你想删除整个单词,而不仅仅是那个字中出现的任何字符。但为什么问这里;为什么不试试看看会发生什么? – jonrsharpe 2014-08-29 07:45:07