gpt4 book ai didi

python - "TypeError: ' 设置 ' object is not callable"

转载 作者:行者123 更新时间:2023-11-28 19:34:01 24 4
gpt4 key购买 nike

我有两个 iPython notebook 安装。一个在 AWS 微型实例上,第二个在我的 Macbook (OS X Yosemite) 上使用 Anaconda。我遇到了他们处理以下代码的方式不同:

my_list = [1, 2, 3, 0, 5, 10, 11, 1, 5]
your_list = [1, 2, 3, 0, 12, 13]
my_set = set(my_list)
your_set = set(your_list)
print my_set
print len(my_set)
print len(my_list)

在 iPython-AWS 上,我的输出是:

set([0, 1, 2, 3, 5, 10, 11])
7
9

在 iPython-Macbook 上,我的输出是:

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-29-cd060f1b0bde> in <module>()
1 my_list = [1, 2, 3, 0, 5, 10, 11, 1, 5]
2 your_list = [1, 2, 3, 0, 12, 13]
----> 3 my_set = set(my_list)
4 your_set = set(your_list)
5 print my_set

TypeError: 'set' object is not callable

此外,这些是安装详细信息(如果相关):1. AWS 微实例上的 iPython:http://i.stack.imgur.com/qYrq8.png

  1. Macbook 上的 iPython 笔记本 - http://i.stack.imgur.com/Q6Id5.png

我似乎找不到造成这种差异的原因,尽管我确实在 Stackoverflow 上遇到过很多关于“TypeError: 'set' object is not callable”问题的线程。如果能帮助我理解为什么会这样,我将不胜感激,如果我能做些什么来确保我的代码在两个安装上运行。

最佳答案

此错误表明您可能定义了一个变量名为 set 的集合,如果您这样做,将会覆盖 built-in 函数 设置 .

发生此问题的示例 -

>>> set = set([1,2,3,4,5])
>>> my_set = set([2,3,4,5,6])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object is not callable

关于python - "TypeError: ' 设置 ' object is not callable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31202534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com