Does Python have an ordered set?(Python有有序集吗?)
问题描述
Python 有一个有序字典.那么有序集合呢?
Python has an ordered dictionary. What about an ordered set?
推荐答案
有一个有序集(可能的新链接)配方这是从 Python 2 文档 中引用的.这可以在 Py2.6 或更高版本以及 3.0 或更高版本上运行,无需任何修改.接口几乎和普通集合一样,只是初始化应该用一个列表来完成.
There is an ordered set (possible new link) recipe for this which is referred to from the Python 2 Documentation. This runs on Py2.6 or later and 3.0 or later without any modifications. The interface is almost exactly the same as a normal set, except that initialisation should be done with a list.
OrderedSet([1, 2, 3])
这是一个 MutableSet,因此 .union
的签名与 set 的签名不匹配,但由于它包含 __or__
,因此可以轻松添加类似的内容:
This is a MutableSet, so the signature for .union
doesn't match that of set, but since it includes __or__
something similar can easily be added:
@staticmethod
def union(*sets):
union = OrderedSet()
union.union(*sets)
return union
def union(self, *sets):
for set in sets:
self |= set
这篇关于Python有有序集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python有有序集吗?


- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01