码迷,mamicode.com
首页 > 其他好文 > 详细

匿名函数

时间:2019-12-09 21:54:14      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:red   eve   range   pre   values   val   ted   return   tool   

# 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

匿名函数

标签:red   eve   range   pre   values   val   ted   return   tool   

原文地址:https://www.cnblogs.com/0B0S/p/12013252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!