{"id":1059,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python lambda","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1059.html","title":{"rendered":"Python lambda"},"content":{"rendered":"
\n
\u641e\u61c2Python lambda\u8868\u8fbe\u5f0f\u7684\u7528\u6cd5<\/span>\n <\/div>\n \u6a21\u677f\uff1a lambda argument: manipulate(argument)<\/span> \u53ef\u4ee5\u7acb\u5373\u4f20\u9012\uff08\u65e0\u9700\u53d8\u91cf\uff09<\/span> \u9700\u8981\u5904\u7406\u7684\u65b9\u6cd5\u662f\u5f88\u7b80\u5355\u7684(+1\uff0c\u5b57\u7b26\u4e32\u62fc\u63a5\u7b49),\u8be5\u51fd\u6570\u4e0d\u503c\u5f97\u62e5\u6709\u4e00\u4e2a\u540d\u5b57\u3002<\/span> <\/body>lambda\u662f\u4ec0\u4e48\uff1f<\/h2>\n
\n \u53c2\u6570\uff1aargument\u5c31\u662f\u8fd9\u4e2a\u533f\u540d\u51fd\u6570\u4f20\u5165\u7684\u53c2\u6570\uff0c\u5192\u53f7\u540e\u9762\u662f\u6211\u4eec\u5bf9\u8fd9\u4e2a\u53c2\u6570\u7684\u64cd\u4f5c\u65b9\u6cd5<\/span> <\/p>\n add_one = lambda <\/span>x:x+1 # 1\u4e2a\u53c2\u6570\uff0c\u6267\u884c\u64cd\u4f5c\u4e3a+1
<\/span> add_nums = lambda <\/span>x,y:x+y # 2\u4e2a\u53c2\u6570\uff0c\u6267\u884c\u64cd\u4f5c\u4e3a\u76f8\u52a0
<\/span> print(add_one(2)) # \u8c03\u7528add_one
<\/span> print(add_nums(3,7)) # \u8c03\u7528add_nums
<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n 3
10 <\/span><\/code><\/pre>\n<\/p><\/div>\nlambda\u7528\u6cd5\u8be6\u89e3<\/h2>\n
lambda + map<\/h3>\n
numbers = [1,2,3,4,5]
add_one = list(map(lambda <\/span>n:n+1,numbers)) #map(fun,sequence)
<\/span> print(list(add_one))
print(tuple(add_one))
<\/span><\/code><\/pre>\n<\/p><\/div>\n [2, 3, 4, 5, 6]
(2, 3, 4, 5, 6) <\/span><\/code><\/pre>\n<\/p><\/div>\n def <\/span>add(num):
return <\/span>num+1
numbers = [1,2,3,4,5]
add_one = list(map(add,numbers))
print(add_one)
print(tuple(add_one))
<\/span><\/code><\/pre>\n<\/p><\/div>\n from <\/span>datetime import <\/span>datetime as <\/span>dt
logs = ['serverLog'<\/span>,'appLog'<\/span>,'paymentLog'<\/span>]
format ='_{}.py'<\/span>.format<\/span>(dt.now<\/span>().strftime<\/span>('%d-%m-%y'<\/span>))
result =list(map(lambda <\/span>x:x+format,logs)) # \u5229\u7528map+lambda <\/span>\u5b9e\u73b0\u5b57\u7b26\u4e32\u62fc\u63a5
<\/span> print(result)
<\/span><\/code><\/pre>\n<\/p><\/div>\n ['serverLog_26-07-20.py', 'appLog_26-07-20.py', 'paymentLog_26-07-20.py'] <\/span><\/code><\/pre>\n<\/p><\/div>\n
person =[{'name'<\/span><\/span><\/span>:'XiaoMing'<\/span>,
'city'<\/span><\/span>:'beijing'<\/span>},
{'name':'XiaoLi'<\/span>,
'city':'shanghai'<\/span>}]
names=list(map(lambda <\/span>x:x['name'],person))
print(names)
<\/span><\/code><\/pre>\n<\/p><\/div>\n ['XiaoMing', 'XiaoLi'] <\/span><\/code><\/pre>\n<\/p><\/div>\n
lambda + filter<\/h3>\n
numbers = [0, 1, 2, -3, 5, -8, 13]
# \u63d0\u53d6\u5947\u6570
<\/span> result = filter(lambda <\/span>x: x % 2, numbers)
print(\"Odd Numbers are :\"<\/span>,list(result))
# \u63d0\u53d6\u5076\u6570
<\/span> result = filter(lambda <\/span>x: x % 2 == 0, numbers)
print(\"Even Numbers are :\"<\/span>,list(result))
# \u63d0\u53d6\u6b63\u6570
<\/span> result = filter(lambda <\/span>x: x>0, numbers)
print(\"Positive Numbers are :\"<\/span>,list(result))
<\/span><\/code><\/pre>\n<\/p><\/div>\n Odd Numbers are : [1, -3, 5, 13]
Even Numbers are : [0, 2, -8]
Positive Numbers are : [1, 2, 5, 13]
<\/span><\/code><\/pre>\n<\/p><\/div>\n person =[{'name'<\/span><\/span><\/span>:'XiaoMing'<\/span>,
'city'<\/span><\/span>:'beijing'<\/span>},
{'name':'XiaoLi'<\/span>,
'city':'shanghai'<\/span>}]
names=list(filter(lambda <\/span>x:x['name']=='XiaoMing'<\/span>,person))
print(names)
<\/span><\/code><\/pre>\n<\/p><\/div>\n [{'name': 'XiaoMing', 'city': 'beijing'}] <\/span><\/code><\/pre>\n<\/p><\/div>\n
lambda + reduce<\/h3>\n
from <\/span>functools import <\/span>reduce # Only Python 3
<\/span> numbers = [1,2,3,4]
result_multiply = reduce((lambda <\/span>x, y: x * y), numbers)
result_add = reduce((lambda <\/span>x,y: x+y), numbers)
print(result_multiply)
print(result_add)
<\/span><\/code><\/pre>\n<\/p><\/div>\n 24
10 <\/span><\/code><\/pre>\n<\/p><\/div>\n\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528lambda<\/h2>\n
\n \u53ea\u9700\u4e00\u884c\u4ee3\u7801\uff0c\u7b80\u6d01\uff08\u672a\u5fc5\u9ad8\u6548\uff09<\/span>
\n \u53ef\u4ee5\u4f1a\u81ea\u52a8\u8fd4\u56de\uff0c\u65e0\u9700return<\/span>
\n lambda\u51fd\u6570\u6ca1\u6709\u51fd\u6570\u540d\u79f0<\/span> <\/p>\n
\n \u4ece\u771f\u6b63\u9700\u6c42\u51fa\u53d1\uff0c\u6211\u4eec\u5728\u5927\u591a\u6570\u65f6\u5019\u662f\u4e0d\u9700\u8981lambda\u7684\uff0c\u56e0\u4e3a\u603b\u53ef\u4ee5\u627e\u5230\u66f4\u597d\u7684\u66ff\u4ee3\u65b9\u6cd5\uff0c<\/b>\u73b0\u5728\u6211\u4eec\u4e00\u8d77\u770b\u4e00\u4e0b\u521a\u624dlambda+reduce \u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u7528lambada\u5b9e\u73b0\u7684\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n from <\/span>functools import <\/span>reduce # Only Python 3
<\/span> numbers = [1,2,3,4]
result_multiply = reduce((lambda <\/span>x, y: x * y), numbers)
result_add = reduce((lambda <\/span>x,y: x+y), numbers)
<\/span><\/code><\/pre>\n<\/p><\/div>\n from <\/span>functools import <\/span>reduce
from <\/span>operator <\/span>import <\/span>mul
numbers = [1,2,3,4]
result_add = sum(numbers)
result_multiply =reduce(mul,numbers)
print(result_add)
print(result_multiply)
<\/span><\/code><\/pre>\n<\/p><\/div>\n 10
24 <\/span><\/code><\/pre>\n<\/p><\/div>\n colors = ['red'<\/span>,'purple'<\/span>,'green'<\/span>,'blue'<\/span>]
result = map(lambda <\/span>c:c.capitalize<\/span>(),colors)
print(list(result))
<\/span><\/code><\/pre>\n<\/p><\/div>\n ['Red', 'Purple', 'Green', 'Blue'] <\/span><\/code><\/pre>\n<\/p><\/div>\n
colors = ['red'<\/span>,'purple'<\/span>,'green'<\/span>,'blue'<\/span>]
result = [c.capitalize<\/span>() for <\/span><\/span>c in <\/span>colors]
print(result)
<\/span><\/code><\/pre>\n<\/p><\/div>\n ['Red', 'Purple', 'Green', 'Blue'] <\/span><\/code><\/pre>\n<\/p><\/div>\n
colors = ['Red'<\/span>,'purple'<\/span>,'Green'<\/span>,'blue'<\/span>]
print(sorted(colors,key=str.capitalize))
<\/span><\/code><\/pre>\n<\/p><\/div>\n ['Red', 'Purple', 'Green', 'Blue'] <\/span><\/code><\/pre>\n<\/p><\/div>\n
\u9002\u5408lambda\u7684\u573a\u666f<\/h2>\n
\n \u4f7f\u7528lambda\u8868\u8fbe\u5f0f\uff0c\u4f1a\u6bd4\u6211\u4eec\u80fd\u60f3\u5230\u7684\u51fd\u6570\u540d\u79f0\u66f4\u5bb9\u6613\u7406\u89e3\u3002<\/span>
\n \u9664\u4e86lambda\uff0c\u6ca1\u6709\u4efb\u4f55python\u63d0\u4f9b\u7684\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u76ee\u3002<\/span>
\n \u56e2\u961f\u4e2d\u6240\u6709\u6210\u5458\u90fd\u719f\u7ec3\u638c\u63e1\u4e86lambda\u8868\u8fbe\u5f0f\uff0c\u5e76\u4e14\u7eb3\u5165\u4e86\u56e2\u961f\u7684\u5f00\u53d1\u89c4\u8303\u3002<\/span> <\/p>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python lambdazh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1059","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1059"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1059"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1059\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}