TypeError: 'list' 对象在尝试访问列表时不可调用 [英] TypeError: 'list' object is not callable while trying to access a list

查看:62
本文介绍了TypeError: 'list' 对象在尝试访问列表时不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to run this code where I have a list of lists. I need to add to inner lists, but I get the error

TypeError: 'list' object is not callable.

谁能告诉我我在这里做错了什么.

Can anyone tell me what am I doing wrong here.

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)

推荐答案

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

For accessing the elements of a list you need to use the square brackets ([]) and not the parenthesis (()).

代替:

print  wordlists(len(words))

您需要使用:

print worldlists[len(words)]

而不是:

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

您需要使用:

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

这篇关于TypeError: 'list' 对象在尝试访问列表时不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆