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

    # def my_sum(x,y):
    # return x+y
    # lambda x,y:x+y
    # print((lambda x,y:x+y)(1,2)) # 3
    # salaries={
    # 'siry':3000,
    # 'tom':7000,
    # 'lili':10000,
    # 'jack':2000
    # }

    # zip()
    # # print(max(salaries)) # tom
    # s='hello'
    # l=[1,2,3,4]
    # g=zip(s,l)
    # # print(g) # <zip object at 0x000001EBE6AE01C8>
    # # print(list(g)) # [('h', 1), ('e', 2), ('l', 3), ('l', 4)]

    # g=zip(salaries.values(),salaries.keys())
    # print(list(g)) # [(3000, 'siry'), (7000, 'tom'), (10000, 'lili'), (2000, 'jack')]

    # def func(k):
    # return salaries[k]
    # max(salaries,key=func)
    # print(max(salaries,key=func)) # lili
    # print(max(salaries,key=lambda k:salaries[k])) # lili
    # print(min(salaries,key=lambda k:salaries[k])) # jack

    # sorted()
    # print(sorted(salaries)) # ['jack', 'lili', 'siry', 'tom']
    # print(sorted(salaries,key=lambda k:salaries[k])) # ['jack', 'siry', 'tom', 'lili']
    # print(sorted(salaries,key=lambda k:salaries[k],reverse=True)) # ['lili', 'tom', 'siry', 'jack']
    # names=['One','Two','Three']
    # l=[]
    # for name in names:
    # res='%s_love'%name
    # l.append(res)
    #
    # print(l) # ['One_love', 'Two_love', 'Three_love']

    # map()
    # g=map(lambda name:'%s_love'%name,names)
    # # print(g) # <map object at 0x0000027316202F08>
    # print(list(g)) # ['One_love', 'Two_love', 'Three_love']

    # filter()
    # names=['One_love', 'Two_love', 'Three_love','egon']
    # g=filter(lambda x:x.endswith('love'),names)
    # # print(g) # <filter object at 0x0000021F63392F48>
    # print(list(g)) # ['One_love', 'Two_love', 'Three_love']

    # reduce()
    # from functools import reduce
    # g=reduce(lambda x,y:x+y,range(1,101))
    # print(g) # 5050

    # from functools import reduce
    # g=reduce(lambda x,y:x+y,range(1,101),100)
    # print(g) # 5150
  • 相关阅读:
    vue项目搭建过程2 -- 使用 vue cli 4.0 搭建 vue 项目
    vue项目搭建过程1 -- 环境搭建
    升级node.js版本
    git的初步了解
    期末总结
    四则运算的封装
    用户故事
    0~10的随机整数运算
    创业近一年在博客园总结一下,希望给来者一点借鉴
    PV与并发之间换算的算法换算公式
  • 原文地址:https://www.cnblogs.com/0B0S/p/12013252.html
Copyright © 2011-2022 走看看