为什么在将列表转换为集合并返回时出现不可散列类型 'list' 错误

Why do I get an unhashable type 'list' error when converting a list to a set and back

与此处的许多其他问题一样,我正在尝试从列表中删除重复项。但是,当我执行其他答案声称有效的代码时,出现以下错误:

TypeError: unhashable type: 'list'

在以下代码行中:

total_unique_words = list(set(total_words))

有谁知道这个问题的可能解决方案吗?这是因为在大多数情况下原始结构不是列表吗?

谢谢!

total_words 必须包含子列表才会发生此错误。

考虑:

>>> total_words = ['red', 'red', 'blue']
>>> list(set(total_words))
['blue', 'red']
>>> total_words = ['red', ['red', 'blue']] # contains a sublist
>>> list(set(total_words))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'