• 16-python-map、filter函数


    1、map函数  映射函数

      引用:

     1 # -*- coding:utf-8 -*-
     2 
     3 num_1=[1,2,3,4,5,6,7,8]
     4 
     5 def map_test(array):   #数值的平方运算
     6     res=[]
     7     for i in array:
     8         res.append(i**2)
     9     return res
    10 
    11 print(map_test(num_1))

    函数应用1(初级版本)

     1 # -*- coding:utf-8 -*-
     2 
     3 num_1=[1,2,3,4,5,6,7,8]
     4 def add_one(x):   #函数算法
     5     return x+1
     6 def reduce_one(x):
     7     return x-1
     8 def squ_two(x):
     9     return x**2
    10 
    11 def map_test(func,array):   #运算函数
    12     res=[]
    13     for i in array:
    14         res.append(func(i))
    15     return res
    16 
    17 print(map_test(squ_two,num_1))   #高级函数
    18 print(map_test(add_one,num_1))
    19 print(map_test(reduce_one,num_1))

    函数应用2(终极版)

    1 num_1=[1,2,3,4,5,6,7,8] 

    2 def map_test(func,array): 3 res=[] 4 for i in array: 5 res.append(func(i)) 6 return res 7 8 print(map_test(lambda x:x**2,num_1)) #直接应用匿名函数 对num_1的运算,想要什么运算就用过匿名函数输入任一计算公式

      函数应用2就写活了。

      map函数的应用

    1 # -*- coding:utf-8 -*-
    2 num_1=[1,2,3,4,5,6,7,8]
    3 res=map(lambda x:x**2,num_1)   #  第一个参数是计算公式或自己定义的计算函数,   第二个参数是可迭代对象。  
    4 print(list(res))

    2、filter函数  过滤函数

      引用:

    1 # -*- coding:utf-8 -*-
    2 
    3 movie_people=["sb_1","sb_2","sb_3","zf"]
    4 
    5 res=[]
    6 for p in movie_people:    # 删除以SB开头的名字
    7     if not p.startswith("sb"):
    8         res.append(p)
    9 print(res)

    函数应用1

     1 # -*- coding:utf-8 -*-
     2 
     3 movie_people=["sb_1","sb_2","sb_3","zf"]
     4 
     5 def filter_test(array):
     6     res = []
     7     for p in array:  # 删除以SB开头的名字
     8         if not p.startswith("sb"):
     9             res.append(p)
    10     return res
    11 
    12 print(filter_test(movie_people))

    函数应用2

     1 # -*- coding:utf-8 -*-
     2 
     3 movie_people=["sb_1","sb_2","sb_3","zf"]
     4 movie_people_1=["1_sb","2_sb","3_sb","zf"]
     5 
     6 def sb_show(n):
     7     return n.endswith("sb")
     8 
     9 def filter_test(func,array):
    10     res = []
    11     for p in array:  # 删除以SB结尾的名字
    12         if not func(p):
    13             res.append(p)
    14     return res
    15 
    16 print(filter_test(sb_show,movie_people_1))

    函数应用3(终极版本)

     1 # -*- coding:utf-8 -*-
     2 
     3 movie_people=["sb_1","sb_2","sb_3","zf"]
     4 movie_people_1=["1_sb","2_sb","3_sb","zf"]
     5 
     6 
     7 def filter_test(func,array):
     8     res = []
     9     for p in array:  # 删除以SB开头的名字
    10         if not func(p):
    11             res.append(p)
    12     return res
    13 
    14 print(filter_test(lambda n:n.endswith("sb"),movie_people_1))

      filter函数(过滤)

    filter()

    1 # -*- coding:utf-8 -*-
    2 
    3 movie_people = ["sb_1", "sb_2", "sb_3", "zf"]
    4 movie_people_1 = ["1_sb", "2_sb", "3_sb", "zf"]
    5 
    6 res = filter(lambda n: not n.endswith("sb"), movie_people_1)
    7 print(list(res))

    3、reduce函数

    引用: 

    1 # -*- coding:utf-8 -*-
    2 
    3 num_1=[1,2,3,4,5,6,7,11,12]
    4 
    5 temp=0
    6 for num in num_1:   #求和
    7     temp=temp+num
    8 
    9 print(temp)

    函数应用1

     1 # -*- coding:utf-8 -*-
     2 
     3 num_1 = [1, 2, 3, 4, 5, 6, 7, 11, 12]
     4 
     5 
     6 def reduce_test(array):
     7     temp = 0
     8     for num in array:
     9         temp = temp + num
    10     return temp
    11 
    12 
    13 print(reduce_test(num_1))

    函数应用2

     1 # -*- coding:utf-8 -*-
     2 
     3 num_1 = [1, 2, 3, 4, 5, 6, 7, 11, 12]
     4 
     5 def reduce_test(func,array):
     6     temp = array.pop(0)
     7     for num in array:
     8         temp=func(temp,num)
     9     return temp
    10 
    11 
    12 print(reduce_test(lambda x,y:x*y,num_1))

    reduce()函数,多个数据经过计算合成一个

    1 # -*- coding:utf-8 -*-
    2 
    3 from functools import reduce
    4 
    5 num_1 = [1, 2, 3, 4, 5, 6, 7, 11, 12]
    6 
    7 s = reduce(lambda x, y: x * y, num_1, 1)
    8 
    9 print(s)
  • 相关阅读:
    Jenkins的安装及使用(一)
    使用 scm-manager 搭建 git/svn 代码管理仓库(二)
    使用 scm-manager 搭建 git/svn 代码管理仓库(一)
    异步处理程序
    观察者模式之烧开水
    Java初转型-SSM配置文件
    Java初转型-Tomcat安装和配置
    Java初转型-MavenWEB项目搭建
    安装 vs2005, vs2008 报错
    SQL Server 数据库邮件 配置
  • 原文地址:https://www.cnblogs.com/zhfang/p/8729537.html
Copyright © 2020-2023  润新知