python的sorted()函数能保证稳定吗?
文档并不能保证这一点。还有其他地方有记录吗?
我猜它可能是稳定的,因为列表上的排序方法保证稳定(注释第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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,手册的目的确实是保证
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 thesort
method. I do realize that the docs aren't 100% clear about this identity; doc patches are always happily accepted!它们稳定。
顺便说一句:有时您可以通过将多遍排序组合到单遍排序中来忽略排序和排序是否稳定的问题。
例如,如果您想根据对象的
last_name
、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: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.
文档同时发生了变化(相关提交)以及
sorted
明确保证了这一点:这部分文档已添加到 Python 2.7 和 Python 3.4(+) 中,因此该语言版本的任何兼容实现都应该具有稳定的
排序
。请注意,对于 CPython,
list.sort
自 Python 以来一直稳定2.3我对
sorted
不是 100% 确定,现在它简单地使用list.sort
,但我还没有检查历史记录。但它很可能“总是”使用list.sort
。The documentation changed in the meantime (relevant commit) and the current documentation of
sorted
explicitly guarantees it: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.3I'm not 100% sure on
sorted
, nowadays it simple useslist.sort
, but I haven't checked the history for that. But it's likely that it "always" usedlist.sort
.关于排序的 Python 3.6 文档现在指出
此外,在该文档中,有一个指向稳定 Timsort 的链接,其中指出
The Python 3.6 doc on sorting now states that
Furthermore, in that document, there is a link to the stable Timsort, which states that
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.