The frozenset type is immutable and hashable — its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set.
The frozenset type is immutable and hashable — its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set.
使用
frozenset
作为dict
键是否安全?是的。根据文档,Frozenset是散列的,因为它是不可变的。这意味着它可以用作dict的密钥,因为密钥的先决条件是它是散列的。
从FrozenSet docs
冗余地,从Dictionary docs:
为了澄清,一个集合(根据定义),无论是否冻结,都不能保持顺序。它们以不考虑顺序和删除重复元素的方式存储在内部,因此以不同顺序构建的两个集合将是字典中的等价键——它们是相同的。
同样地
来自the official docs
(重点是我的)
不,^{} hashing algorithm 不依赖于元素的顺序,只依赖于元素本身。具有相同元素的两个FS是相等的并且具有相等的散列,因此满足“dict identity”的两个条件,换句话说,它们是相同的dict key:
相关问题 更多 >
编程相关推荐