python的sorted()函数能保证稳定吗?

发布于 2024-08-15 05:51:32 字数 466 浏览 6 评论 0原文

文档并不能保证这一点。还有其他地方有记录吗?

我猜它可能是稳定的,因为列表上的排序方法保证稳定(注释第9点:“从Python 2.3开始,sort()方法保证稳定”),和sorted在功能上类似。但是,我找不到任何明确的消息来源。

目的:如果两条记录中的主键相同,我需要根据主键和辅助键进行排序。如果sorted()保证稳定,我可以在辅助键上排序,然后在主键上排序并得到我需要的结果。

PS:为了避免任何混淆,我使用 stable 的意思是“如果保证不改变比较相等的元素的相对顺序,则该排序是稳定的”。

The documentation doesn't guarantee that. Is there any other place that it is documented?

I'm guessing it might be stable since the sort method on lists is guaranteed to be stable (Notes 9th point: "Starting with Python 2.3, the sort() method is guaranteed to be stable"), and sorted is functionally similar. However, I'm not able to find any definitive source that says so.

Purpose: I need to sort based on a primary key and also a secondary key in cases where the primary key is equal in both records. If sorted() is guaranteed to be stable, I can sort on the secondary key, then sort on the primary key and get the result I need.

PS: To avoid any confusion, I'm using stable in the sense of "a sort is stable if it guarantees not to change the relative order of elements that compare equal".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

云朵有点甜 2024-08-22 05:51:32

是的,手册的目的确实是保证 sorted 稳定,并且它使用与 sort 方法完全相同的算法。我确实意识到文档并没有 100% 清楚这个身份;文档补丁总是被愉快地接受!

Yes, the intention of the manual is indeed to guarantee that sorted is stable and indeed that it uses exactly the same algorithm as the sort method. I do realize that the docs aren't 100% clear about this identity; doc patches are always happily accepted!

坠似风落 2024-08-22 05:51:32

它们稳定

顺便说一句:有时您可以通过将多遍排序组合到单遍排序中来忽略排序和排序是否稳定的问题。

例如,如果您想根据对象的 last_namefirst_name 属性对对象进行排序,您可以一次性完成:

sorted_list= sorted(
    your_sequence_of_items,
    key= lambda item: (item.last_name, item.first_name))

利用元组比较。

这个答案按原样涵盖了原来的问题。对于更多与排序相关的问题,请参阅 Python 排序方法

They are stable.

By the way: you sometimes can ignore knowing whether sort and sorted are stable, by combining a multi-pass sort in a single-pass one.

For example, if you want to sort objects based on their last_name, first_name attributes, you can do it in one pass:

sorted_list= sorted(
    your_sequence_of_items,
    key= lambda item: (item.last_name, item.first_name))

taking advantage of tuple comparison.

This answer, as-is, covers the original question. For further sorting-related questions, there is the Python Sorting How-To.

最近可好 2024-08-22 05:51:32

文档同时发生了变化(相关提交)以及sorted 明确保证了这一点:

内置的sorted()函数保证稳定。如果保证不改变比较相等的元素的相对顺序,则排序是稳定的 - 这有助于多次排序(例如,按部门排序,然后按薪资等级排序)。

这部分文档已添加到 Python 2.7 和 Python 3.4(+) 中,因此该语言版本的任何兼容实现都应该具有稳定的排序

请注意,对于 CPython,list.sortPython 以来一直稳定2.3

  • Tim Peters 重写了他的 list.sort() 实现 - 这是一种“稳定排序”(相同的输入在输出中以相同的顺序出现)并且比以前更快。

我对 sorted 不是 100% 确定,现在它简单地使用 list.sort,但我还没有检查历史记录。但它很可能“总是”使用list.sort

The documentation changed in the meantime (relevant commit) and the current documentation of sorted explicitly guarantees it:

The built-in sorted() function is guaranteed to be stable. A sort is stable if it guarantees not to change the relative order of elements that compare equal — this is helpful for sorting in multiple passes (for example, sort by department, then by salary grade).

This part of the documentation was added to Python 2.7 and Python 3.4(+) so any compliant implementation of that language version should have a stable sorted.

Note that for CPython the list.sort has been stable since Python 2.3

  • Tim Peters rewrote his list.sort() implementation - this one is a "stable sort" (equal inputs appear in the same order in the output) and faster than before.

I'm not 100% sure on sorted, nowadays it simple uses list.sort, but I haven't checked the history for that. But it's likely that it "always" used list.sort.

零崎曲识 2024-08-22 05:51:32

关于排序的 Python 3.6 文档现在指出

保证排序稳定

此外,在该文档中,有一个指向稳定 Timsort 的链接,其中指出

自 2.3 版本以来,Timsort 一直是 Python 的标准排序算法

The Python 3.6 doc on sorting now states that

Sorts are guaranteed to be stable

Furthermore, in that document, there is a link to the stable Timsort, which states that

Timsort has been Python's standard sorting algorithm since version 2.3

酷遇一生 2024-08-22 05:51:32

Python 2.4 的 “新增功能”文档有效地表明了排序( )首先创建一个列表,然后对其调用 sort() ,为您提供所需的保证,尽管“官方”文档中没有。如果您真的担心的话,也可以检查来源。

The "What's New" docs for Python 2.4 effectively make the point that sorted() first creates a list, then calls sort() on it, providing you with the guarantee you need though not in the "official" docs. You could also just check the source, if you're really concerned.

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