lambda,sorted,filter,map,递归,二分法
1. lambda 匿名函数
语法:
lambda 参数:返回值
不能完成复杂的操作
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
2. sorted() 函数
排序.
1. 可迭代对象
2. key=函数. 排序规则
3. reverse. 是否倒序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# lst = [5,7,6,12,1,13,9,18,5] # # lst.sort() # sort是list里面的一个方法 # # print(lst) # # ll = sorted(lst, reverse=True) # 内置函数. 返回给你一个新列表 新列表是被排序的 # print(ll) # 给列表排序. 根据字符串的长度进行排序 lst = ["大阳哥a", "尼古拉斯aa", "赵四aaa", "刘能a", "广坤aaaaaa", "谢大脚a"] # # def func(s): # return s.count('a') # 返回数字 # # ll = sorted(lst, key=lambda s:s.count('a')) # 内部. 把可迭代对象中的每一个元素传递给func # print(ll) # # lst = [ # {'id':1, 'name':'alex', 'age':18}, # {'id':2, 'name':'taibai', 'age':58}, # {'id':3, 'name':'wusir', 'age':38}, # {'id':4, 'name':'ritian', 'age':48}, # {'id':5, 'name':'女神', 'age':18} # ] # # ll = sorted(lst, key=lambda dic:dic['age'], reverse=True) # print(ll)
3. filter() 函数
过滤
1. 函数, 返回True或False
2. 可迭代对象
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# def func(i): # 判断奇数 # return i % 2 == 1 lst = [1,2,3,4,5,6,7,8,9] ll = filter(lambda i:i%2==1, lst) # 第一个参数. 函数. 将第二个参数中的每一个元素传给函数. 函数如果返回True, 留下该元素. # print("__iter__" in dir(ll)) # print("__next__" in dir(ll)) # print(list(ll)) # lst = [ # {'id':1, 'name':'alex', 'age':18}, # {'id':2, 'name':'taibai', 'age':58}, # {'id':3, 'name':'wusir', 'age':38}, # {'id':4, 'name':'ritian', 'age':48}, # {'id':5, 'name':'女神', 'age':18} # ] # # print(list(filter(lambda dic: dic['age']>40, lst)))
4. map() 映射函数
1. 函数
2. 可迭代对象
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# lst = [1,2,3,4,5,6,7,8,9,0,23,23,4,52,35,234,234,234,234,234,23,4] # it = map(lambda i: i * i, lst) # 把可迭代对象中的每一个元素传递给前面的函数进行处理. 处理的结果会返回成迭代器 # print(list(it)) # lst1 = [ 1, 2, 3, 4, 5] # lst2 = [ 2, 4, 6, 8] # print(list(map(lambda x, y:x+y, lst1, lst2))) # 如果函数中有多个参数. 后面对应的列表要一一对应
5. 递归
自己调用自己.
def func():
func()
func()
难点:不好想.需要找规律. 不好读
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
6. 二分法
掐头结尾取中间. 不停的改变左和右. 间接改变中间. 查询效率非常高
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# lst = [11,22,33,44,55,66,77,88,99,123,234,345,456,567,678,789,1111] # n = 567 # left = 0 # right = len(lst) - 1 # count = 1 # while left <= right: # middle = (left + right) // 2 # if n > lst[middle]: # left = middle + 1 # elif n < lst[middle]: # right = middle - 1 # else: # print(count) # print("存在") # print(middle) # break # count = count + 1 # else: # print("不存在") # lst = [11,22,33,44,55,66,77,88,99,123,234,345,456,567,678,789,1111] # # def binary_search(left, right, n): # middle = (left + right)//2 # if left > right: # return -1 # if n > lst[middle]: # left = middle + 1 # elif n < lst[middle]: # right = middle - 1 # else: # return middle # return binary_search(left, right, n) #此处return用于返回给上一次调用值 否则整个函数如果进行递归后会返回None # print(binary_search(0, len(lst)-1, 65) ) # def binary_search(lst, n): # left = 0 # right = len(lst) - 1 # middle = (left + right) // 2 # if right <= 0: # print("没找到") # return # if n > lst[middle]: # lst = lst[middle+1:] # elif n < lst[middle]: # lst = lst[:middle] # else: # print("找到了") # return # binary_search(lst, n) # binary_search(lst, 65) #列表切割二分法 #