{"id":2763,"date":"2025-03-14T22:47:07","date_gmt":"2025-03-14T14:47:07","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2763"},"modified":"2025-03-14T22:47:07","modified_gmt":"2025-03-14T14:47:07","slug":"python-lambda%ef%bc%88%e5%8c%bf%e5%90%8d%e5%87%bd%e6%95%b0%ef%bc%89%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/14\/python-lambda%ef%bc%88%e5%8c%bf%e5%90%8d%e5%87%bd%e6%95%b0%ef%bc%89%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Python Lambda\uff08\u533f\u540d\u51fd\u6570\uff09\u8be6\u89e3"},"content":{"rendered":"\n<p>Lambda \u51fd\u6570\uff0c\u4e5f\u79f0\u4e3a\u533f\u540d\u51fd\u6570\uff0c\u662f\u4e00\u79cd\u5728 Python \u4e2d\u5b9a\u4e49\u7b80\u6d01\u51fd\u6570\u7684\u65b9\u5f0f\u3002\u4e0e\u5e38\u89c4\u7684 <code>def<\/code> \u5b9a\u4e49\u51fd\u6570\u4e0d\u540c\uff0c<code>lambda<\/code> \u662f\u4e00\u4e2a\u5355\u884c\u51fd\u6570\uff0c\u5e38\u7528\u4e8e\u7b80\u77ed\u7684\u3001\u4e00\u6b21\u6027\u7684\u529f\u80fd\u573a\u666f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u57fa\u672c\u8bed\u6cd5<\/strong><\/h2>\n\n\n\n<p>Lambda \u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lambda \u53c2\u65701, \u53c2\u65702, ... : \u8868\u8fbe\u5f0f<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>lambda<\/code> \u662f\u5173\u952e\u8bcd\u3002<\/li>\n\n\n\n<li><code>\u53c2\u65701, \u53c2\u65702, ...<\/code> \u662f\u51fd\u6570\u7684\u8f93\u5165\u53c2\u6570\uff0c\u53ef\u4ee5\u6709\u591a\u4e2a\u53c2\u6570\u3002<\/li>\n\n\n\n<li><code>\u8868\u8fbe\u5f0f<\/code> \u662f\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u8868\u8fbe\u5f0f\u7684\u503c\u5c06\u4f5c\u4e3a\u51fd\u6570\u7684\u8f93\u51fa\u3002<\/li>\n<\/ul>\n\n\n\n<p>Lambda \u51fd\u6570\u4e0d\u9700\u8981\u663e\u5f0f\u7684 <code>return<\/code> \u8bed\u53e5\uff0c\u56e0\u4e3a\u5b83\u4f1a\u81ea\u52a8\u8fd4\u56de\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b 1\uff1a\u6700\u7b80\u5355\u7684 Lambda \u51fd\u6570<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>add = lambda x, y: x + y\nprint(add(3, 5))  # \u8f93\u51fa 8<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Lambda \u51fd\u6570\u7684\u5e94\u7528\u573a\u666f<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u7528\u4e8e\u51fd\u6570\u5f0f\u7f16\u7a0b<\/strong><\/h3>\n\n\n\n<p>Lambda \u51fd\u6570\u901a\u5e38\u4e0e <code>map()<\/code>\u3001<code>filter()<\/code>\u3001<code>reduce()<\/code> \u7b49\u51fd\u6570\u914d\u5408\u4f7f\u7528\uff0c\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u8f6c\u6362\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b 2\uff1a\u4e0e <code>map()<\/code> \u914d\u5408\u4f7f\u7528<\/strong><\/h4>\n\n\n\n<p><code>map()<\/code> \u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u548c\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u8fd4\u56de\u4e00\u4e2a\u5c06\u51fd\u6570\u5e94\u7528\u5230\u6bcf\u4e2a\u5143\u7d20\u540e\u7684\u65b0\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers = &#91;1, 2, 3, 4]\nsquared = map(lambda x: x ** 2, numbers)\nprint(list(squared))  # \u8f93\u51fa &#91;1, 4, 9, 16]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b 3\uff1a\u4e0e <code>filter()<\/code> \u914d\u5408\u4f7f\u7528<\/strong><\/h4>\n\n\n\n<p><code>filter()<\/code> \u51fd\u6570\u6839\u636e\u7ed9\u5b9a\u7684\u51fd\u6570\u548c\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u8fd4\u56de\u4e00\u4e2a\u8fc7\u6ee4\u540e\u7684\u5143\u7d20\u96c6\u5408\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers = &#91;1, 2, 3, 4, 5, 6]\neven_numbers = filter(lambda x: x % 2 == 0, numbers)\nprint(list(even_numbers))  # \u8f93\u51fa &#91;2, 4, 6]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b 4\uff1a\u4e0e <code>reduce()<\/code> \u914d\u5408\u4f7f\u7528<\/strong><\/h4>\n\n\n\n<p><code>reduce()<\/code> \u51fd\u6570\u5c06\u4e00\u4e2a\u4e8c\u5143\u51fd\u6570\u5e94\u7528\u4e8e\u5e8f\u5217\u7684\u6bcf\u4e00\u9879\uff0c\u5c06\u7ed3\u679c\u6c47\u603b\u4e3a\u4e00\u4e2a\u503c\u3002\u5b83\u4f4d\u4e8e <code>functools<\/code> \u6a21\u5757\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from functools import reduce\n\nnumbers = &#91;1, 2, 3, 4]\nproduct = reduce(lambda x, y: x * y, numbers)\nprint(product)  # \u8f93\u51fa 24<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. Lambda \u51fd\u6570\u7684\u4f18\u7f3a\u70b9<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u4f18\u70b9<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7b80\u6d01\u6027<\/strong>\uff1a\u9002\u7528\u4e8e\u7b80\u5355\u7684\u3001\u77ed\u5c0f\u7684\u51fd\u6570\uff0c\u907f\u514d\u4e86\u5197\u957f\u7684 <code>def<\/code> \u8bed\u6cd5\u3002<\/li>\n\n\n\n<li><strong>\u533f\u540d\u6027<\/strong>\uff1a\u53ef\u4ee5\u76f4\u63a5\u521b\u5efa\u51fd\u6570\u5bf9\u8c61\uff0c\u800c\u4e0d\u9700\u8981\u4e3a\u5176\u547d\u540d\uff0c\u9002\u7528\u4e8e\u4e00\u6b21\u6027\u4f7f\u7528\u7684\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 \u7f3a\u70b9<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53ef\u8bfb\u6027\u5dee<\/strong>\uff1a\u7531\u4e8e lambda \u51fd\u6570\u901a\u5e38\u53ea\u6709\u4e00\u884c\uff0c\u529f\u80fd\u8f83\u4e3a\u7b80\u5355\uff0c\u5b83\u4eec\u7684\u53ef\u8bfb\u6027\u4e0d\u5982\u5e38\u89c4\u7684\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong>\u529f\u80fd\u9650\u5236<\/strong>\uff1aLambda \u51fd\u6570\u53ea\u80fd\u5305\u542b\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u65e0\u6cd5\u5305\u542b\u591a\u4e2a\u8bed\u53e5\u6216\u590d\u6742\u7684\u903b\u8f91\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. Lambda \u51fd\u6570\u4e0e <code>def<\/code> \u5b9a\u4e49\u7684\u6bd4\u8f83<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u4f7f\u7528 <code>def<\/code> \u5b9a\u4e49\u51fd\u6570<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def add(x, y):\n    return x + y\n\nprint(add(3, 5))  # \u8f93\u51fa 8<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 \u4f7f\u7528 <code>lambda<\/code> \u5b9a\u4e49\u51fd\u6570<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>add = lambda x, y: x + y\nprint(add(3, 5))  # \u8f93\u51fa 8<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e24\u8005\u7684\u7ed3\u679c\u76f8\u540c\uff0c\u4f46 <code>lambda<\/code> \u51fd\u6570\u7b80\u6d01\u3001\u7d27\u51d1\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u529f\u80fd\u573a\u666f\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u591a\u4e2a\u53c2\u6570\u7684 Lambda \u51fd\u6570<\/strong><\/h2>\n\n\n\n<p>Lambda \u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u4e5f\u53ef\u4ee5\u5e94\u7528\u4e8e\u66f4\u590d\u6742\u7684\u8ba1\u7b97\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b 5\uff1a\u591a\u4e2a\u53c2\u6570<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>multiply = lambda x, y, z: x * y * z\nprint(multiply(2, 3, 4))  # \u8f93\u51fa 24<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. Lambda \u51fd\u6570\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.1 \u5728 <code>sorted()<\/code> \u4e2d\u4f7f\u7528 Lambda<\/strong><\/h3>\n\n\n\n<p>\u5728\u5bf9\u5217\u8868\u6392\u5e8f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 Lambda \u51fd\u6570\u4f5c\u4e3a\u6392\u5e8f\u7684\u5173\u952e\u5b57\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pairs = &#91;(1, 2), (3, 1), (5, 4)]\npairs.sort(key=lambda x: x&#91;1])  # \u6309\u7167\u5143\u7ec4\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6392\u5e8f\nprint(pairs)  # \u8f93\u51fa &#91;(3, 1), (1, 2), (5, 4)]<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Lambda \u51fd\u6570<\/strong> \u5141\u8bb8\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u533f\u540d\u7684\u3001\u7b80\u5355\u7684\u51fd\u6570\u3002<\/li>\n\n\n\n<li>\u5b83\u901a\u5e38\u7528\u4e8e\u4e00\u6b21\u6027\u64cd\u4f5c\u6216\u4e0e\u9ad8\u9636\u51fd\u6570\uff08\u5982 <code>map()<\/code>\u3001<code>filter()<\/code> \u548c <code>reduce()<\/code>\uff09\u7ed3\u5408\u4f7f\u7528\u3002<\/li>\n\n\n\n<li>Lambda \u51fd\u6570\u53ea\u80fd\u5305\u542b\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u9002\u5408\u7b80\u6d01\u7684\u4efb\u52a1\uff0c\u4f46\u4e0d\u9002\u5408\u590d\u6742\u903b\u8f91\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lambda \u51fd\u6570\uff0c\u4e5f\u79f0\u4e3a\u533f\u540d\u51fd\u6570\uff0c\u662f\u4e00\u79cd\u5728 Python \u4e2d\u5b9a\u4e49\u7b80\u6d01\u51fd\u6570\u7684\u65b9\u5f0f\u3002\u4e0e\u5e38\u89c4\u7684 def \u5b9a\u4e49\u51fd\u6570\u4e0d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2764,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2763"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2763\/revisions"}],"predecessor-version":[{"id":2765,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2763\/revisions\/2765"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2764"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}