lambda , sorted , filter , map函数,递归遍历一个目录
1,lambda函数
- lambda只能处理简单的函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #一个简单的函数 2 def func(x,y): 3 return x+y 4 #用lambda实现 5 lambda x,y : x+y
2,sorted排序函数
-
sorted函数
sorted函数是排序的,是排序一个iterable类型的数据,一般的iterable类型的数据都要这个sort方法,sorted函数能自定义排序的规则#sorted函数的类型 sorted(iterable,key=none,reverse=False) lst = [{"id":1,"name":"shuai","age":25},{"id":3,"name":"xiang","age":18},{"id":2,"name":"aoli","age":27}] #按年龄排序 l1 = sorted(lst,key=lambda dic : dic["age"]) print(l1) #按名字首字母排序 l2 = sorted(lst,key=lambda dic : dic["name"][0]) print(l2)
3,filter函数
- filter函数
通过函数func判断iterable里面的元素是否符合
#filter(func,iterable) lst = [19,20,18,30,25,17] def func(age): return age >18 f = filter(func,lst) print(f) print("__iter__" in dir(f)) for i in f: print(i)
4,map函数
- map映射函数
#map(func,iterable) lst = [1,5,3,5,4,8,7,9] l1 = map(lambda x : x**2,lst) print(list(l1))
5,递归
递归主要特点是自己调用自己,一定要有指定的结束,Python中官方表示的最深递归次数为1000
一个递归的死循环
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
ceng = 1 def fun(ceng): print(ceng) fun(ceng+1) fun(ceng) 运行结果 995 996 997 998Traceback (most recent call last): File "E:/python_iteam/LivePython/day6/目录/递归.py", line 26, in <module> fun(ceng) File "E:/python_iteam/LivePython/day6/目录/递归.py", line 25, in fun fun(ceng+1) File "E:/python_iteam/LivePython/day6/目录/递归.py", line 25, in fun fun(ceng+1) File "E:/python_iteam/LivePython/day6/目录/递归.py", line 25, in fun fun(ceng+1) [Previous line repeated 994 more times] File "E:/python_iteam/LivePython/day6/目录/递归.py", line 24, in fun print(ceng) RecursionError: maximum recursion depth exceeded while calling a Python object
通过递归,遍历一个文件夹里的所有文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os def func(file_dir,ceng): list = os.listdir(file_dir)#通过os模块,得到目录下面的目录,以list表示 for file in list:#循环list,区分文件和目录 file_path = os.path.join(file_dir,file)#得到绝对路径 if os.path.isdir(file_path): print("\t"*ceng+"%s :" %file) func(file_path,ceng+1)#递归函数 else: print("\t"*ceng+"%s"%file) func("F:\\",0)
6,二分法查找
- 第一种,直接循环
1 list = [1,3,4,5,6,8,9,10,24,34,45,54,63] 2 lift = 0 3 right = len(list) 4 n = int(input("请输入一个数:")) 5 while lift < right: 6 mid = (lift + right) // 2 7 if list[mid] > n: 8 right = mid 9 elif list[mid] < n : 10 lift = mid + 1 11 else: 12 print("找到了数%s,下标是%s" %(list[mid],mid)) 13 break 14 else: 15 print("list里没有这个数")
- 第二种,通过函数递归,建立一个新的list
1 list = [1,3,4,5,6,8,9,10,24,34,45,54,63] 2 lift = 0 3 right = len(list) 4 n = int(input("请输入一个数:")) 5 def func(list,n): 6 lift = 0 7 right = len(list) 8 mid = (lift + right)//2 9 if mid > 0: 10 if list[mid] > n: 11 new_list = list[:mid] 12 func(new_list,n) 13 elif list[mid] < n: 14 new_list = list[mid+1:] 15 func(new_list,n) 16 elif list[mid] == n: 17 print("找到了数%s" %list[mid]) 18 else: 19 print("找不到") 20 func(list,n)
- 第三种,通过函数递归,给出左右
1 list = [1,3,4,5,6,8,9,10,24,34,45,54,63] 2 lift = 0 3 right = len(list) 4 n = int(input("请输入一个数:")) 5 6 def func(list,lift,right,n): 7 mid = (lift+right)//2 8 if lift < right: 9 if list[mid] > n: 10 right = mid 11 func(list, lift, right, n) 12 elif list[mid] < n : 13 lift = mid+1 14 func(list, lift, right, n) 15 elif list[mid] == n : 16 print("找到了%s,位置%s" %(list[mid],mid)) 17 else: 18 print("没找到") 19 func(list,lift,right,n)