我試圖用它替換字[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