类型错误:“列表”尝试访问列表时对象不可调用

发布于 2024-11-02 17:20:59 字数 648 浏览 1 评论 0原文

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

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

尘曦 2024-11-09 17:20:59

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

而不是:

print  wordlists(len(words))

您需要使用:

print worldlists[len(words)]

而不是:

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

您需要使用:

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

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

Instead of:

print  wordlists(len(words))

you need to use:

print worldlists[len(words)]

And instead of:

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

you need to use:

worldlists[len(words)].append(words)
终止放荡 2024-11-09 17:20:59

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

To get elements of a list you have to use list[i] instead of list(i).

与他有关 2024-11-09 17:20:59

wordlists 不是一个函数,它是一个列表。您需要括号下标

print  wordlists[len(words)]

wordlists is not a function, it is a list. You need the bracket subscript

print  wordlists[len(words)]
や三分注定 2024-11-09 17:20:59

当我调用与另一个被分类为列表的变量同名的函数时,我也遇到了错误。

一旦我整理好命名,错误就解决了。

I also got the error when I called a function that had the same name as another variable that was classified as a list.

Once I sorted out the naming the error was resolved.

半边脸i 2024-11-09 17:20:59

您正尝试在此处调用 wordlists

print  wordlists(len(words)) <--- Error here.

尝试:

print wordlists[len(words)]

You are attempting to call wordlists here:

print  wordlists(len(words)) <--- Error here.

Try:

print wordlists[len(words)]
落花浅忆 2024-11-09 17:20:59

尝试wordlists[len(words)]() 是一个函数调用。当您执行 wordlists(..) 时,Python 认为您正在调用一个名为 wordlists 的函数,该函数结果是一个 list。因此出现了错误。

Try wordlists[len(words)]. () is a function call. When you do wordlists(..), python thinks that you are calling a function called wordlists which turns out to be a list. Hence the error.

情绪 2024-11-09 17:20:59

检查保存程序的文件名。如果文件名是wordlists
那么你会得到一个错误。您的文件名不应与您在程序中使用的任何方法{函数}相同。

Check your file name in which you have saved your program. If the file name is wordlists
then you will get an error. Your filename should not be same as any of methods{functions} that you use in your program.

冬天的雪花 2024-11-09 17:20:59
del list

上面的命令对我有用

del list

above command worked for me

花开浅夏 2024-11-09 17:20:59

即使我遇到了同样的错误,但我解决了它,我在工作中使用了很多列表,所以我只是重新启动了我的内核(这意味着如果您使用的是 Jupyter 或 Google Colab 等笔记本,您可以重新启动并再次运行所有单元,通过这样做,您的问题将得到解决,并且错误将消失

谢谢

Even I got the same error, but I solved it, I had used many list in my work so I just restarted my kernel (meaning if you are using a notebook such as Jupyter or Google Colab you can just restart and again run all the cells, by doing this your problem will be solved and the error vanishes.

Thank you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文