{"id":168420,"date":"2022-04-18T04:54:58","date_gmt":"2022-04-17T20:54:58","guid":{"rendered":"http:\/\/www.idc.net\/help\/168420\/"},"modified":"2022-04-18T04:54:58","modified_gmt":"2022-04-17T20:54:58","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython%e4%b9%8blambda%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/168420\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1aPython\u4e4blambda\u8868\u8fbe\u5f0f"},"content":{"rendered":"<p><span style=\"font-size: 18px\"><strong><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/article\/000\/000\/003\/5d1dcdad4ce06281.jpg\" class=\"aligncenter\"><\/strong><\/span><\/p>\n<p><span style=\"font-size: 18px\"><strong>lambda\u8868\u8fbe\u5f0f\u683c\u5f0f\u4ee5\u53ca\u5e94\u7528\u573a\u666f<\/strong><\/span><\/p>\n<p><strong>1\u3001lambda\u51fd\u6570\u4e0elist\u7684\u7ed3\u5408\u4f7f\u7528<\/strong><\/p>\n<pre>list&nbsp;=&nbsp;lambda:x&nbsp;for&nbsp;x&nbsp;in&nbsp;range(10)\nprint&nbsp;(list[0])\n&gt;&gt;&gt;9\nlist&nbsp;=&nbsp;lambda&nbsp;x:x&nbsp;for&nbsp;x&nbsp;in&nbsp;range(10)\nprint&nbsp;(list[0])\n&gt;&gt;&gt;0<\/pre>\n<p><strong>2\u3001map,filter,reduce\u51fd\u6570<\/strong><\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre>a&nbsp;=&nbsp;[('a',1),('b',2),('c',3),('d',4)]\na_1&nbsp;=&nbsp;list(map(lambda&nbsp;x:x[0],a))<\/pre>\n<p>\u5982\u4e0a\u4f8b\u5b50\uff0cmap\u51fd\u6570\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2alambda\u8868\u8fbe\u5f0f\uff0c\u8f93\u5165\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8fd4\u56de\u8be5\u5bf9\u8c61\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u7b2c\u4e8c\u4e2a\u5c31\u662f\u9700\u8981\u4f5c\u7528\u7684\u5bf9\u8c61\uff0c\u6b64\u5904\u662f\u4e00\u4e2a\u5217\u8868\u3002Python3\u4e2dmap\u8fd4\u56de\u4e00\u4e2amap\u5bf9\u8c61\uff0c\u6211\u4eec\u9700\u8981\u4eba\u5de5\u8f6c\u4e3alist\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u5c31\u662f[\u2018a\u2019,\u2019b\u2019,\u2019c\u2019,\u2019d\u2019]&nbsp;<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre>a&nbsp;=&nbsp;[1,2,3,4]\nb&nbsp;=&nbsp;[2,3,4,5]\na_1&nbsp;=&nbsp;list(map(lambda&nbsp;x,y:x+y,a,b))<\/pre>\n<p>\u4e0a\u8fb9\u8fd9\u4e2a\u4f8b\u5b50\u662f\u4e3a\u4e86\u8bf4\u660e,lambda\u8868\u8fbe\u5f0f\u53c2\u6570\u53ef\u4ee5\u662f\u591a\u4e2a\u3002\u8fd4\u56de\u7ed3\u679c\u662f[3,5,7,9]<\/p>\n<p>\u76f8\u5173\u63a8\u8350\uff1a\u300aPython\u89c6\u9891\u6559\u7a0b\u300b<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre>a&nbsp;=&nbsp;[1,2,3,4,5,6,7]\na_1&nbsp;=&nbsp;filter(lambda&nbsp;x:x&lt;4,a)<\/pre>\n<p>\u5982\u4e0a\u4f8b\u5b50\uff0c\u5b9a\u4e49lambda\u8868\u8fbe\u5f0f\uff0c\u7b5b\u9009a\u5217\u8868\u4e2d\u5c0f\u4e8e4\u7684\u5143\u7d20\uff0c\u7ed3\u679c\u4e3a[1,2,3]\u3002filter\u51fd\u6570\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u65e0\u9700\u518d\u8fdb\u884c\u8f6c\u6362\uff0c\u7b2c\u4e09\u4e2a\u662f\u521d\u59cb\u503c\uff0c\u6211\u4eec\u6ca1\u7ed9\u521d\u59cb\u503c\uff0c\u90a3\u4e48\u5f00\u59cb\u64cd\u4f5c\u7684\u4e24\u4e2a\u5143\u7d20\u5c31\u662f\u5e8f\u5217\u7684\u524d\u4e24\u4e2a\u3002\u5426\u5219\u5c06\u4f7f\u7528\u6211\u4eec\u7ed9\u51fa\u7684\u521d\u59cb\u503c\u548c\u5e8f\u5217\u7b2c\u4e00\u4e2a\u5143\u7d20\u64cd\u4f5c\uff0c\u7136\u540e\u7ed3\u679c\u518d\u4e0e\u7b2c\u4e09\u4e2a\u5143\u7d20\u64cd\u4f5c\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u4e0a\u4e2a\u4f8b\u5b50\u7ed3\u679c\u662f28<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre>from&nbsp;functools&nbsp;import&nbsp;reduce&nbsp;#python3\u9700\u8981\u5bfc\u5165\u6b64\u6a21\u5757\na&nbsp;=&nbsp;[1,2,3,4,5,6,7]\na_1&nbsp;=&nbsp;reduce(lambda&nbsp;x,y:x+y,a)<\/pre>\n<p>reduce\u4e2d\u4f7f\u7528\u7684lambda\u8868\u8fbe\u5f0f\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0creduce\u51fd\u6570\u5171\u4e09\u4e2a\u53c2\u6570\u3002<\/p>\n<p>\u7b2c\u4e00\u4e2a\u662f\u5c31\u662flambda\u8868\u8fbe\u5f0f\uff0c\u7b2c\u4e8c\u4e2a\u662f\u8981\u7d2f\u8ba1\u7684\u5e8f\u5217\uff0c\u7b2c\u4e09\u4e2a\u662f\u521d\u59cb\u503c\uff0c\u6211\u4eec\u6ca1\u7ed9\u521d\u59cb\u503c\uff0c\u90a3\u4e48\u5f00\u59cb\u64cd\u4f5c\u7684\u4e24\u4e2a\u5143\u7d20\u5c31\u662f\u5e8f\u5217\u7684\u524d\u4e24\u4e2a\u3002\u5426\u5219\u5c06\u4f7f\u7528\u6211\u4eec\u7ed9\u51fa\u7684\u521d\u59cb\u503c\u548c\u5e8f\u5217\u7b2c\u4e00\u4e2a\u5143\u7d20\u64cd\u4f5c\uff0c\u7136\u540e\u7ed3\u679c\u518d\u4e0e\u7b2c\u4e09\u4e2a\u5143\u7d20\u64cd\u4f5c\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u4e0a\u4e2a\u4f8b\u5b50\u7ed3\u679c\u662f28\u3002<\/p>\n<p><strong>3\u3001\u5b57\u5178\u591a\u6761\u4ef6\u6392\u5e8f<\/strong><\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre>dict&nbsp;=&nbsp;{'a':1,'b':2,'c':3,'d':4,'e':3,'f':1,'g':7}\nsorted_dict_asc&nbsp;=&nbsp;sorted(dict.items(),key=lambda&nbsp;item:item[0])\nsorted_dict_dsc&nbsp;=&nbsp;sorted(dict.items(),key=lambda&nbsp;item:item[0],reverse=True)<\/pre>\n<p>\u8f93\u51fa\uff08\u7b2c\u4e00\u4e2a\u5347\u5e8f\uff0c\u7b2c\u4e8c\u4e2a\u964d\u5e8f\uff09\uff1a<\/p>\n<pre>[('a',&nbsp;1),&nbsp;('b',&nbsp;2),&nbsp;('c',&nbsp;3),&nbsp;('d',&nbsp;4),&nbsp;('e',&nbsp;3),&nbsp;('f',&nbsp;1),&nbsp;('g',&nbsp;7)]\n[('g',&nbsp;7),&nbsp;('f',&nbsp;1),&nbsp;('e',&nbsp;3),&nbsp;('d',&nbsp;4),&nbsp;('c',&nbsp;3),&nbsp;('b',&nbsp;2),&nbsp;('a',&nbsp;1)]]<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>lambda\u8868\u8fbe\u5f0f\u683c\u5f0f\u4ee5\u53ca\u5e94\u7528\u573a\u666f 1\u3001lambda\u51fd\u6570\u4e0elist\u7684\u7ed3\u5408\u4f7f\u7528 list&nbsp;=&#038;nbsp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":168421,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-168420","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/168420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=168420"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/168420\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/168421"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=168420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=168420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=168420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}