为什么Python的标准库中没有排序容器?

发布于 2024-11-06 05:51:01 字数 106 浏览 1 评论 0原文

是否存在阻止将排序容器添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

简美 2024-11-13 05:51:01

还有一个 python sortedcontainers 模块,它实现排序列表、字典和集合类型。它与 blist 非常相似,但在纯 Python 中实现,并且在大多数情况下更快

>>> from sortedcontainers import SortedSet
>>> ss = SortedSet([3, 7, 2, 2])
>>> ss
SortedSet([2, 3, 7])

它还具有其他软件包不常见的功能:

>>> from sortedcontainers import SortedDict
>>> sd = SortedDict((num, num) for num in range(100000))
>>> sd.iloc[-5] # Lookup the fifth-to-last key.
99995

披露:我是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.

>>> from sortedcontainers import SortedSet
>>> ss = SortedSet([3, 7, 2, 2])
>>> ss
SortedSet([2, 3, 7])

It also has functionality uncommon to other packages:

>>> from sortedcontainers import SortedDict
>>> sd = SortedDict((num, num) for num in range(100000))
>>> sd.iloc[-5] # Lookup the fifth-to-last key.
99995

Disclosure: I am the author of the sortedcontainers module.

情感失落者 2024-11-13 05:51:01

这是 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.

泪是无色的血 2024-11-13 05:51:01

还有 blist 模块,其中包含 sortedset 数据类型:

sortedset(iterable=(), key=None)

>>> from blist import sortedset
>>> my_set = sortedset([3,7,2,2])
sortedset([2, 3, 7]

There is also the blist module that contains a sortedset data type:

sortedset(iterable=(), key=None)

>>> from blist import sortedset
>>> my_set = sortedset([3,7,2,2])
sortedset([2, 3, 7]
云雾 2024-11-13 05:51:01

不完全是“排序容器”,但您可能对标准库的 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".

怪我闹别瞎闹 2024-11-13 05:51:01

标准库中有一个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.

何以笙箫默 2024-11-13 05:51:01

对于排序集的特定情况,我发现Flag很有用,例如:

from enum import Flag
Color = Flag('Color', 'RED GREEN BLUE')

这可以像集合一样使用,|是联合,& 是交集,~ 是倒置,例如:

set1 = Color.RED | Color.GREEN
set2 = Color.BLUE
union = set1 | set2
intersection = set1 & set2
inversion = ~set1
empty = Color(0)
universal = ~empty
print(universal)

Which prints:

Color.RED|GREEN|BLUE

集合按声明顺序自动排序(关于集合的讨论点)并且通用集合是封闭的(其中我喜欢)。

For the specific case of a sorted set I find Flag useful, e.g.:

from enum import Flag
Color = Flag('Color', 'RED GREEN BLUE')

This can be used like a set, | is union, & is intersection, and ~ is inversion, e.g.:

set1 = Color.RED | Color.GREEN
set2 = Color.BLUE
union = set1 | set2
intersection = set1 & set2
inversion = ~set1
empty = Color(0)
universal = ~empty
print(universal)

Which prints:

Color.RED|GREEN|BLUE

The sets are automatically sorted in declaration order (point of discussion w.r.t. sets) and the universal set is closed (which I like).

£冰雨忧蓝° 2024-11-13 05:51:01

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文