gpt4 book ai didi

python - AttributeError: 'NoneType' 对象没有属性 'add'

转载 作者:行者123 更新时间:2023-12-05 01:21:15 25 4
gpt4 key购买 nike

出现如下错误

AttributeError: 'NoneType' object has no attribute 'add'

当我尝试这个的时候。

not_yet_bought_set = set()
.
.
.
for value in set_dict.itervalues():
for item in value:
not_yet_bought_set = not_yet_bought_set.add(item)

我不明白为什么会出现这个错误,是因为我总是把 not_yet_bought_set 设为新的吗?我做这个,因为当我只做的时候

not_yet_bought_set.add(item)

不会有所有值的所有项目。我不知道为什么。

值是集合和

not_yet_bought_set.union(value)

也会产生这个错误

感谢您的帮助。

最佳答案

not_yet_bought_set.add(item)

这将返回 None 并且您将其分配给 not_yet_bought_set。所以,not_yet_bought_set 现在变成了 None。下一次

not_yet_bought_set = not_yet_bought_set.add(item)

被执行,add 将在 None 上被调用。这就是它失败的原因。

要解决此问题,只需执行此操作。不要将其分配给任何东西。

 not_yet_bought_set.add(item)

关于python - AttributeError: 'NoneType' 对象没有属性 'add',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19853872/

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