Python sort使用方法

本文详细介绍了Python中sort()和sorted()函数的区别,包括它们的适用场景、参数说明和实例演示。重点讲解了如何对元组、字典和多维列表进行排序,以及使用key和reverse参数的高级用法。
摘要由CSDN通过智能技术生成

sort()是列表的一个排序方法,直接修改原列表,没有返回值。
sorted()使用范围更广,不局限于列表,能接受所有迭代器,返回排好序的新列表。

使用方法:

list.sort(*, key=None, reverse=None)
sorted(iterable[, key][, reverse])

key 是带一个参数的函数,表示用什么方式排序。
reverse 表示排序结果是否反转

例子:

a = [2, 4, 7, 1, 8, 5]
sorted(a)
[1, 2, 4, 5, 7, 8] #返回一个排好序的列表
a.sort() #sort()没有返回值,但是a已经被改变了
a
[1, 2, 4, 5, 7, 8]

对元祖排序

b = (2, 4, 7, 1, 8, 5) #定义一个元祖类型
sorted(b) #可以用sorted排序
[1, 2, 4, 5, 7, 8]
b.sort() #但是sort不行
Traceback (most recent call last):
File “”, line 1, in
AttributeError: ‘tuple’ object has no attribute ‘sort’

对字典排序

c = {1: ‘D’, 2: ‘B’, 3: ‘B’, 4: ‘E’, 5: ‘A’}
sorted© #对键进行排序
[1, 2, 3, 4, 5]
sorted(c.values()) #对值进行排序
[‘A’, ‘B’, ‘B’, ‘D’, ‘E’]
sort()是列表的一个排序方法,直接修改原列表,没有返回值。
sorted()使用范围更广,不局限于列表,能接受所有迭代器,返回排好序的新列表。

使用方法:

list.sort(*, key=None, reverse=None)
sorted(iterable[, key][, reverse])

key 是带一个参数的函数,表示用什么方式排序。
reverse 表示排序结果是否反转

例子:

a = [2, 4, 7, 1, 8, 5]
sorted(a)
[1, 2, 4, 5, 7, 8] #返回一个排好序的列表
a.sort() #sort()没有返回值,但是a已经被改变了
a
[1, 2, 4, 5, 7, 8]

#对元祖排序
b = (2, 4, 7, 1, 8, 5) #定义一个元祖类型
sorted(b) #可以用sorted排序
[1, 2, 4, 5, 7, 8]
b.sort() #但是sort不行
Traceback (most recent call last):
File “”, line 1, in
AttributeError: ‘tuple’ object has no attribute ‘sort’

#对字典排序
c = {1: ‘D’, 2: ‘B’, 3: ‘B’, 4: ‘E’, 5: ‘A’}
sorted© #对键进行排序
[1, 2, 3, 4, 5]
sorted(c.values()) #对值进行排序
[‘A’, ‘B’, ‘B’, ‘D’, ‘E’]

#对字符串排序
s = ‘cts’
sorted(s)
[‘c’, ‘s’, ‘t’] # 如果直接用sorted那么会将排序结果放入列表中,并且以字符的形式。
“”.join(sorted(s)) #相当于将字符连接起来转成字符串
‘cst’

#多维列表排序
d = [[5,2],[3,4],[9,7],[2,6]]
d.sort()
d
[[2, 6], [3, 4], [5, 2], [9, 7]]
d = [[5,2],[3,4],[9,7],[2,6]]
sorted(d)
[[2, 6], [3, 4], [5, 2], [9, 7]] #都默认以第一维排序
d = [[5,2],[3,4],[9,7],[2,6]]
sorted(d, key = lambda x:x[1]) #如果要指定第二维排序:
[[5, 2], [3, 4], [2, 6], [9, 7]]

#多个参数排序:
from operator import itemgetter #要用到itemgetter函数
e = [(‘hi’, ‘A’, 27),(‘hello’, ‘C’, 90),(‘yo’, ‘D’, 8)]
sorted(e, key=itemgetter(2)) #以第三列排序
[(‘yo’, ‘D’, 8), (‘hi’, ‘A’, 27), (‘hello’, ‘C’, 90)]
sorted(e, key=itemgetter(1,2)) #结合第二和第三列拍,先排第二列
[(‘hi’, ‘A’, 27), (‘hello’, ‘C’, 90), (‘yo’, ‘D’, 8)]

对字符串排序

s = ‘cts’
sorted(s)
[‘c’, ‘s’, ‘t’] # 如果直接用sorted那么会将排序结果放入列表中,并且以字符的形式。
“”.join(sorted(s)) #相当于将字符连接起来转成字符串
‘cst’

多维列表排序

d = [[5,2],[3,4],[9,7],[2,6]]
d.sort()
d
[[2, 6], [3, 4], [5, 2], [9, 7]]
d = [[5,2],[3,4],[9,7],[2,6]]
sorted(d)
[[2, 6], [3, 4], [5, 2], [9, 7]] #都默认以第一维排序
d = [[5,2],[3,4],[9,7],[2,6]]
sorted(d, key = lambda x:x[1]) #如果要指定第二维排序:
[[5, 2], [3, 4], [2, 6], [9, 7]]

多个参数排序:

from operator import itemgetter #要用到itemgetter函数
e = [(‘hi’, ‘A’, 27),(‘hello’, ‘C’, 90),(‘yo’, ‘D’, 8)]
sorted(e, key=itemgetter(2)) #以第三列排序
[(‘yo’, ‘D’, 8), (‘hi’, ‘A’, 27), (‘hello’, ‘C’, 90)]
sorted(e, key=itemgetter(1,2)) #结合第二和第三列拍,先排第二列
[(‘hi’, ‘A’, 27), (‘hello’, ‘C’, 90), (‘yo’, ‘D’, 8)]

how to sort list in Python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值