python基础中遇到的问题(TypeError: unhashable type: 'list')
d20220330
#false
>>> l=[{i:i+1} for i in [1,2,3]]
>>> l
[{1: 2}, {2: 3}, {3: 4}]
>>> d={**i for i in l}
File "<stdin>", line 1
SyntaxError: dict unpacking cannot be used in dict comprehension
#true
>>> d = {}
>>> for i in l:
... d.update(i)
...
>>> d
{1: 2, 2: 3, 3: 4}
day20200616
不能向集合中添加列表,字典,集合,会报(TypeError: unhashable type: 'list')错误。
可以添加元组。
s01 = set()
s01.add([1,2,3])
print(s01)
TypeError: unhashable type: 'list'