在对可迭代对象进行排序时,Python如何打领带 [英] How does Python break tie when sorting an iterable

查看:90
本文介绍了在对可迭代对象进行排序时,Python如何打领带的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Python如何根据某种指定的键来决定要打成平局的两个项目之间的顺序.例如,给定:l = [[1, 2, 3], [1, 2], [1], [2, 3, 4], [1, 2, 4, 5]],Python如何按这种顺序对[1, 2, 3][2, 3, 4]进行排序:

I wonder how Python decides the order between two items that would be in a tie based on some specified key of a sort. For example, given: l = [[1, 2, 3], [1, 2], [1], [2, 3, 4], [1, 2, 4, 5]], how does Python order [1, 2, 3] and [2, 3, 4] in this sort:

sorted(l, key=lambda i: len(i), reverse=True)

它会在领带中保持项目之间的原始(相对)顺序吗?还是随机订购它们?

Does it keep the original (relative) order between items in a tie? Or does it order them randomly?

推荐答案

也在 wiki :

从Python 2.2开始,保证排序是稳定的.那 表示当多个记录具有相同的键时,它们的原始 订单被保留.

Starting with Python 2.2, sorts are guaranteed to be stable. That means that when multiple records have the same key, their original order is preserved.

这篇关于在对可迭代对象进行排序时,Python如何打领带的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆