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 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01