问题描述
我正在使用 The Quick Python Book 学习 Python 3,作者在其中谈到了frozensets,并指出由于 set 是可变的,因此不可散列,因此不适合作为字典键,因此引入了它们的 freeze 对应项.除了元组是有序数据结构而frozenset(或更一般地说是集合)是无序的明显区别之外,元组和frozenset之间还有其他区别吗?
I'm learning Python 3 using The Quick Python Book, where the author talks about frozensets, stating that since sets are mutable and hence unhashable, thereby becoming unfit for being dictionary keys, their frozen counterparts were introduced. Other than the obvious difference that a tuple is an ordered data structure while frozenset, or more generally a set, is unordered, are there any other differences between a tuple and a frozenset?
推荐答案
tuples
是不可变的lists
,frozensets
是不可变的sets
.
tuples
are immutable lists
, frozensets
are immutable sets
.
tuples
确实是对象的有序集合,但它们可以包含重复和不可散列的对象,并且具有切片功能
tuples
are indeed an ordered collection of objects, but they can contain duplicates and unhashable objects, and have slice functionality
frozensets
没有被索引,但你有 sets
的功能 - O(1) 元素查找,以及联合和交集等功能.它们也不能包含重复项,就像它们的可变对应项一样.
frozensets
aren't indexed, but you have the functionality of sets
- O(1) element lookups, and functionality such as unions and intersections. They also can't contain duplicates, like their mutable counterparts.
这篇关于Python中元组和冻结集之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!