python中,一般在涉及到列表排序时,都用内置的sort()方法或者全局的sorted()方法,区别如下:
1、sort()方法只能用于列表排序,不能用于字符串,字典等其他可迭代序列;sorted()方法可以用于所有的可迭代序列;
2、sort()方法是在原列表基础上进行排序,返回none,会破坏原始列表结构;sorted()方法是返回一个排序后的新序列,对原始列表无影响;
#sort()排序 >>> a=[6,9,8,4,3,1,2] >>> b=a.sort() >>> print(b) none >>> print(a) [1, 2, 3, 4, 6, 8, 9] #sorted()排序 >>> a=[6,9,8,4,3,1,2] >>> b=sorted(a) >>> print(b) [1, 2, 3, 4, 6, 8, 9] >>> print(a) [6, 9, 8, 4, 3, 1, 2]
字典排序时,sorted()方法默认是按照字典的键(key)排序的,如下:
>>> a={5:'a',1:'e',4:'b',2:'d',3:'c'} >>> b=sorted(a) >>> print(b) [1, 2, 3, 4, 5]
如果需要按照字典的value排序,可以用下面的方法:
>>> a={5:'a',1:'e',4:'b',2:'d',3:'c'} >>> b=sorted(a.items(), key=lambda item:item[1]) >>> print(b) [(5, 'a'), (4, 'b'), (3, 'c'), (2, 'd'), (1, 'e')]
高级用法
sort()方法和sorted()方法都可以指定参数来处理一些复杂场景的排序
1、key参数:指定一个函数,可以是内置函数,也可以是自己定义的函数,此函数将在每个元素比较前被调用。
2、reverse参数:此参数指定true or false,来进行降序或者升序,默认为false(升序)。
如下:
a = ["this", "a", "is", "bag"] b = sorted(a, key=str.lower) c = sorted(a, key=str.lower, reverse=true) print(b) print(c) ['a', 'bag', 'is', 'this'] ['this', 'is', 'bag', 'a']
更广泛的使用情况是用复杂对象的某些值来对复杂对象的序列排序,例如:
一个列表保存着每个学生的姓名,档次和分数
#学习中遇到问题没人解答?小编创建了一个python学习交流群:531509025 student_tuples = [ ('john', 'a', 96), ('leky', 'd', 63), ('andy', 'a', 92), ('jane', 'b', 82), ('dave', 'b', 85), ('cany', 'a', 96) ]
场景1、按档次从高到低进行排序
student_tuples = [ ('john', 'a', 96), ('leky', 'd', 63), ('andy', 'a', 92), ('jane', 'b', 82), ('dave', 'b', 85), ('cany', 'a', 96) ] print(sorted(student_tuples, key=lambda student: student[1])) [('john', 'a', 96), ('andy', 'a', 92), ('cany', 'a', 96), ('jane', 'b', 82), ('dave', 'b', 85), ('leky', 'd', 63)]
场景2、按分数从高到低排序
方法(1)、使用reverse参数
student_tuples = [ ('john', 'a', 96), ('leky', 'd', 63), ('andy', 'a', 92), ('jane', 'b', 82), ('dave', 'b', 85), ('cany', 'a', 96), ] print(sorted(student_tuples, key=lambda student: student[2], reverse=true)) [('john', 'a', 96), ('cany', 'a', 96), ('andy', 'a', 92), ('dave', 'b', 85), ('jane', 'b', 82), ('leky', 'd', 63)]
方法(2)、使用负号(-)
student_tuples = [ ('john', 'a', 96), ('leky', 'd', 63), ('andy', 'a', 92), ('jane', 'b', 82), ('dave', 'b', 85), ('cany', 'a', 96), ] print(sorted(student_tuples, key=lambda student: -student[2])) [('john', 'a', 96), ('cany', 'a', 96), ('andy', 'a', 92), ('dave', 'b', 85), ('jane', 'b', 82), ('leky', 'd', 63)]
注意:负号(-)只能用于数字前面,不能用于字符串前面
场景3、按档次从高到低进行排序,档次相同的按分数从高到底排序
student_tuples = [ ('john', 'a', 96), ('leky', 'd', 63), ('andy', 'a', 92), ('jane', 'b', 82), ('dave', 'b', 85), ('cany', 'a', 96) ] print(sorted(student_tuples, key=lambda student: [student[1], -student[2]])) [('john', 'a', 96), ('cany', 'a', 96), ('andy', 'a', 92), ('dave', 'b', 85), ('jane', 'b', 82), ('leky', 'd', 63)]
场景4、按档次从低到高进行排序,档次相同的按分数从低到高排序
student_tuples = [ ('john', 'a', 96), ('leky', 'd', 63), ('andy', 'a', 92), ('jane', 'b', 82), ('dave', 'b', 85), ('cany', 'a', 96) ] print(sorted(student_tuples, key=lambda student: [student[1], -student[2]],reverse=true)) [('leky', 'd', 63), ('jane', 'b', 82), ('dave', 'b', 85), ('andy', 'a', 92), ('john', 'a', 96), ('cany', 'a', 96)]
场景5、按档次从低到高进行排序,档次相同的按分数从低到高排序,最后再按照姓名升序
姓名是字符串,不能在字符串前面用“符号(-)”来排序,可以重写“富比较”方法
#学习中遇到问题没人解答?小编创建了一个python学习交流群:531509025 class reversinator(object): def __init__(self, obj): self.obj = obj def __lt__(self, other): return other.obj < self.obj student_tuples = [ ('john', 'a', 96), ('leky', 'd', 63), ('andy', 'a', 92), ('jane', 'b', 82), ('dave', 'b', 85), ('cany', 'a', 96), ] print(sorted(student_tuples, key=lambda student: [student[1], -student[2], reversinator(student[0])], reverse=true)) [('leky', 'd', 63), ('jane', 'b', 82), ('dave', 'b', 85), ('andy', 'a', 92), ('cany', 'a', 96), ('john', 'a', 96)]
到此这篇关于python序列排序的具体场景实现的文章就介绍到这了,更多相关python序列排序内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论