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

2024-04-25 06:32:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试运行这个代码,其中我有一个列表列表。我需要添加到内部列表,但我得到了错误

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)

Tags: 代码in列表forlenhereerrorglobal
3条回答

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

而不是:

print  wordlists(len(words))

您需要使用:

print worldlists[len(words)]

而不是:

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

您需要使用:

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

单词表不是一个函数,它是一个列表。你需要括号下标

print  wordlists[len(words)]

要获取列表元素,必须使用list[i],而不是list(i)

相关问题 更多 >