• Python_匿名函数_47


    匿名函数

    Eva_J

    匿名函数:为了解决那些功能很简单的需求而设计的一句话函数

    复制代码
    #这段代码
    def calc(n):
        return n**n
    print(calc(10))
     
    #换成匿名函数
    calc = lambda n:n**n
    print(calc(10))
    复制代码

    上面是我们对calc这个匿名函数的分析,下面给出了一个关于匿名函数格式的说明

    函数名 = lambda 参数 :返回值
    
    #参数可以有多个,用逗号隔开
    #匿名函数不管逻辑多复杂,只能写一行,且逻辑执行结束后的内容就是返回值
    #返回值和正常的函数一样可以是任意数据类型

    我们可以看出,匿名函数并不是真的不能有名字。

    匿名函数的调用和正常的调用也没有什么分别。 就是 函数名(参数) 就可以了~~~

    请把以下函数变成匿名函数
    def add(x,y):
        return x+y
    
    add = lambda x,y:x+y
    print(add(1,2))

    上面是匿名函数的函数用法。除此之外,匿名函数也不是浪得虚名,它真的可以匿名。在和其他功能函数合作的时候

    l=[3,2,100,999,213,1111,31121,333]
    print(max(l))
    
    
    dic={'k1':10,'k2':100,'k3':30} 
    print(max(dic)) # k3 #默认取的是key的最大值
    
    
    dic={'k1':10,'k2':100,'k3':30}
    def func(key):
        return dic[key]
    print(max(dic,key=func))   #根据返回值判断最大值,返回值最大的那个参数是结果
    
    
    print(dic[max(dic,key=lambda k:dic[k])])

    dic={'k1':10,'k2':100,'k3':30}
    def func(key):
    return dic[key]
    print(max(dic,key=func)) #根据返回值判断最大值,返回值最大的那个参数是结果

    dic={'k1':10,'k2':100,'k3':30}
    def func(key):

    return max(key)
    print(func(dic.values()))
    res = map(lambda x:x**2,[1,5,7,4,8])
    for i in res:
        print(i)
    
    输出
    25
    16
    res = filter(lambda x:x>10,[5,8,11,9,15])
    for i in res:
        print(i)
    
    输出
    15

    面试题:

    1.下面程序的输出结果是:
    d = lambda p:p*2
    t = lambda p:p*3
    x = 2
    x = d(x)
    x = t(x)
    x = d(x)
    print x
    
    2.现有两元组(('a'),('b')),(('c'),('d')),请使用python中匿名函数生成列表[{'a':'c'},{'b':'d'}]
    
    3.以下代码的输出是什么?请给出答案并解释。
    def multipliers():
        return [lambda x:i*x for i in range(4)]
    print([m(2) for m in multipliers()])
    请修改multipliers的定义来产生期望的结果。
    
    练习
    max([1,2,3,4,5,-6,-7],key=abs)
    
    ret = map(abs,[-1,2,-3,4])
    for i in ret:
        print(i)
    
    def func(x):
        return x**2
    ret = map(func,[-1,2,-3,4])
    for i in ret:
        print(i)
    
    ret = map(lambda x:x**2,[-1,2,-3,4])
    
    
    def func(x):
        return x>10
    
    res = filter(func,[5,8,11,9,15])
    for i in res:
        print(i)
    
    
    # min max filter map sorted —— lambda

     面试题:

    1.下面程序的输出结果是:
    d = lambda p:p*2
    t = lambda p:p*3
    x = 2
    x = d(x) 
    x = t(x)
    x = d(x)
    print x #24
    
    2.现有两元组(('a'),('b')),(('c'),('d')),请使用python中匿名函数生成列表[{'a':'c'},{'b':'d'}]
    ret = zip((('a'),('b')),(('c'),('d')))
    print(ret)
    for i in ret:
        print(i)
    
    ret = zip((('a'), ('b')), (('c'), ('d')))
    def func(tup):
        return {tup[0]:tup[1]}
    res = map(func, ret)
    print(list(res))
    
    ret = zip((('a'), ('b')), (('c'), ('d')))
    ret = map(lambda t:{t[0]:t[1]},ret)
    print(list(ret))

    print(list(map(lambda t:{t[0]:t[1]},zip((('a'), ('b')), (('c'), ('d'))))))
    
    
    


    3.以下代码的输出是什么?请给出答案并解释。 def multipliers(): return [lambda x:i*x for i in range(4)] print([m(2) for m in multipliers()]) 请修改multipliers的定义来产生期望的结果。
    def multipliers():
        return [lambda x:i*x for i in range(4)]
    print([m(2) for m in multipliers()])
    
    def multipliers():
        return (lambda x:i*x for i in range(4))
    print([m(2) for m in multipliers()])
    
    
    # [6, 6, 6, 6]
    # [0, 2, 4, 6]
    
    
  • 相关阅读:
    AX7 VM can not starting
    AX3空Invoice明细问题
    Solution to “VirtualBox can't operate in VMX root mode” error in Windows 7
    Inventory of the materials to teach you how to query a date certain combination of dimensions
    How to Debug Enterprise Portal Code in Dynamics AX 2009
    Axapta 3 COM Connector
    AX 与Citrix打印机问题
    AX ERP 真正的自动批处理
    SQL语句转摘
    D365: Table, Form, Class to extension
  • 原文地址:https://www.cnblogs.com/LXL616/p/10699472.html
Copyright © 2020-2023  润新知