实用干货,python列表排序sort()和sorted()的区别

sort()是列表类型的方法,只适用于列表;sorted()是内置函数,支持各种容器类型。它们都可以排序,且用法类似,但sort()是在原地排序的,不会返回排序后的列表,而sorted()是返回新的排序列表。

1 >>> help(list.sort)
2 Help on method_descriptor:
3
4 sort(...)
5 L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
6 
7
8 >>> help(sorted)
9 Help on built-in function sorted in module builtins:
10
11 sorted(iterable, /, *, key=None, reverse=False)
12 Return a new list containing all items from the iterable in ascending order.
13
14 A custom key function can be supplied to customize the sort order, and the
15 reverse flag can be set to request the result in descending order.

本文仅简单介绍排序用法。

例如列表L:

>>> L = ['pytho
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值