排序 sorted
就是比谁是老大
示例
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Python 高阶函数的sorted 用法
# 反手就是一个排序
def sortedTest():
# 函数,序列
result = sorted([3,4,1,5,0])
print(result)
sortedTest()
运行结果
D:\PythonProject>python run.py
[0, 1, 3, 4, 5]
指定形式排序
示例-【key = abs】
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Python 高阶函数的sorted 用法
# 指定形式排序
def sortedTest():
# 按 绝对值排序
result = sorted([3,4,1,-5,0],key = abs)
print(result)
sortedTest()
运行结果
D:\PythonProject>python run.py
[0, 1, 3, 4, -5]
字符串排序
示例
忽略大小写+反向排序
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Python 高阶函数的sorted 用法
# 字符串排序
def sortedTest():
#忽略大小写+反向排序
result = sorted(["CCC", "bBB", "aaa", "DDD"],key = str.lower, reverse = True)
print(result)
sortedTest()
运行结果
D:\PythonProject>python run.py
['DDD', 'CCC', 'bBB', 'aaa']