• 第23日python学习作用域 匿名函数 函数是编程


    map:处理序列中的每个元素,得到的是“list”,和处理前的“list”元素个数及位置原来一样()

    filter:遍历所有的元素得到一个bool值,如果是Turn则留下来

    reduce:处理一个序列,把序列进行合并操作

    只要是序列map  filter reduce都是适用的

    作用域:

    name="test"
    def test():
    name="test"
    def test1():
    name="test1"
    print(name)
    return test1

    a=test()#接收return返回的值test1的值,但是test1是函数名所以是函数的地址,把函数的地址赋值给a
    print(a)#输出test1的内存地址
    a()#a拿到地址在加()运行函数里面的内容

    #a()看上去输出的是全局变量的,但是之前定义在test1函数面

    test()() #——————>表示直接调用test里面的第二个函数

    补充:如果定义了多个嵌套的函数可以test()()()使用


    匿名函数:
    #使用函数
    name="test"
    def test(x):
    return x+"_good"

    f=test(name)
    print(f)

    #使用匿名函数
    f=lambda x: x+"_good"
    print(f(name))


    #以上2个结果是一样的


    编程的方法论:就像流派一样
    面向过程:按按流程来模拟整个过程,例如:谈恋爱,一步一步来
    函数式:函数式=编程语言定义的函数+数学意义的函数。先使用数学表达出来,在进行使用python编程出来。y=2*x+1
    面向对象:对函数进行分类和封装,让开发“更快更好更强...”


    #高阶函数:1.把函数当做参数传递给另一个函数  2、返回值中包含函数。中任意一点即是高阶函数

    def test():
    print("vevery good")

    def test1(name):
    print(name)
    return test
    test1(test())



    内置函数:map filter reduce

    map函数:
    对列表进行+ 1- *等操作。对列表中的值都操作一次,出来列表一样
    
    
    #把下面里表的值求平方

    # num_1=[1,2,10,5,7,9]
    # li=[]
    # for cheng in num_1:
    # test= cheng**2
    # li.append(test)
    # print(li)

    #假如1w个的值求平方
    # num_1=[1,2,10,5,7,9]
    # def test (args):
    # li=[]
    # for cheng in args:
    # li.append( cheng**2)
    # return li
    # ret=test(num_1)
    # print(ret)

    # #每个值都加1
    # num_1=[1,2,10,5,7,9]
    # def test (args):
    # li=[]
    # for cheng in args:
    # li.append( cheng+1)
    # return li
    # ret=test(num_1)
    # print(ret)


    # #同时操作对数据+1 -1 **等
    # num_1=[1,2,10,5,7,9]
    # def add_a(x):
    # return x+1
    #
    # def reduce(y):
    # return y-1
    #
    # def mi(z):
    # return z**2
    #
    # def map_test(fun,foo):
    # li=[]
    # for i in foo:#把传入的列表的值进行for循环操作
    # res=fun(i)
    # li.append(res)#把res处理好的值添加到li列表里面
    # return li#函数需要返回值,不然拿不到结果
    # ret1=map_test(add_a,num_1)#把函数当做参数传递给另一个函数,高级函数
    # print(ret1)


    #在进行缩减
    # lambda x:x+1
    #
    # lambda x:x-1
    #
    # lambda x:x**2
    #
    # num_1=[1,2,10,5,7,9]
    # def map_test(fun,foo):
    # li=[]
    # for i in foo:#把传入的列表的值进行for循环操作
    # res=fun(i)
    # li.append(res)#把res处理好的值添加到li列表里面
    # return li#函数需要返回值,不然拿不到结果
    # ret1=map_test(lambda x:x+1,num_1)#把函数当做参数传递给另一个函数,高级函数
    # print(ret1)



    num_1=[1,2,10,5,7,9]
    test=map(lambda x:x+1,num_1)
    print(test)#打印的是个地址

    test=map(lambda x:x+1,num_1)
    li=[]
    for i in test:
    li.append(i)
    print(li)
    # print(list(test))#正常打印
    # print(list(map(lambda x:x+1,num_1)))
     
    filter 函数:对列表进行字符筛选,帅选功能
    # #使用for循环
    # movie=["zhangshang","sb_lisi","sb_waner","mazi"]
    # li=[]
    # for i in movie:
    # if not i.startswith("sb"):#去除开头包含sb的内容
    # li.append(i)
    # print(li)

    #使用函数
    # movie=["zhangshang","sb_lisi","sb_waner","mazi"]
    # def foo(arry):
    # li=[]
    # for i in arry:
    # if not i.startswith("sb"):#去除开头包含sb的内容
    # li.append(i)
    # return li
    # test=foo(movie)
    # print(test)
    # #以上是事项固定逻辑


    movie1=["zhangshang","lisis_sb","waner_sb","mazi"]
    def sb_show(n):
    return n.endswith("sb")

    def foo(arry,movie):
    li=[]
    for i in movie:
    if not arry(i):
    li.append(i)
    return li

    test=foo(sb_show,movie1)
    print(test)


    # 终极版:
    movie1=["zhangshang","lisis_sb","waner_sb","mazi"]
    test=filter(lambda n:not n.endswith("sb"),movie1)
    print(list(test))

    reduce 函数:
    #需求把下列列表中的值加起来
    #for循环
    # num=[1,3,4,7,8,10]
    # res=0
    # for i in num:
    # res=i+res
    # print(res)
    #
    # #函数
    # num=[1,3,4,7,8,10]
    # def test(num1):
    # res=0
    # for i in num1:
    # res=i+res
    # return res
    # test=test(num)
    # print(test)

    #相加
    def test(x,y):
    return x+y

    lambda x,y:x+y

    from functools import reduce#导入函数reduce
    num=[1,3,4,7,8,10]
    test=reduce(lambda x,y:x+y ,num,10)#最后的10是指定初始值,可以不指定
    print(test)
    print(reduce(lambda x,y:x+y ,num,10))
     






  • 相关阅读:
    Java动态代理设计模式
    AOP的相关概念
    如何解决表单提交的中文乱码问题
    怎么防止重复提交
    http的响应码200,404,302,500表示的含义分别是?
    JSP三大指令是什么?
    说一下 session 的工作原理?
    session 和 cookie 有什么区别?
    说一下 JSP 的 4 种作用域?
    jsp有哪些内置对象?作用分别是什么?
  • 原文地址:https://www.cnblogs.com/jianchixuexu/p/11521577.html
Copyright © 2020-2023  润新知