Difference between tuples and frozensets in Python(Python中元组和冻结集之间的区别)
问题描述
我正在使用 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中元组和冻结集之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python中元组和冻结集之间的区别


基础教程推荐
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 求两个直方图的卷积 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 包装空间模型 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01