set.difference() raising TypeError: unhashable type 'list'
set.difference() raising TypeError: unhashable type 'list'
基本上我想要实现的是在一个列表中找到唯一值,有人告诉我使用 set.difference()
最容易实现,尽管它会提高
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\ui\view.py", line 359, in _scheduled_task
await item.callback(interaction)
File "G:\Project\ReactionRoles.py", line 152, in callback
await self.view.add_reactions()
File "G:\Project\ReactionRoles.py", line 114, in add_reactions
reactions_to_push = list(set(self.reactions).difference(set(already_in)))
TypeError: unhashable type: 'list'
代码如下所示:
assert self.model is not None
assert self.reactions is not None
already_in = self.get_roles()
assert already_in is not None
reactions_to_push = list(set(self.reactions).difference(set(already_in)))
for role in reactions_to_push:
await self.original_message.add_reaction(role[1])
我以为是因为列表是空的,但是当我将代码更改为 return 一些随机的东西而不是列表中的空时它仍然做同样的事情,我不知道原因可能是什么
您是否在其中一个设置列表中存储了一个列表?
类似于:
#ok
set([1,2,3])
{1, 2, 3}
# Error [3] is not hashable
set([1,2,[3]])
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unhashable type: 'list'
基本上我想要实现的是在一个列表中找到唯一值,有人告诉我使用 set.difference()
最容易实现,尽管它会提高
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\ui\view.py", line 359, in _scheduled_task
await item.callback(interaction)
File "G:\Project\ReactionRoles.py", line 152, in callback
await self.view.add_reactions()
File "G:\Project\ReactionRoles.py", line 114, in add_reactions
reactions_to_push = list(set(self.reactions).difference(set(already_in)))
TypeError: unhashable type: 'list'
代码如下所示:
assert self.model is not None
assert self.reactions is not None
already_in = self.get_roles()
assert already_in is not None
reactions_to_push = list(set(self.reactions).difference(set(already_in)))
for role in reactions_to_push:
await self.original_message.add_reaction(role[1])
我以为是因为列表是空的,但是当我将代码更改为 return 一些随机的东西而不是列表中的空时它仍然做同样的事情,我不知道原因可能是什么
您是否在其中一个设置列表中存储了一个列表?
类似于:
#ok
set([1,2,3])
{1, 2, 3}
# Error [3] is not hashable
set([1,2,[3]])
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unhashable type: 'list'