我尝试在 Pyscripter 中运行以下代码,但它一直返回错误“'list' object is not callable”。我通过 Python shell 运行代码,它运行得非常好。我不太明白为什么它在 Pyscripter 中不起作用。另外,我使用的是 Python 2.7。
import itertools
print list(itertools.permutations([1,2,3,4], 2))
即使在 Pyscripter 中创建一个简单的列表也会返回同样的错误。
list()
提前致谢!
最佳答案
几乎可以肯定,您已将名称列表重新绑定(bind)到列表实例:
>>> import itertools
>>> print list(itertools.permutations([1,2,3,4], 2))
[(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]
>>>
>>> list = [2,3,4]
>>> list(itertools.permutations([1,2,3,4], 2))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
不要调用列表 list
或字符串 str
等。
关于Python 'list' 对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10886003/