为什么在将列表转换为集合并返回时,会收到不可分类的“列表”错误 [英] 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']]#包含子列表
>>>列表(set(total_words))
追溯(最近的最后一次调用):
文件< stdin>,第1行,< module>
TypeError:unhashable类型:'list'
Like many other questions on here, I'm attempting to remove duplicates from a list. However, when I execute code that other answers claim work I get the following error:
TypeError: unhashable type: 'list'
on the following line of code:
total_unique_words = list(set(total_words))
Does anyone know a possible solution to this problem? Is this because in most cases the original structure isn't a list?
Thanks!
解决方案
total_words
must contain sublists for this error to occur.
Consider:
>>> 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'
这篇关于为什么在将列表转换为集合并返回时,会收到不可分类的“列表”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文