Python 怪异名称错误:全局名称';StrList';没有定义

Python 怪异名称错误:全局名称';StrList';没有定义,python,global-variables,Python,Global Variables,我有这个代码,我认为看起来不错 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:

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

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
并将其作为输出:

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

看起来您没有重新加载模块就修改了模块


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

修复问题中的缩进。这正是它返回的内容。你确定吗?当我运行它时,我得到这个
{'A':集([0,2,3]),'C':集([0,1,3]),'B':集([0,1]),'E':集([1,2]),'D':集([3])
@enginefree,Python3以不同的方式显示这些集。但问题中
C
的值似乎仍然存在wrong@gnibbler但是
C
包含在
0,1,3
索引中,而不仅仅是
0,3
@enginefree,这就是我在“问题”中所说的意思。对不起,如果我不干净的话,是它!谢谢,我是新来的
 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}}