- 对字符进行切割并获取局部数据,定义函数接收参数,处理获得到的后辍名并返回
def func(data_str):
return data_str.split(".")[-1]
name = '01 python基础入门.mp4'
ext = func(name)
print(ext)
# 方式2 通过 lambda进行实现
func = lambda data_str: data_str.split(".")[-1]
ext = func(name)
print(ext)
if __name__ == '__main__':
print('Python')
data_list = [lambda data_str: data_str.split(".")[-1],
lambda data_str: data_str.split(".")[-1],
lambda data_str: data_str.split(".")[-1],
]
name = '01 python基础入门.mp4'
ext = data_list[1](name)
print(ext)
data_list = [
'5 编译器和解释器.mp4',
'9 Python解释器种类.mp4',
'2 课堂笔记的创建.mp4',
'8 Python介绍.mp4',
'7 编程语言的分类.mp4',
'3 常见计算机基本概念.mp4',
'1 今日概要.mp4',
'6 学习编程本质上的三件事.mp4',
'4 编程语言.mp4'
]
def func(x):
return int(x.split(" ")[0])
ret = sorted(data_list, key=func)
print(ret)
# lambda 匿名函数表达
ret = sorted(data_list, key=lambda x: int(x.split(" ")[0]))
print(ret)