Python 类型错误:“列表”对象不可调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18758186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: 'list' object is not callable
提问by Rodrigo Villalta
I can't find the problem i'm facing...
this is exactly what the error tells me:
我找不到我面临的问题......
这正是错误告诉我的:
File "C:/Users/Rodrigo Villalta/Desktop/listasparimpar.py", line 38,
in listas_par_impar
if lista2(m) > lista2 [m+1]: TypeError: 'list' object is not callable
This is the code:
这是代码:
def listas_par_impar(lista,lista2):
lista3=[]
lista4=[]
for i in lista2:
if i % 2 == 0:
lista=lista+[i]
else:
pass
for i in lista:
if i %2 != 0:
lista2=lista2+[i]
else:
pass
for i in lista:
if i%2==0:
if i not in lista3:
lista3=lista3+[i]
lista=lista[1:]
for i in lista2:
if i%2!=0:
if i not in lista4:
lista4=lista4+[i]
lista=lista[1:]
for recorrido in range(1,len(lista)):
for posicion in range(len(lista)-recorrido):
if lista(posicion) > lista [posicion+1]:
lista[posicion], lista[posicion+1] = lista[posicion+1], lista[posicion]
for r in range(1,len(lista2)):
for m in range(len(lista2)-r):
if lista2(m) > lista2 [m+1]:
lista2[m], lista2[m+1] = lista2[m+1], lista2[m]
print (lista4, lista3)
采纳答案by abarnert
In this line:
在这一行:
if lista2(m) > lista2 [m+1]:
… you've written lista2(m)
instead of lista2[m]
.
... 你写的lista2(m)
不是lista2[m]
.
This means you're trying to call lista2
like a function, with argument m
. What you wanted to do is index lista2
like a list, with index m
.
这意味着您正在尝试lista2
像函数一样使用参数进行调用m
。您想要做的是lista2
像列表一样索引,带有 index m
。
回答by NeverForgetY2K
if lista2(m) > lista2 [m+1]:
Should be:
应该:
if lista2[m] > lista2 [m+1]: