更改并传递一个全局变量来运行Python

时间:2013-05-25 11:35:49

标签: python function variables global nltk

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

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

1 个答案:

答案 0 :(得分:0)

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