类型错误:“列表”尝试访问列表时对象不可调用
我正在尝试在有列表列表的地方运行此代码。我需要添加到内部列表,但收到错误
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
要访问列表的元素,您需要使用方括号 (
[]
),而不是括号 (()
)。而不是:
您需要使用:
而不是:
您需要使用:
For accessing the elements of a list you need to use the square brackets (
[]
) and not the parenthesis (()
).Instead of:
you need to use:
And instead of:
you need to use:
要获取列表的元素,您必须使用
list[i]
而不是list(i)
。To get elements of a list you have to use
list[i]
instead oflist(i)
.wordlists 不是一个函数,它是一个列表。您需要括号下标
wordlists is not a function, it is a list. You need the bracket subscript
当我调用与另一个被分类为列表的变量同名的函数时,我也遇到了错误。
一旦我整理好命名,错误就解决了。
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.
您正尝试在此处调用
wordlists
:尝试:
You are attempting to call
wordlists
here:Try:
尝试
wordlists[len(words)]
。()
是一个函数调用。当您执行wordlists(..)
时,Python 认为您正在调用一个名为wordlists
的函数,该函数结果是一个list
。因此出现了错误。Try
wordlists[len(words)]
.()
is a function call. When you dowordlists(..)
, python thinks that you are calling a function calledwordlists
which turns out to be alist
. Hence the error.检查保存程序的文件名。如果文件名是
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.
上面的命令对我有用
above command worked for me
即使我遇到了同样的错误,但我解决了它,我在工作中使用了很多列表,所以我只是重新启动了我的内核(这意味着如果您使用的是 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.