sorted ()函数实例详解

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

sorted ()函数实例详解

返回函数主目录

  sorted ()函数:

  函数对所有可迭代的对象进行排序操作。即对序列(列表、元组、字典、集合、还包括字符串)进行排序。

语  法

sorted(iterable, key=None, reverse=False)

参  数

iterable -- 可迭代对象。

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

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

注  意

1、sorted()函数默认升序排序,通过手动将其 reverse 参数值改为 True,可实现降序排序。

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

3、sorted()函数会返回一个排序列表,不改变原有序列

4、在调用 sorted() 函数时,还可传入一个 key 参数,它可以接受一个函数,该函数的功能是指定 sorted() 函数按照什么标准进行排序。

5、多关键字排序的方法详解下面实例

返 回 值

返回重新排序的列表。

知识拓展

List sort ()函数: sort() 函数用于对原列表进行排序,如果指定参数,则使用指定的比较函数

sort 与 sorted 区别:

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

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

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

实 例

代  码

#sorted()函数会返回一个排序列表,不改变原有序列

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

sorted(mlist)

print(mlist)

print(sorted(mlist))

运行结果

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

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

代  码

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

print(sorted(mlist))

mlist2=sorted(mlist)

print(mlist2)

运行结果

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

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

代  码

# sorted() 函数默认对序列中元素进行升序排序,通过手动将其 reverse 参数值改为 True,可实现降序排序。

mlist = [3,1,4,9,3]

print(mlist)

print(sorted(mlist))

print(sorted(mlist, reverse=True))

运行结果

[3, 1, 4, 9, 3]

[1, 3, 3, 4, 9]

[9, 4, 3, 3, 1]

代  码

# 对元组进行排序

mlist = (4, 7, 7, 1, 9, 5, 2, 6)

print(mlist)

print(sorted(mlist))

运行结果

(4, 7, 7, 1, 9, 5, 2, 6)

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

代  码

# 字典默认按照key进行排序

mlist = {3:4,1: 5,4:3,6:2,5:8}

print(mlist)

print(sorted(mlist.items()))

运行结果

{1: 5, 3: 4, 4: 3, 5: 8, 6: 2}

[(1, 5), (3, 4), (4, 3), (5, 8), (6, 2)]

代  码

mlist = {"苹果":2,"芒果": 5,"火龙果":3,"草莓":2,"菠萝":8}

print(mlist)

print(sorted(mlist.items()))

运行结果

{'火龙果': 3, '草莓': 2, '菠萝': 8, '苹果': 2, '芒果': 5}

[('火龙果', 3), ('芒果', 5), ('苹果', 2), ('草莓', 2), ('菠萝', 8)]

代  码

# 对集合进行排序

mlist = {4, 7, 7, 1, 9, 5, 2, 6}

print(mlist)

print(sorted(mlist))

运行结果

{1, 2, 4, 5, 6, 7, 9}

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

代  码

# 对字符串进行排序

mlist = "47719526"

print(mlist)

print(sorted(mlist))

运行结果

47719526

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

代  码

#在调用 sorted() 函数时,还可传入一个 key 参数,它可以接受一个函数,该函数的功能是指定 sorted() 函数按照什么标准进行排序。

mlist =["Adversity Awake","逆境清醒","走寻半生仍笑叹","得失看平淡"]

print(mlist)

print(sorted(mlist))

print(sorted(mlist,key=lambda mlist:len(mlist)))

运行结果

['Adversity Awake', '逆境清醒', '走寻半生仍笑叹', '得失看平淡']

['Adversity Awake', '得失看平淡', '走寻半生仍笑叹', '逆境清醒']

['逆境清醒', '得失看平淡', '走寻半生仍笑叹', 'Adversity Awake']

代  码

mlist = [('苹果','红',8),('苹果','青',9),('芒果','黄',2), ('桃子','红',1),('菠萝','黄',6)]

print(mlist)

print(sorted(mlist)) 

print(sorted(mlist, key=lambda s: s[0])) 

print(sorted(mlist, key=lambda s: s[1])) 

print(sorted(mlist, key=lambda s: s[2])) # 按数量排序

print(sorted(mlist, key=lambda s: s[2], reverse=True)) # 按降序

运行结果

[('苹果', '红', 8), ('苹果', '青', 9), ('芒果', '黄', 2), ('桃子', '红', 1), ('菠萝', '黄', 6)]

[('桃子', '红', 1), ('芒果', '黄', 2), ('苹果', '红', 8), ('苹果', '青', 9), ('菠萝', '黄', 6)]

[('桃子', '红', 1), ('芒果', '黄', 2), ('苹果', '红', 8), ('苹果', '青', 9), ('菠萝', '黄', 6)]

[('苹果', '红', 8), ('桃子', '红', 1), ('苹果', '青', 9), ('芒果', '黄', 2), ('菠萝', '黄', 6)]

[('桃子', '红', 1), ('芒果', '黄', 2), ('菠萝', '黄', 6), ('苹果', '红', 8), ('苹果', '青', 9)]

[('苹果', '青', 9), ('苹果', '红', 8), ('菠萝', '黄', 6), ('芒果', '黄', 2), ('桃子', '红', 1)]

代  码

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

print(mlist)

print(sorted(mlist))

print(sorted(mlist, reverse=True))

运行结果

['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)

print(sorted(mlist, reverse=True))

运行结果

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

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

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

多关键字排序的方法

代  码

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

print(mlist)

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

print(sorted(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)

print(sorted(mlist,key=lambda x:x[0])) 

运行结果

[[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)

print(sorted(mlist,key=lambda x:x[1])) 

运行结果

[[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)

print(sorted(mlist,key=lambda x:(x[1],-x[0]))) 

运行结果

[[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)

print(sorted(mlist,key=lambda x:(len(x),x))) 

运行结果

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

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

代  码

#

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

print(mlist)

print(sorted(mlist,key=lambda x:(len(x),list(map(lambda c:-ord(c),x))))) 

运行结果

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

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

代  码

#

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

print(mlist)

print(sorted(mlist,key=lambda x:(-len(x),x))) 

运行结果

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

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

代  码

mlist= ['走寻半生仍笑叹','得失看平淡','愿你释怀愁与冷','萤火明晰双眼','辨分真与假','逆境清醒']

print(mlist)

print(sorted(mlist,key=lambda x:(len(x),x)))

print(sorted(mlist,key=lambda x:(len(x),list(map(lambda c:-ord(c),x))))) 

print(sorted(mlist,key=lambda x:(-len(x),x)))

运行结果

['走寻半生仍笑叹', '得失看平淡', '愿你释怀愁与冷', '萤火明晰双眼', '辨分真与假',
 '逆境清醒']
['逆境清醒', '得失看平淡', '辨分真与假', '萤火明晰双眼', '愿你释怀愁与冷', '走寻
半生仍笑叹']
['逆境清醒', '辨分真与假', '得失看平淡', '萤火明晰双眼', '走寻半生仍笑叹', '愿你
释怀愁与冷']
['愿你释怀愁与冷', '走寻半生仍笑叹', '萤火明晰双眼', '得失看平淡', '辨分真与假',
 '逆境清醒']

代  码

运行结果

返回函数主目录

 

你可能感兴趣的:(python,python,开发语言,经验分享,青少年编程)