zoukankan      html  css  js  c++  java
  • Python day10 global关键字、函数递归、匿名函数、map函数的用法详解

    1.global关键字  引用全局变量,在局部全局变量改变,也会改变,global相当于指针,将地址指向全局变量的name
    name='littlepage'
    
    def littepage():
        global name
        name='LargePage'
        return name
    
    print(littepage())
    print(name)

    2.函数递归————举例,阶乘factorial
    def factorial(x):
        if(x==1):return x
        else:return x*factorial(x-1)
    
    print(factorial(4))
    
    

    一行阶乘(使用到3中的匿名函数)

    func=lambda x:x*func(x-1) if (x>1)  else 1
    print(func(9))
    3.匿名函数(anonymous function)
    def calc(x):
        return x+1
    等同于
    func=lambda x:x+1
    
    print(calc(1))
    print(func(1))
     
    4.map函数——映射函数
    map(func,liter1)
    原理解析
    def map_test(func,array):
        ret=[]
        for i in array:
            res=func(i)
            ret.append(res)
        return ret
    
    fun1=lambda x:x+1
    arr=[1,2,3,4,5]
    re=map_test(fun1,arr)
    print(re)
    
    def fun2(x):
        return x-1;
    
    re=map_test(fun2,arr)
    print(re)
    系统中有同样的函数,map函数,返回的是object地址,要进行遍历
    for i in map(lambda x:x+1,arr):
        print(i)
    同样的例子,让字符串转大写,如果不想要层级打印也可转为list
    msg="lllddddsciffbo"
    print(list(map(lambda x:x.upper(),msg)))
     
  • 相关阅读:
    HDU 1950 Bridging signals(LIS)
    PKU 1094 Sorting It All Out(拓扑排序)
    中国剩余定理(孙子定理)详解
    51Nod 1079
    翻转游戏
    不构造树的情况下验证先序遍历
    图说流程管理
    从架构到流程
    POS(Plan Operation Support 和 OES(Operation Enable Support)
    流程规划方法→POS法
  • 原文地址:https://www.cnblogs.com/littlepage/p/9384377.html
Copyright © 2011-2022 走看看