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'
posted @ 2020-06-16 14:49  绣幕  阅读(5516)  评论(0编辑  收藏  举报