8.02_python_lx_day13<2>

一.高阶函数 : 能够把函数当成参数传递的就是高阶函数

(1)map

map(func,Iterable)
  功能:处理数据
    把Iterable中的数据一个一个拿出来,扔到func函数中做处理
    把处理之后的结果放到迭代器当中,最后返回迭代器
  参数:
    func : 自定义函数 或 内置函数
    Iterable : 可迭代性数据(容器类型数据 range对象 迭代器)
  返回值:
    迭代器

 <1>lst = ["1","2","3","4"]  # [1,2,3,4]

①常规写法

1 lst = ["1","2","3","4"]
2 lst_new = []
3 for i in lst:
4     lst_new.append(int(i))
5 print(lst_new)

②map改造

1 lst = ["1","2","3","4"]
2 it = map(int,lst)
3 print(list(it))

<2>lst = [1,2,3,4] => [2,8,24,64]

①常规写法

1 lst = [1,2,3,4]
2 lst_new = []
3 for i in lst:
4     res = i << i
5     lst_new.append(res)
6 print(lst_new)

②map改造

参数和返回值return一定要写

1 def func(n):
2     return n << n 
3 lst = [1,2,3,4]
4 if = map(func,lst)

③lambda + map 

1 it = map(lambda n : n<<n , lst)
2 print(list(it))

<3>dic = {97:"a",98:"b",99:"c"}  # ["a","b","c"] => ascii [97,98,99]

常规写法  

将键值对反转

 1 dic = {97:"a",98:"b",99:"c"}
 2 dic_new = {}
 3 for k,v in dic.items():
 4     dic_new[v] = k
 5 print(dic_new)
 6 
 7 lst = ["a","b","c"]
 8 lst_new = []
 9 # 遍历列表
10 for i in lst:
11     # 通过键取值
12     res = dic_new[i]
13     # 把值追加到新的列表中
14     lst_new.append(res)
15 # 返回新列表
16 print(lst_new)

map改造

 1 def func(n):
 2     # 原字典
 3     dic = {97:"a",98:"b",99:"c"}
 4     # 新字典
 5     dic_new = {}
 6     # 遍历原字典
 7     for k,v in dic.items():
 8         # 更换键值对
 9         dic_new[v] = k
10     print(dic_new) # {'a': 97, 'b': 98, 'c': 99}
11     # 通过键来获取值
12     return dic_new[n]
13 lst = ["a","b","c"]
14 it = map(func,lst)
15 print(list(it))

(2)filter

filter(func,iterable)
  功能: 过滤数据
    return True 当前这个数据保留
    return False 当前这个数据舍弃
  参数:
    func : 自定义函数
    iterable : 可迭代型数据(容器类型数据,range对象,迭代器)
  返回值:
    迭代器

<1>lst = [1,2,3,4,5,6,7,8,9,10] 取偶

①常规写法

1 lst = [1,2,3,4,5,6,7,8,9,10]
2 lst_new = []
3 for i in lst:
4     if i % 2 == 0:
5         lst_new.append(i)
6 print(lst_new

②filter改写

1 def func():
2     if i % 2 == 0:
3         return True
4     else:
5         return False
6 it = filter(func,lst)
7 print(list(it))

③filter + lambda 改写

1 lst = [1,2,3,4,5,6,7,8,9,10]
2 it = filter(lambda i: True if i %2==0 else False,lst)
3 print(lst)

(3)reduce

reduce(func,iterable)
  功能:计算数据
    先把iterable中的前两个值拿出来,扔到func当中做运算,
    把计算的结果和iterable中的第三个元素在扔到func当中做运算,
    再把结果算出来,和第四个元素做运算,以此类推
    直到所有结果运算完毕.返回该结果
  参数:
    func : 自定义函数
    iterable : 可迭代型数据(容器类型数据,range对象,迭代器)
  返回值:
    计算之后的结果

from  functools import reduce

<1>lst = [5,4,8,8]  => 整型5488

①常规写法

1 lst = [5,4,8,8]
2 strvar = ""
3 for i in lst:
4     strvar += str(i)
5 res = int(strvar)
6 print(res , type(res))

②使用迭代器

 1 from collections import Iterator,Iterable
 2 lst = [5,4,8,8]
 3 it = iter(lst)
 4 print(isinstance(it , Iterator))
 5 print(isinstance(it , Iterable))
 6 num1 = next(it)
 7 num2 = next(it)
 8 print(num1,num2)
 9 num = num1 * 10 + num2
10 print(num) # 54
11 for i in it:
12     num = num * 10 + i # 54 * 10 + 8 => 548
13 print(num, type(num))

③reduce 改造

1 def func(x,y):
2     return x*10+y
3 lst = [5,4,8,8]
4 res = reduce(func,lst)
5 print(res,type(res))

④reduce+lambda改造

1 lst = [5,4,8,8]
2 res = reduce(lambda x,y:x*10+y,lst)
3 print(res)

<2>"789" -> 数字7 数字8 数字9

1 def func1(x,y):
2     return x*10 + y
3 def func2(n):
4     dic = {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9}
5     return dic[n]
6 it = map(func2,"789") # [7,8,9]
7 res = reduce(func1,it)
8 print(res,type(res))

(4)sorted

sorted(iterable,key=函数,reverse=False)
  功能:排序
  参数:
    iterable:可迭代型数据(容器类型数据,range对象,迭代器)
    key :指定自定义函数或内置函数
    reverse :代表升序或者降序 , 默认是升序(从小到大排序) reverse=False
  返回值:
    排序后的结果

<1>默认是从小到大排序

1 lst = [1,2,3,4,5,-90,-4,-1,100]
2 res = sorted(lst)
3 print(res)

<2>reverse 从大到小排序

1 lst = [1,2,3,4,5,-90,-4,-1,100]
2 res = sorted(lst,reverse=True)
3 print(res)

<3>指定函数进行排序

①按照绝对值排序 abs

1 lst = [-10,-1,3,5]
2 res = sorted(lst,key=abs)
3 print(res)

<4>使用自定义函数进行排序

1 lst = [19,21,38,43,55]
2 def func(n):
3     return n % 10
4 lst = sorted(lst,key=func)
5 print(lst)

<5>sorted 和 sort 之间的区别

①sorted可以排序一切容器类型数据, sort只能排列表
② sorted返回的是新列表,sort是基于原有的列表进行修改
③ 推荐使用sorted

原文地址:https://www.cnblogs.com/Magicianlx/p/13418836.html