【发布时间】:2022-01-23 13:19:54
【问题描述】:
我了解集合键是不可变的,因此列表等数据类型不符合成为集合中的键的条件。在下面显示的示例中,如果元组中存在字典,为什么元组不能用作键?有人可以帮我解释一下吗?
x= (1,{'a':1})
y= (1,2)
print(type(x),type(y))
# piece of code which is not giving me an error is below
set1 = {x,'INDIA'}
# set 2 can be created in similar manner without an error
set2 = {y,'INDIA'}
set2
【问题讨论】:
-
集合中的所有项目都必须是可散列的,而字典不是(它们的元组也不是)。
标签: python dictionary set tuples mutable