参考链接: Python内置函数sorted()和列表的sort方法使用介绍
参考链接: Python中优先级队列的实现,heapq堆队列算法实现最小堆
参考链接: 排序指南
参考链接: sorted(iterable, *, key=None, reverse=False)
参考链接: sort(*, key=None, reverse=False)
参考链接: Operator 模块函数
参考链接: 使用 cmp 参数的旧方法
参考链接: 其它
代码实验展示:
# 实现多级排序,首先按成绩降序,再按年龄升序,最后按姓名字典序升序
stuTuple = tuple(
[
(98, 23, 'mike'),
(97, 37, 'steve'),
(92, 18, 'james'),
(91, 16, 'eric'),
(99, 12, 'john'),
(99, 12, 'dave'),
(99, 14, 'jane'),
(98, 25, 'jack'),
(98, 25, 'bob'),
]
)
def printInfo(ls):
for item in ls:
print(item)
ls = list()
for stu in stuTuple:
ls.append(stu)
print("以下为排序前".center(50,'-'))
printInfo(ls)
print('使用三元组进行逐个元素排序')
print("以下为排序后".center(50,'-'))
ls.sort(key=lambda x:(-1*x[0], x[1], x[2]))
printInfo(ls)
控制台结果输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 796 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\测试排序> & 'D:\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '12639' '--' 'c:\Users\chenxuqi\Desktop\测试排序\test.py'
----------------------以下为排序前----------------------
(98, 23, 'mike')
(97, 37, 'steve')
(92, 18, 'james')
(91, 16, 'eric')
(99, 12, 'john')
(99, 12, 'dave')
(99, 14, 'jane')
(98, 25, 'jack')
(98, 25, 'bob')
使用三元组进行逐个元素排序
----------------------以下为排序后----------------------
(99, 12, 'dave')
(99, 12, 'john')
(99, 14, 'jane')
(98, 23, 'mike')
(98, 25, 'bob')
(98, 25, 'jack')
(97, 37, 'steve')
(92, 18, 'james')
(91, 16, 'eric')
(base) PS C:\Users\chenxuqi\Desktop\测试排序>
代码实验展示:
# 实现多级排序,首先按成绩降序,再按年龄升序,最后按姓名字典序升序
stuTuple = tuple(
[
(98, 23, 'mike'),
(97, 37, 'steve'),
(92, 18, 'james'),
(91, 16, 'eric'),
(99, 12, 'john'),
(99, 12, 'dave'),
(99, 14, 'jane'),
(98, 25, 'jack'),
(98, 25, 'bob'),
]
)
def printInfo(ls):
for item in ls:
print(item)
ls = list()
for stu in stuTuple:
ls.append(stu)
print("以下为排序前".center(50,'-'))
printInfo(ls)
print('使用三元组进行逐个元素排序')
print("以下为排序后".center(50,'-'))
ls.sort(key=lambda x: x[2])
ls.sort(key=lambda x: x[1])
ls.sort(key=lambda x: x[0], reverse=True)
printInfo(ls)
控制台结果输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 764 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\测试排序> & 'D:\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '12752' '--' 'c:\Users\chenxuqi\Desktop\测试排序\test.py'
----------------------以下为排序前----------------------
(98, 23, 'mike')
(97, 37, 'steve')
(92, 18, 'james')
(91, 16, 'eric')
(99, 12, 'john')
(99, 12, 'dave')
(99, 14, 'jane')
(98, 25, 'jack')
(98, 25, 'bob')
使用三元组进行逐个元素排序
----------------------以下为排序后----------------------
(99, 12, 'dave')
(99, 12, 'john')
(99, 14, 'jane')
(98, 23, 'mike')
(98, 25, 'bob')
(98, 25, 'jack')
(97, 37, 'steve')
(92, 18, 'james')
(91, 16, 'eric')
(base) PS C:\Users\chenxuqi\Desktop\测试排序>
使用cmp参数实现方式,代码实验展示:
# 实现多级排序,首先按成绩降序,再按年龄升序,最后按姓名字典序升序
stuTuple = tuple(
[
(98, 23, 'mike'),
(97, 37, 'steve'),
(92, 18, 'james'),
(91, 16, 'eric'),
(99, 12, 'john'),
(99, 12, 'dave'),
(99, 14, 'jane'),
(98, 25, 'jack'),
(98, 25, 'bob'),
]
)
def printInfo(ls):
for item in ls:
print(item)
ls = list()
for stu in stuTuple:
ls.append(stu)
print("以下为排序前".center(50,'-'))
printInfo(ls)
print('使用cmp参数进行排序')
def cmp_to_key(mycmp):
'Convert a cmp= function into a key= function'
class K:
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
return K
def compareStudent(x, y): # 返回1表示要交换x和y
if x[0]>y[0]:
return -1
elif x[0]<y[0]:
return 1
else:
if x[1]>y[1]:
return 1
elif x[1]<y[1]:
return -1
else:
if x[2]>y[2]:
return 1
elif x[2]<y[2]:
return -1
else:
return 0
ls.sort(key=cmp_to_key(compareStudent))
print("以下为排序后".center(50,'-'))
printInfo(ls)
控制台结果输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 774 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\测试排序> & 'D:\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '12976' '--' 'c:\Users\chenxuqi\Desktop\测试排序\test.py'
----------------------以下为排序前----------------------
(98, 23, 'mike')
(97, 37, 'steve')
(92, 18, 'james')
(91, 16, 'eric')
(99, 12, 'john')
(99, 12, 'dave')
(99, 14, 'jane')
(98, 25, 'jack')
(98, 25, 'bob')
使用cmp参数进行排序
----------------------以下为排序后----------------------
(99, 12, 'dave')
(99, 12, 'john')
(99, 14, 'jane')
(98, 23, 'mike')
(98, 25, 'bob')
(98, 25, 'jack')
(97, 37, 'steve')
(92, 18, 'james')
(91, 16, 'eric')
(base) PS C:\Users\chenxuqi\Desktop\测试排序>
使用operator.itemgetter函数实现,代码实验展示:
# 实现多级排序,首先按成绩降序,再按年龄升序,最后按姓名字典序升序
import operator
stuTuple = tuple(
[
(98, 23, 'mike'),
(97, 37, 'steve'),
(92, 18, 'james'),
(91, 16, 'eric'),
(99, 12, 'john'),
(99, 12, 'dave'),
(99, 14, 'jane'),
(98, 25, 'jack'),
(98, 25, 'bob'),
]
)
def printInfo(ls):
for item in ls:
print(item)
ls = list()
for stu in stuTuple:
ls.append(stu)
print("以下为排序前".center(50,'-'))
printInfo(ls)
print('使用operator.itemgetter函数进行排序')
ls.sort(key=operator.itemgetter(2),reverse=False)
ls.sort(key=operator.itemgetter(1),reverse=False)
ls.sort(key=operator.itemgetter(0),reverse=True)
print("以下为排序后".center(50,'-'))
printInfo(ls)
控制台结果输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 776 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\测试排序> & 'D:\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '13071' '--' 'c:\Users\chenxuqi\Desktop\测试排序\test.py'
----------------------以下为排序前----------------------
(98, 23, 'mike')
(97, 37, 'steve')
(92, 18, 'james')
(91, 16, 'eric')
(99, 12, 'john')
(99, 12, 'dave')
(99, 14, 'jane')
(98, 25, 'jack')
(98, 25, 'bob')
使用operator.itemgetter函数进行排序
----------------------以下为排序后----------------------
(99, 12, 'dave')
(99, 12, 'john')
(99, 14, 'jane')
(98, 23, 'mike')
(98, 25, 'bob')
(98, 25, 'jack')
(97, 37, 'steve')
(92, 18, 'james')
(91, 16, 'eric')
(base) PS C:\Users\chenxuqi\Desktop\测试排序>
自定义类型的多重排序(使用元组),代码实验展示:
# 实现多级排序,首先按成绩降序,再按年龄升序,最后按姓名字典序升序
import operator
stuTuple = tuple(
[
(98, 23, 'mike'),
(97, 37, 'steve'),
(92, 18, 'james'),
(91, 16, 'eric'),
(99, 12, 'john'),
(99, 12, 'dave'),
(99, 14, 'jane'),
(98, 25, 'jack'),
(98, 25, 'bob'),
]
)
class Student():
def __init__(self, grade, age, name):
self.grade = grade
self.age = age
self.name = name
def __repr__(self):
return "Student(grade={0}, age={1}, name='{2}')"\
.format(self.grade,self.age,self.name)
def printInfo(ls):
for item in ls:
print(item)
ls = list()
for stu in stuTuple:
ls.append(Student(*stu))
print("以下为排序前".center(50,'-'))
printInfo(ls)
print("使用元组进行排序,一次性排序")
ls.sort(key=lambda x:(-1*x.grade,x.age,x.name))
print("以下为排序后".center(50,'-'))
printInfo(ls)
控制台结果输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 798 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\测试排序> & 'D:\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '13530' '--' 'c:\Users\chenxuqi\Desktop\测试排序\test.py'
----------------------以下为排序前----------------------
Student(grade=98, age=23, name='mike')
Student(grade=97, age=37, name='steve')
Student(grade=92, age=18, name='james')
Student(grade=91, age=16, name='eric')
Student(grade=99, age=12, name='john')
Student(grade=99, age=12, name='dave')
Student(grade=99, age=14, name='jane')
Student(grade=98, age=25, name='jack')
Student(grade=98, age=25, name='bob')
使用元组进行排序,一次性排序
----------------------以下为排序后----------------------
Student(grade=99, age=12, name='dave')
Student(grade=99, age=12, name='john')
Student(grade=99, age=14, name='jane')
Student(grade=98, age=23, name='mike')
Student(grade=98, age=25, name='bob')
Student(grade=98, age=25, name='jack')
Student(grade=97, age=37, name='steve')
Student(grade=92, age=18, name='james')
Student(grade=91, age=16, name='eric')
(base) PS C:\Users\chenxuqi\Desktop\测试排序>
自定义类型的多重排序(使用元组,分步排序),代码实验展示:
# 实现多级排序,首先按成绩降序,再按年龄升序,最后按姓名字典序升序
import operator
stuTuple = tuple(
[
(98, 23, 'mike'),
(97, 37, 'steve'),
(92, 18, 'james'),
(91, 16, 'eric'),
(99, 12, 'john'),
(99, 12, 'dave'),
(99, 14, 'jane'),
(98, 25, 'jack'),
(98, 25, 'bob'),
]
)
class Student():
def __init__(self, grade, age, name):
self.grade = grade
self.age = age
self.name = name
def __repr__(self):
return "Student(grade={0}, age={1}, name='{2}')"\
.format(self.grade,self.age,self.name)
def printInfo(ls):
for item in ls:
print(item)
ls = list()
for stu in stuTuple:
ls.append(Student(*stu))
print("以下为排序前".center(50,'-'))
printInfo(ls)
print("使用元组进行排序,一次性排序")
ls.sort(key=lambda x:x.name)
ls.sort(key=lambda x:x.age)
ls.sort(key=lambda x:x.grade,reverse=True)
print("以下为排序后".center(50,'-'))
printInfo(ls)
控制台结果输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 788 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\测试排序> & 'D:\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '13627' '--' 'c:\Users\chenxuqi\Desktop\测试排序\test.py'
----------------------以下为排序前----------------------
Student(grade=98, age=23, name='mike')
Student(grade=97, age=37, name='steve')
Student(grade=92, age=18, name='james')
Student(grade=91, age=16, name='eric')
Student(grade=99, age=12, name='john')
Student(grade=99, age=12, name='dave')
Student(grade=99, age=14, name='jane')
Student(grade=98, age=25, name='jack')
Student(grade=98, age=25, name='bob')
使用元组进行排序,一次性排序
----------------------以下为排序后----------------------
Student(grade=99, age=12, name='dave')
Student(grade=99, age=12, name='john')
Student(grade=99, age=14, name='jane')
Student(grade=98, age=23, name='mike')
Student(grade=98, age=25, name='bob')
Student(grade=98, age=25, name='jack')
Student(grade=97, age=37, name='steve')
Student(grade=92, age=18, name='james')
Student(grade=91, age=16, name='eric')
(base) PS C:\Users\chenxuqi\Desktop\测试排序>
自定义类型的多重排序(使用cmp函数),代码实验展示:
# 实现多级排序,首先按成绩降序,再按年龄升序,最后按姓名字典序升序
import operator
stuTuple = tuple(
[
(98, 23, 'mike'),
(97, 37, 'steve'),
(92, 18, 'james'),
(91, 16, 'eric'),
(99, 12, 'john'),
(99, 12, 'dave'),
(99, 14, 'jane'),
(98, 25, 'jack'),
(98, 25, 'bob'),
]
)
class Student():
def __init__(self, grade, age, name):
self.grade = grade
self.age = age
self.name = name
def __repr__(self):
return "Student(grade={0}, age={1}, name='{2}')"\
.format(self.grade,self.age,self.name)
def printInfo(ls):
for item in ls:
print(item)
ls = list()
for stu in stuTuple:
ls.append(Student(*stu))
print("以下为排序前".center(50,'-'))
printInfo(ls)
#################################################################
print('使用cmp参数进行排序')
def cmp_to_key(mycmp):
'Convert a cmp= function into a key= function'
class K:
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
return K
def compareStudent(x, y): # 返回1表示要交换x和y
if x.grade>y.grade:
return -1
elif x.grade<y.grade:
return 1
else:
if x.age>y.age:
return 1
elif x.age<y.age:
return -1
else:
if x.name>y.name:
return 1
elif x.name<y.name:
return -1
else:
return 0
ls.sort(key=cmp_to_key(compareStudent))
#############################################################
print("以下为排序后".center(50,'-'))
printInfo(ls)
控制台结果输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 787 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\测试排序> & 'D:\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '13782' '--' 'c:\Users\chenxuqi\Desktop\测试排序\test.py'
----------------------以下为排序前----------------------
Student(grade=98, age=23, name='mike')
Student(grade=97, age=37, name='steve')
Student(grade=92, age=18, name='james')
Student(grade=91, age=16, name='eric')
Student(grade=99, age=12, name='john')
Student(grade=99, age=12, name='dave')
Student(grade=99, age=14, name='jane')
Student(grade=98, age=25, name='jack')
Student(grade=98, age=25, name='bob')
使用cmp参数进行排序
----------------------以下为排序后----------------------
Student(grade=99, age=12, name='dave')
Student(grade=99, age=12, name='john')
Student(grade=99, age=14, name='jane')
Student(grade=98, age=23, name='mike')
Student(grade=98, age=25, name='bob')
Student(grade=98, age=25, name='jack')
Student(grade=97, age=37, name='steve')
Student(grade=92, age=18, name='james')
Student(grade=91, age=16, name='eric')
(base) PS C:\Users\chenxuqi\Desktop\测试排序>
自定义类型的多重排序(_重载_lt__成员方法),代码实验展示:
# 实现多级排序,首先按成绩降序,再按年龄升序,最后按姓名字典序升序
import operator
stuTuple = tuple(
[
(98, 23, 'mike'),
(97, 37, 'steve'),
(92, 18, 'james'),
(91, 16, 'eric'),
(99, 12, 'john'),
(99, 12, 'dave'),
(99, 14, 'jane'),
(98, 25, 'jack'),
(98, 25, 'bob'),
]
)
class Student():
def __init__(self, grade, age, name):
self.grade = grade
self.age = age
self.name = name
def __repr__(self):
return "Student(grade={0}, age={1}, name='{2}')"\
.format(self.grade,self.age,self.name)
def printInfo(ls):
for item in ls:
print(item)
ls = list()
for stu in stuTuple:
ls.append(Student(*stu))
print("以下为排序前".center(50,'-'))
printInfo(ls)
#################################################################
print('使用__lt__函数进行排序')
def stu__lessthan(self, y): # 返回1表示要交换x和y
if self.grade != y.grade:
return self.grade > y.grade
elif self.age != y.age:
return self.age < y.age
else:
return self.name < y.name
Student.__lt__ = stu__lessthan
ls.sort()
#############################################################
print("以下为排序后".center(50,'-'))
printInfo(ls)
控制台结果输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 826 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\测试排序> & 'D:\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '14067' '--' 'c:\Users\chenxuqi\Desktop\测试排序\test.py'
----------------------以下为排序前----------------------
Student(grade=98, age=23, name='mike')
Student(grade=97, age=37, name='steve')
Student(grade=92, age=18, name='james')
Student(grade=91, age=16, name='eric')
Student(grade=99, age=12, name='john')
Student(grade=99, age=12, name='dave')
Student(grade=99, age=14, name='jane')
Student(grade=98, age=25, name='jack')
Student(grade=98, age=25, name='bob')
使用__lt__函数进行排序
----------------------以下为排序后----------------------
Student(grade=99, age=12, name='dave')
Student(grade=99, age=12, name='john')
Student(grade=99, age=14, name='jane')
Student(grade=98, age=23, name='mike')
Student(grade=98, age=25, name='bob')
Student(grade=98, age=25, name='jack')
Student(grade=97, age=37, name='steve')
Student(grade=92, age=18, name='james')
Student(grade=91, age=16, name='eric')
(base) PS C:\Users\chenxuqi\Desktop\测试排序>
自定义类型的多重排序(使用operator.attrgetter进行分步排序),代码实验展示:
# 实现多级排序,首先按成绩降序,再按年龄升序,最后按姓名字典序升序
import operator
stuTuple = tuple(
[
(98, 23, 'mike'),
(97, 37, 'steve'),
(92, 18, 'james'),
(91, 16, 'eric'),
(99, 12, 'john'),
(99, 12, 'dave'),
(99, 14, 'jane'),
(98, 25, 'jack'),
(98, 25, 'bob'),
]
)
class Student():
def __init__(self, grade, age, name):
self.grade = grade
self.age = age
self.name = name
def __repr__(self):
return "Student(grade={0}, age={1}, name='{2}')"\
.format(self.grade,self.age,self.name)
def printInfo(ls):
for item in ls:
print(item)
ls = list()
for stu in stuTuple:
ls.append(Student(*stu))
print("以下为排序前".center(50,'-'))
printInfo(ls)
#################################################################
print('使用operator.attrgetter进行分步排序')
ls.sort(key=operator.attrgetter('age','name'))
ls.sort(key=operator.attrgetter('grade'),reverse=True)
#############################################################
print("以下为排序后".center(50,'-'))
printInfo(ls)
控制台结果输出:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 843 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\测试排序> & 'D:\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '14433' '--' 'c:\Users\chenxuqi\Desktop\测试排序\test.py'
----------------------以下为排序前----------------------
Student(grade=98, age=23, name='mike')
Student(grade=97, age=37, name='steve')
Student(grade=92, age=18, name='james')
Student(grade=91, age=16, name='eric')
Student(grade=99, age=12, name='john')
Student(grade=99, age=12, name='dave')
Student(grade=99, age=14, name='jane')
Student(grade=98, age=25, name='jack')
Student(grade=98, age=25, name='bob')
使用operator.attrgetter进行分步排序
----------------------以下为排序后----------------------
Student(grade=99, age=12, name='dave')
Student(grade=99, age=12, name='john')
Student(grade=99, age=14, name='jane')
Student(grade=98, age=23, name='mike')
Student(grade=98, age=25, name='bob')
Student(grade=98, age=25, name='jack')
Student(grade=97, age=37, name='steve')
Student(grade=92, age=18, name='james')
Student(grade=91, age=16, name='eric')
(base) PS C:\Users\chenxuqi\Desktop\测试排序>