TypeError:“ list”对象在尝试访问列表时不可调用


问题内容

我试图在有列表列表的地方运行此代码。我需要添加到内部列表中,但出现错误

TypeError: 'list' object is not callable.

谁能告诉我我在做什么错。

def createlists():
    global maxchar
    global minchar
    global worddict
    global wordlists

    for i in range(minchar, maxchar + 1):
        wordlists.insert(i, list())
    #add data to list now
    for words in worddict.keys():
        print words
        print  wordlists(len(words)) # <--- Error here.
        (wordlists(len(words))).append(words)  # <-- Error here too
        print "adding word " + words + " at " + str(wordlists(len(words)))
    print wordlists(5)

问题答案:

要访问列表的元素,您需要使用方括号([])而不是括号(())。

代替:

print  wordlists(len(words))

您需要使用:

print worldlists[len(words)]

而不是:

(wordlists(len(words))).append(words)

您需要使用:

worldlists[len(words)].append(words)