zoukankan      html  css  js  c++  java
  • 匿名函数

    calc = lambda n:n**2
    print(calc(10))
    add = lambda x,y:x+y
    dic = {'k1':10,'k2':100,'k3':20}
    # def func(key):
    #     return dic[key]
    # 当key参数不为空时,就以key的函数对象为判断的标准
    print(max(dic,key=lambda k: dic[k]))

     带key的内置函数:

    min,max,

    filter,map,sorted

    都有key参数,都可以和lambda合作

    a = (('a'),('b'))
    b = (('c'),('d'))
    ret = zip(a,b)
    def func(tup):
        return {tup[0]:tup[1]}
    map(func,ret)
    for i in ret:
        print(i)
    # 匿名函数一般和常用内置函数相结合
    # max min map filter sorted
    a = (('a'),('b'))
    b = (('c'),('d'))
    ret = zip(a,b)
    res = map(lambda tup: {tup[0]:tup[1]},ret)
    print(list(res))

     

  • 相关阅读:
    C#基础
    自动化测试
    C# 数据结构题目
    .NET基础知识
    Sharepoint题目
    题目总结2
    数据库索引
    题目总结(2014-1-10)
    Stack详解
    SpringBoot入门基础知识点
  • 原文地址:https://www.cnblogs.com/shuoran/p/11663765.html
Copyright © 2011-2022 走看看