• 2018年12月12日 函数4 函数式编程


    流程导图网站

    https://www.processon.com/

    函数式编程

    函数即变量

    编程流派:

    1.面向过程:找到解决问题的入口,按照一个固定的流程去模拟解决问题的流程
    2.函数体:编程语言定义的函数+数学意义的函数 
      高阶函数:a.函数接收的参数是一个函数名;b.返回值中包含函数
      尾调用优化:在函数最后一步调用另外一个函数(最后一行不一定是函数最后一步)

    3.面向对象

    def bar():
        print("bar")
    
    def foo():
        print("foo")
        return bar#返回bar的内存地址
    
    foo()()
    
    def handle():
        print("handle")
        return handle #也可以返回自己的内存地址
    handle()()()()()()

     尾调用:最后一步去调用别的函数

      

    l_s=[1,2,3,4,5]
    
    head,tail,s,*a=1_s 将序列分割成 第一个,第二个,第三个和剩下组成序列 
    print(head,tail,s,a)

    l_s=[1,2,3,4,5]
    print(*l_s)
    print(l_s)
    #注意这里*是遍历的意思

    1 2 3 4 5

    [1, 2, 3, 4, 5]

    map 函数

    _s=[1,2,4,5,3,4,3,2]
    
    def add_one (x):#+1操作
        return x+1
    Add_one=lambda x:x+1
    
    def reduce_one(x):#-1操作
        return x-1
    Reduce_one = lambda  x : x-1
    
    def map_test(func,num):
        ret=[]
        for i in num:
            res=func(i)
            ret.append(res)
        return ret
    
    w=map_test(add_one,l_s)# 第一个为传递了函数的方法,注意函数名作参数,第二个为需要 操作的数据
    w2=map_test(Add_one,l_s)
    print (w,'
    ',w2)

    w=map_test(add_one,l_s)# 第一个为传递了函数的方法,注意函数名作参数,第二个为需要 操作的数据
    w2=map_test(Add_one,l_s)
    print (w,'
    ',w2)
    a=map(lambda x:x+1,l_s) #可迭代类型,可以使用for循环遍历,map是个迭代器只能使用1次
    print("内置map结果:",list(a))#list 传递可迭代对象则生成列表
    
    b=map(reduce_one,l_s)
    print("内置map传递有名函数",list(b))

    map函数详解

    http://www.runoob.com/python/python-func-map.html

    filter函数

  • 相关阅读:
    DHCP脚本
    7.31
    7.30
    7.26
    7.24
    VLAN与三层交换机
    静态路由配置
    7.17
    四次挥手
    TCP三次握手,四次挥手
  • 原文地址:https://www.cnblogs.com/python1988/p/10106387.html
Copyright © 2020-2023  润新知