我试图用它替换字[NOUN]
的字符串。我无能为力!如何插入和替换另一个列表中的单词列表或python中的字符串
这里是我下面的代码 - 它会返回大量的错误 - 变量故事是一个字符串,listOfNouns是一个列表 - 所以我尝试通过拆分它:
def replacement(story, listOfNouns):
length = len(story1)
story1 = story.split()
for c in range(0,len(story1)):
if c in listOfNouns:
story1[c]= 'NOUN'
story = ''.join(story)
return story
这里的字符串转换成一个列表该错误消息我得到下面时,我打电话与
replacement("Let's play marbles", ['marbles'])
上述功能:
Traceback (most recent call last):
File "<pyshell#189>", line 1, in <module>
replacement("Let's play marbels", ['marbels'])
File "C:/ProblemSet4/exam.py", line 3, in replacement
length = len(story1)
UnboundLocalError: local variable 'story1' referenced before assignment
我怎样才能从另一个列表中其他元素取代新story1列表?
如何修改元组并返回新的字符串 - 应该说:
Let's play [NOUN]
???
任何人都可以请帮忙吗?我迷路了,我一直在尝试使用Python/Java中的所有知识来解决这个问题!
,什么是之后的下一步是什么? – 2013-03-24 17:31:44
现在它只是返回“让我们玩大理石”而不是“让我们玩''NOUN'] – 2013-03-24 17:33:21