0

我有这个我认为看起来不错的代码

def makeInverseIndex(strList):
    numStrList = list(enumerate(strList))
    n = 0
    dictionary = {}
    while (n < len(strList)):
        for word in numStrList[n][1].split():
            if word not in dictionary:
                dictionary[word] = {numStrList[n][0]}
            elif {numStrList[n][0]} not in dictionary[word]:
                dictionary[word]|={numStrList[n][0]} 
        n = n+1                     

    return dictionary

但是当我尝试运行模块时,我设法得到这个错误:

   >>> makeInverseIndex(L)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "./inverse_index_lab.py", line 21, in makeInverseIndex
    for word in numStrList[n][1].split():
NameError: global name 'StrList' is not defined

我看不出错误来自哪里。

我想输入这样的内容:

 L=['A B C', 'B C E', 'A E', 'C D A']

并将其作为输出:

D={'A':{0,2,3}, 'B':{0,1}, 'C':{0,3}, 'D':{3}, 'E':{1,2}}
4

1 回答 1

1

看起来您已经修改了模块而没有重新加载它。

回溯向您显示源文件中该行的当前内容,这可能与您加载模块时不同。

于 2013-07-05T02:57:06.453 回答