Python中的元组和frozensets的区别 [英] Difference between tuples and frozensets in Python
问题描述
我正在使用快速Python书中的Python 3,其中作者谈到frozensets,指出由于集合是可变的,因此不可容忍,从而变得不适合作为字典键,它们的冻结对应项目被引入。除了一个明显的区别,一个元组是一个有序数据结构,而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?
推荐答案
元组
是不可变的列表
, frozensets
是不可变的集
。
tuples
are immutable lists
, frozensets
are immutable sets
.
元组
确实是对象的有序集合,但它们可以包含重复项和不可重用对象,并具有切片功能
tuples
are indeed an ordered collection of objects, but they can contain duplicates and unhashable objects, and have slice functionality
frozensets
没有编入索引,但您具有集
- 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中的元组和frozensets的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!