0

我有一个递归函数,如下所示,它更改了全局变量,但似乎我无法将更改后的全局变量传递给递归函数,因为错误异常说:

File "forlooptest.py", line 18, in get_final_list
        wordList.remove(w)
        ValueError: list.remove(x): x not in list

我认为list.remove(x): x not in list是因为全局变量没有改变。

谁能告诉我我该怎么做?谢谢!

import wordNet
import copy
from __builtin__ import any as b_any

final_list = []
wrec = 'start'

def get_final_list(wordList, w):

'''functions for get the sematically linked words list
  traverse and check every word then append to final_list'''

final_list.append(w)
hyplst = wordNet.gethypSet(w)
hyperlst = wordNet.gethyperSet(w)
wordList.remove(w)
for word in wordList:
            if (word in hyplst
                or word in hyperlst
                or b_any(word in x for x in hyplst)
                or b_any(word in x for x in hyperlst)):
                global wrec
                wrec = word
                break

get_final_list(wordList, wrec)

return final_list
4

1 回答 1

0

您的问题与全球无关。如果没有符合您的条件的单词,您只需执行相同的递归调用,从而再次删除 wrec。

于 2013-05-25T11:45:28.473 回答