sort()函数实例详解

​​活动地址:CSDN21天学习挑战赛

sort()函数实例详解

返回函数主目录

  Python 列表 List sort ()函数:

  sort() 函数用于对原列表进行排序,如果指定参数,则使用指定的比较函数。使用 sort() 方法进行正序排序,永久性排序,排序后不可恢复原来的排序。倒序排序,在 sort() 方法括号内传入参数 reverse=True 达到倒序排序效果。永久性,排序后不再保持原来排序方式的

语  法

list.sort(key=None, reverse=False)

参  数

key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。

reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)。

注  意

1、使用sort()方法对list排序会修改list本身,不会返回新list,

2、如果你不需要保留原来的list,可使用sort()。

3、sort()方法不能对字典dict进行排序。

4、sort()方法不能直接将 int 和 str 类型的比较排序。

5、sort()方法不能直接将 tuple 和 str 类型的比较。

6、sort()方法不能直接将 list 和 str 类型的比较。

7、sort()方法默认升序排序。

8、python2中有cmp参数,但是在python3中这一参数被取消了

返 回 值

该方法不返回任何值,但是会对列表的对象进行排序。

知识拓展

sorted ()函数:函数对所有可迭代的对象进行排序操作。

sort 与 sorted 区别:

1、sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。

2、list 的 sort()方法无返回值,sorted()函数会返回一个重新排序的列表。

3、list.sort()会修改原序列,sorted()函数不改变原有序列

实 例

代  码

#函数sort()是对列表就地排序

mlist= [4,7,7,1,9,5,2,6]

mlist.sort()

print(mlist)

运行结果

[1, 2, 4, 5, 6, 7, 7, 9]

代  码

#函数sort()会修改原序列,不返回任何值

mlist= [4,7,7,1,9,5,2,6]

print(mlist.sort())

mlist2=mlist.sort()

print(mlist2)

运行结果

None

None

代  码

#sort()排序reverse参数接受的是一个bool类型的值 (Ture or False),排序规则:reverse = True 降序, reverse = False 升序(默认)。 (Ture or False)第一个字母必须是大写的。

mlist= [4,7,7,1,9,5,2,6]

mlist.sort(reverse=True)

print(mlist)

运行结果

[9, 7, 7, 6, 5, 4, 2, 1]

代  码

#sort()排序key参数接受的是一个只有一个形参的函数,sort()将按照权值大小进行排序

mlist= [4,7,7,1,9,5,2,6]

mlist.sort()

print(mlist)

def size(a):

    mlist=10-int(a)

    return mlist

mlist.sort(key=size)

print(mlist)

运行结果

[1, 2, 4, 5, 6, 7, 7, 9]

[9, 7, 7, 6, 5, 4, 2, 1]

代  码

mlist = ["Apple", "Sydney", "Banana", "Strawberry", "pineapple", "peach"]

print(mlist)

mlist.sort()

print(mlist)

mlist.sort(reverse=True)

print(mlist)

运行结果

['Apple', 'Sydney', 'Banana', 'Strawberry', 'pineapple', 'peach']

['Apple', 'Banana', 'Strawberry', 'Sydney', 'peach', 'pineapple']

['pineapple', 'peach', 'Sydney', 'Strawberry', 'Banana', 'Apple']

代  码

mlist = ["苹果", "雪梨", "香蕉", "草莓", "菠萝", "桃子"]

print(mlist)

mlist.sort()

print(mlist)

mlist.sort(reverse=True)

print(mlist)

运行结果

['苹果', '雪梨', '香蕉', '草莓', '菠萝', '桃子']

['桃子', '苹果', '草莓', '菠萝', '雪梨', '香蕉']

['香蕉', '雪梨', '菠萝', '草莓', '苹果', '桃子']

代  码

mlist= [0,2,4,6,8,'Adversity Awake', '逆境清醒']

mlist.sort()

print(mlist)

运行结果

TypeError: unorderable types: str() < int()

# sort 方法不适合 int 和 str 类型的比较。

代  码

mlist= ['0','8','2','6','4','逆境清醒','Adversity Awake']

mlist.sort()

print(mlist)

运行结果

['0', '2', '4', '6', '8', 'Adversity Awake', '逆境清醒']

代  码

#降序输出列表

mlist= ['0','8','2','6','4','逆境清醒','Adversity Awake']

mlist.sort(reverse=True)

print(mlist)

运行结果

['逆境清醒', 'Adversity Awake', '8', '6', '4', '2', '0']

代  码

mlist= [2,9,7,0,6,9,1,8]

mlist.sort()

print(mlist)

运行结果

[0, 1, 2, 6, 7, 8, 9, 9]

代  码

#降序输出列表

mlist= [2,9,7,0,6,9,1,8]

mlist.sort(reverse=True)

print(mlist)

运行结果

[9, 9, 8, 7, 6, 2, 1, 0]

代  码

mlist=[(2,9),(7,0),(6,9),(1,8)]

mlist.sort()

print(mlist)

运行结果

[(1, 8), (2, 9), (6, 9), (7, 0)]

代  码

#降序输出列表

mlist=[(2,9),(7,0),(6,9),(1,8)]

mlist.sort(reverse=True)

print(mlist)

运行结果

[(7, 0), (6, 9), (2, 9), (1, 8)]

代  码

#通过指定列表中的元素排序来输出列表

#指定列表中的第二个元素排序来输出列表

mlist=[(2,9),(7,0),(6,9),(1,8)]

print(mlist)

def choosesort(mkey):

    return mkey[1]

mlist.sort(key=choosesort)

print(mlist)

运行结果

[(2, 9), (7, 0), (6, 9), (1, 8)]

[(7, 0), (1, 8), (2, 9), (6, 9)]

代  码

#通过指定列表中的元素排序来输出列表

#指定列表中的第二个元素排序来输出列表

#降序输出列表

mlist=[(2,9),(7,0),(6,9),(1,8)]

print(mlist)

def choosesort(mkey):

    return mkey[1]

mlist.sort(key=choosesort,reverse=True)

print(mlist)

运行结果

[(2, 9), (7, 0), (6, 9), (1, 8)]

[(2, 9), (6, 9), (1, 8), (7, 0)]

代  码

mlist= ['0','8','2','6','4','逆境清醒','Adversity Awake']

mlist.sort(reverse=True)

print(mlist)

mlist1= [(0,8,2,6,4),'逆境清醒','Adversity Awake']

mlist1.sort(reverse=True)

print(mlist1)

运行结果

['逆境清醒', 'Adversity Awake', '8', '6', '4', '2', '0']

。。。。

TypeError: unorderable types: tuple() < str()

# sort 方法不适合 tuple 和 str 类型的比较。

代  码

mlist= ['0','8','2','6','4','逆境清醒','Adversity Awake']

mlist.sort(reverse=True)

print(mlist)

mlist1= [[0,8,2,6,4],'逆境清醒','Adversity Awake']

mlist1.sort(reverse=True)

print(mlist1)

运行结果

['逆境清醒', 'Adversity Awake', '8', '6', '4', '2', '0']

。。。

TypeError: unorderable types: list() < str()

# sort 方法不适合 list 和 str 类型的比较。

代  码

#非要将列表里不同类型相比较,可以利用 key 参数将数字全部转换为 str 类型后排序

mlist= [4,7,7,1,9,"Adversity Awake",'abc', 'syz']

mlist.sort(key = str)

print(mlist)

运行结果

[1, 4, 7, 7, 9, 'Adversity Awake', 'abc', 'syz']

多关键字排序的方法

代  码

mlist=[[2,9],[7,0],[6,9],[1,8],[3,4],[1,6]]

print(mlist)

#先按第一个元素升序排序,第一个元素相同按第二个元素升序排序

mlist.sort()

print(mlist)

运行结果

[2, 9], [7, 0], [6, 9], [1, 8], [3, 4], [1, 6]]

[1, 6], [1, 8], [2, 9], [3, 4], [6, 9], [7, 0]]

代  码

#先按第一个元素升序排序,第一个元素相同则保持原来的顺序

mlist=[[2,9],[7,0],[6,9],[1,8],[3,4],[1,6]]

print(mlist)

mlist.sort(key=lambda x:x[0])

print(mlist)

运行结果

[2, 9], [7, 0], [6, 9], [1, 8], [3, 4], [1, 6]]

[1, 8], [1, 6], [2, 9], [3, 4], [6, 9], [7, 0]]

代  码

#先按第二个元素升序排序,第二个元素相同则保持原来的顺序

mlist=[[2,9],[7,0],[6,9],[1,8],[3,4],[1,6]]

print(mlist)

mlist.sort(key=lambda x:x[1])

print(mlist)

运行结果

[2, 9], [7, 0], [6, 9], [1, 8], [3, 4], [1, 6]]

[7, 0], [3, 4], [1, 6], [1, 8], [2, 9], [6, 9]]

代  码

#先按第二个元素升序排序,第二个元素相同按第一个元素降序排序

mlist=[[2,9],[7,0],[6,9],[1,8],[3,4],[1,6]]

print(mlist)

mlist.sort(key=lambda x:(x[1],-x[0]))

print(mlist)

运行结果

[2, 9], [7, 0], [6, 9], [1, 8], [3, 4], [1, 6]]

[7, 0], [3, 4], [1, 6], [1, 8], [6, 9], [2, 9]]

代  码

#先按字符串长度升序排序,长度相同按字典序升序排序

mlist= ['0','8','2','6','4','逆境清醒','Adversity Awake']

print(mlist)

mlist.sort(key=lambda x:(len(x),x))

print(mlist)

运行结果

['0', '8', '2', '6', '4', '逆境清醒', 'Adversity Awake']

['0', '2', '4', '6', '8', '逆境清醒', 'Adversity Awake']

代  码

#先按字符串长度升序排序,长度相同按字典序降序排序

mlist= ['0','8','2','6','4','逆境清醒','Adversity Awake']

print(mlist)

mlist.sort(key=lambda x:(len(x),list(map(lambda c:-ord(c),x))))

print(mlist)

运行结果

['0', '8', '2', '6', '4', '逆境清醒', 'Adversity Awake']

['8', '6', '4', '2', '0', '逆境清醒', 'Adversity Awake']

代  码

#先按字符串长度降序排序,长度相同按字典序升序排序

mlist= ['0','8','2','6','4','逆境清醒','Adversity Awake']

print(mlist)

mlist.sort(key=lambda x:(-len(x),x))

print(mlist)

运行结果

['0', '8', '2', '6', '4', '逆境清醒', 'Adversity Awake']

['Adversity Awake', '逆境清醒', '0', '2', '4', '6', '8']

返回函数主目录

     推荐阅读:

29

svg实例详解系列(一)
28

查看jdk安装路径,在windows上实现多个java jdk的共存解决办法,安装java19后终端乱码的解决

27

bba02a1c4617422c9fbccbf5325850d9.png

原创唯美浪漫情人节表白专辑,(复制就可用)(html5,css3,svg)更好的向你所喜欢的人表达内心的感受。

26

fea225cb9ec14b60b2d1b797dd8278a2.png

2023年春节祝福第二弹——送你一只守护兔,让它温暖每一个你【html5 css3】画会动的小兔子
25

1f53fb9c6e8b4482813326affe6a82ff.png

2023春节祝福系列第一弹(放飞祈福孔明灯,祝福大家身体健康)(附完整源代码及资源免费下载)
24

6176c4061c72430eb100750af6fc4d0e.png

HTML+CSS+svg绘制精美彩色闪灯圣诞树,HTML+CSS+Js实时新年时间倒数倒计时(附源代码)

23

17b403c4307c4141b8544d02f95ea06c.png

​草莓熊python绘图(春节版,圣诞倒数雪花版)附源代码

22

5d409c8f397a45c986ca2af7b7e725c9.png

【程序人生】卡塔尔世界杯元素python海龟绘图(附源代码),世界杯主题前端特效5个(附源码)

21

0a4256d5e96d4624bdca36433237080b.png

​​

python爱心源代码集锦
20

4d9032c9cdf54f5f9193e45e4532898c.png

​​

巴斯光年python turtle绘图__附源代码
19

074cd3c255224c5aa21ff18fdc25053c.png

​​​

Three.js实例详解___旋转的精灵女孩(附完整代码和资源)
18

daecd7067e7c45abb875fc7a1a469f23.png

​​​​

​草莓熊python turtle绘图(玫瑰花版)附源代码

17

fe88b78e78694570bf2d850ce83b1f69.png

​​​​

立体多层玫瑰绘图源码__玫瑰花python 绘图源码集锦

16

c5feeb25880d49c085b808bf4e041c86.png

​​​​

皮卡丘python turtle海龟绘图(电力球版)附源代码

15

38266b5036414624875447abd5311e4d.png

​​​​

【CSDN云IDE】个人使用体验和建议(含超详细操作教程)(python、webGL方向)

14

03ed644f9b1d411ba41c59e0a5bdcc61.png

​​​​

草莓熊python turtle绘图(风车版)附源代码

13

09e08f86f127431cbfdfe395aa2f8bc9.png

​​​​

用代码过中秋,python海龟月饼你要不要尝一口?

12

40e8b4631e2b486bab2a4ebb5bc9f410.png

​​​​

《 Python List 列表全实例详解系列》__系列总目录

11

938bc5a8bb454a41bfe0d4185da845dc.jpeg

​​​​

用代码写出浪漫__合集(python、matplotlib、Matlab、java绘制爱心、玫瑰花、前端特效玫瑰、爱心)

10

0f09e73712d149ff90f0048a096596c6.png

​​​​

Python函数方法实例详解全集(更新中...)

9

93d65dbd09604c4a8ed2c01df0eebc38.png

​​​​

matplotlib 自带绘图样式效果展示速查(28种,全)

8

aa17177aec9b4e5eb19b5d9675302de8.png

​​​​

手机屏幕坏了____怎么把里面的资料导出(18种方法)

7

1750390dd9da4b39938a23ab447c6fb6.jpeg

​​​​

2023年2月多家权威机构____编程语言排行榜__薪酬状况

6

dc8796ddccbf4aec98ac5d3e09001348.jpeg

​​​​

Python中Print()函数的用法___实例详解(全,例多)

5

1ab685d264ed4ae5b510dc7fbd0d1e55.jpeg

​​​​

色彩颜色对照表(300种颜色)(16进制、RGB、CMYK、HSV、中英文名)

4

80007dbf51944725bf9cf4cfc75c5a13.png

​​​​

Node.js (v19.1.0npm 8.19.3) vue.js安装配置教程(超详细)

3

c6374d75c29942f2aa577ce9c5c2e12b.png

​​​​

Tomcat 启动闪退问题解决集(八大类详细)

2

5218ac5338014f389c21bdf1bfa1c599.png

​​​​

Tomcat端口配置(详细)

1

fffa2098008b4dc68c00a172f67c538d.png

​​​​

Tomcat11、tomcat10 安装配置(Windows环境)(详细图文)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逆境清醒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值