为什么Python的标准库中没有排序容器?
是否存在阻止将排序容器添加到 Python 的 Python 设计决策 (PEP)?
(OrderedDict
不是排序容器,因为它是按插入顺序排序的。)
Is there a Python design decision (PEP) that precludes a sorted container from being added to Python?
(OrderedDict
is not a sorted container since it is ordered by insertion order.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
还有一个 python sortedcontainers 模块,它实现排序列表、字典和集合类型。它与 blist 非常相似,但在纯 Python 中实现,并且在大多数情况下更快。
它还具有其他软件包不常见的功能:
披露:我是sortedcontainers 模块的作者。
There's also a python sortedcontainers module that implements sorted list, dict, and set types. It's very similar to blist but implemented in pure-Python and in most cases faster.
It also has functionality uncommon to other packages:
Disclosure: I am the author of the sortedcontainers module.
这是 Guido 有意识的设计决定(他甚至对于添加
collections
模块有些犹豫)。他的目标是在为应用程序选择数据类型时保留“一种明显的方法”。基本概念是,如果用户足够成熟,认识到内置类型不是解决他们问题的正确解决方案,那么他们也可以找到合适的第三方库。
鉴于 list+sorting、list+heapq 和 list+bisect 涵盖了许多用例,否则这些用例将依赖于固有排序的数据结构,并且存在像 blist 这样的包,因此没有巨大的动力来增加这个空间的复杂性标准库。
在某些方面,这类似于标准库中没有多维数组的事实,而是将这项任务交给了 NumPy 人员。
It's a conscious design decision on Guido's part (he was even somewhat reluctant regarding the addition of the
collections
module). His goal is to preserve "one obvious way to do it" when it comes to the selection of data types for applications.The basic concept is that if a user is sophisticated enough to realise that the builtin types aren't the right solution for their problem, then they're also up to the task of finding an appropriate third party library.
Given that list+sorting, list+heapq and list+bisect cover many of the use cases that would otherwise rely on inherently sorted data structures, and packages like blist exist, there isn't a huge drive to add more complexity in this space to the standard library.
In some ways, it is similar to the fact that there's no multi-dimensional array in the standard library, instead ceding that task to the NumPy folks.
还有 blist 模块,其中包含 sortedset 数据类型:
There is also the blist module that contains a sortedset data type:
不完全是“排序容器”,但您可能对标准库的 bisect 模块感兴趣,它“提供对按排序顺序维护列表的支持,而无需在每次插入后对列表进行排序”。
Not exactly a "sorted container", but you might be interested in the standard library's bisect module, which "provides support for maintaining a list in sorted order without having to sort the list after each insertion".
标准库中有一个heapq,它不是完全排序的,但有点排序。还有一个 blist 包,但它不在标准库中。
There is a
heapq
in the standard library, it is not exactly sorted, but kind of. There is also a blist package, but it is not in the standard library.对于排序集的特定情况,我发现
Flag
很有用,例如:这可以像集合一样使用,
|
是联合,&
是交集,~
是倒置,例如:Which prints:
集合按声明顺序自动排序(关于集合的讨论点)并且通用集合是封闭的(其中我喜欢)。
For the specific case of a sorted set I find
Flag
useful, e.g.:This can be used like a set,
|
is union,&
is intersection, and~
is inversion, e.g.:Which prints:
The sets are automatically sorted in declaration order (point of discussion w.r.t. sets) and the universal set is closed (which I like).
Python 列表是有序的。如果你对它们进行排序,它们就会保持原样。在 Python 2.7 中,添加了
OrderedDict
类型维护一个显式排序的字典。Python也有集合(一个集合,其中的成员必须是唯一的),但是根据定义,它们是无序的。对集合进行排序只会返回一个
列表
。Python lists are ordered. If you sort them, they stay that way. In Python 2.7 an
OrderedDict
type was added to maintain an explicitly ordereded dictionary.Python also has sets (a collection in which the members must be unique), but by definition they are unordered. Sorting a set just returns a
list
.