Python入门:sort与sorted区别

本文探讨了Python中内置的sort方法和sorted函数的区别。sort方法用于就地排序列表,不返回排序后的列表,而sorted函数则返回一个新的排序列表。两者都支持key参数指定比较键和reverse参数来实现升序或降序排序。此外,它们都是稳定的排序算法,能保持相等元素的相对顺序。在排序过程中,sort方法会改变原列表,而sorted不会。
摘要由CSDN通过智能技术生成

sort(*, key=None, reverse=None)
#只是针对list的方法
This method sorts the list in place, using only < comparisons between items. Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state).

sort() accepts two arguments that can only be passed by keyword (keyword-only arguments):

key specifies a function of one argument that is used to extract a comparison key from each list element (for example, key=str.lower). The key corresponding to each item in the list is calculated once and then used for the entire sorting process. The default value of None means that list items are sorted directly without calculating a separate key value.</

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值