{"id":2475,"date":"2017-07-20T10:54:39","date_gmt":"2017-07-20T02:54:39","guid":{"rendered":"http:\/\/cn.hostease.com\/xueyuan\/?p=2475"},"modified":"2025-01-08T16:08:50","modified_gmt":"2025-01-08T08:08:50","slug":"python%e8%bf%9b%e9%98%b607-%e5%87%bd%e6%95%b0%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/cn.hostease.com\/xueyuan\/jishu\/python\/python%e8%bf%9b%e9%98%b607-%e5%87%bd%e6%95%b0%e5%af%b9%e8%b1%a1\/","title":{"rendered":"Python\u8fdb\u963607 \u51fd\u6570\u5bf9\u8c61"},"content":{"rendered":"<div id=\"cnblogs_post_body\">\n<h3>lambda\u51fd\u6570<\/h3>\n<p>\u5728\u5c55\u5f00\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u63d0\u4e00\u4e0blambda\u51fd\u6570\u3002\u53ef\u4ee5\u5229\u7528lambda\u51fd\u6570\u7684\u8bed\u6cd5\uff0c\u5b9a\u4e49\u51fd\u6570\u3002lambda\u4f8b\u5b50\u5982\u4e0b\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>func = lambda x,y: x + y\nprint func(3,4)<\/pre>\n<\/div>\n<p>lambda\u751f\u6210\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u3002\u8be5\u51fd\u6570\u53c2\u6570\u4e3ax,y\uff0c\u8fd4\u56de\u503c\u4e3ax+y\u3002\u51fd\u6570\u5bf9\u8c61\u8d4b\u7ed9func\u3002func\u7684\u8c03\u7528\u4e0e\u6b63\u5e38\u51fd\u6570\u65e0\u5f02\u3002<\/p>\n<p>\u4ee5\u4e0a\u5b9a\u4e49\u53ef\u4ee5\u5199\u6210\u4ee5\u4e0b\u5f62\u5f0f\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>def func(x, y):\n    return x + y<\/pre>\n<\/div>\n<h3>\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012<\/h3>\n<p>\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8fdb\u884c\u53c2\u6570\u4f20\u9012\u3002\u51fd\u6570\u540d(\u6bd4\u5982func)\u5373\u8be5\u5bf9\u8c61\u3002\u6bd4\u5982\u8bf4:<\/p>\n<div class=\"cnblogs_code\">\n<pre>def test(f, a, b):\n    print 'test'\n    print f(a, b)\n\ntest(func, 3, 5)<\/pre>\n<\/div>\n<p>test\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570f\u5c31\u662f\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u3002\u5c06func\u4f20\u9012\u7ed9f\uff0ctest\u4e2d\u7684f()\u5c31\u62e5\u6709\u4e86func()\u7684\u529f\u80fd\u3002<\/p>\n<p>\u6211\u4eec\u56e0\u6b64\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0a\u9762\u7684test\u51fd\u6570\uff0c\u5e26\u5165\u4e0d\u540c\u7684\u51fd\u6570\u53c2\u6570\u3002\u6bd4\u5982:<\/p>\n<div class=\"cnblogs_code\">\n<pre>test((lambda x,y: x**2 + y), 6, 9)<\/pre>\n<\/div>\n<h3>map()\u51fd\u6570<\/h3>\n<p>map()\u662fPython\u7684\u5185\u7f6e\u51fd\u6570\u3002\u5b83\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u3002<\/p>\n<div class=\"cnblogs_code\">\n<pre>re = map((lambda x: x+3),[1,3,5,6])<\/pre>\n<\/div>\n<p>\u8fd9\u91cc\uff0cmap()\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u662flambda\u6240\u5b9a\u4e49\u7684\u51fd\u6570\u5bf9\u8c61\uff0c\u4e00\u4e2a\u662f\u5305\u542b\u6709\u591a\u4e2a\u5143\u7d20\u7684\u8868\u3002map()\u7684\u529f\u80fd\u662f\u5c06\u51fd\u6570\u5bf9\u8c61\u4f9d\u6b21\u4f5c\u7528\u4e8e\u8868\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u6bcf\u6b21\u4f5c\u7528\u7684\u7ed3\u679c\u50a8\u5b58\u4e8e\u8fd4\u56de\u7684\u8868re\u4e2d\u3002map\u901a\u8fc7\u8bfb\u5165\u7684\u51fd\u6570(\u8fd9\u91cc\u662flambda\u51fd\u6570)\u6765\u64cd\u4f5c\u6570\u636e\uff08\u8fd9\u91cc\u201c\u6570\u636e\u201d\u662f\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u201c\u64cd\u4f5c\u201d\u662f\u5bf9\u6bcf\u4e2a\u6570\u636e\u52a03\uff09\u3002<\/p>\n<p>\u5728Python 3.X\u4e2d\uff0cmap()\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u5faa\u73af\u5bf9\u8c61\u3002\u53ef\u4ee5\u5229\u7528list()\u51fd\u6570\uff0c\u5c06\u8be5\u5faa\u73af\u5bf9\u8c61\u8f6c\u6362\u6210\u8868\u3002<\/p>\n<p>\u5982\u679c\u4f5c\u4e3a\u53c2\u6570\u7684\u51fd\u6570\u5bf9\u8c61\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u53ef\u4f7f\u7528\u4e0b\u9762\u7684\u65b9\u5f0f\uff0c\u5411map()\u4f20\u9012\u51fd\u6570\u53c2\u6570\u7684\u591a\u4e2a\u53c2\u6570\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>re = map((lambda x,y: x+y),[1,2,3],[6,7,9])<\/pre>\n<\/div>\n<p>map()\u5c06\u6bcf\u6b21\u4ece\u4e24\u4e2a\u8868\u4e2d\u5206\u522b\u53d6\u51fa\u4e00\u4e2a\u5143\u7d20\uff0c\u5e26\u5165lambda\u6240\u5b9a\u4e49\u7684\u51fd\u6570\u3002<\/p>\n<h3>filter()\u51fd\u6570<\/h3>\n<p>filter\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e5f\u662f\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u3002\u5b83\u4e5f\u662f\u5c06\u4f5c\u4e3a\u53c2\u6570\u7684\u51fd\u6570\u5bf9\u8c61\u4f5c\u7528\u4e8e\u591a\u4e2a\u5143\u7d20\u3002\u5982\u679c\u51fd\u6570\u5bf9\u8c61\u8fd4\u56de\u7684\u662fTrue\uff0c\u5219\u8be5\u6b21\u7684\u5143\u7d20\u88ab\u50a8\u5b58\u4e8e\u8fd4\u56de\u7684\u8868\u4e2d\u3002filter\u901a\u8fc7\u8bfb\u5165\u7684\u51fd\u6570\u6765\u7b5b\u9009\u6570\u636e\u3002\u540c\u6837\uff0c\u5728Python 3.X\u4e2d\uff0cfilter\u8fd4\u56de\u7684\u4e0d\u662f\u8868\uff0c\u800c\u662f\u5faa\u73af\u5bf9\u8c61\u3002<\/p>\n<p>filter\u51fd\u6570\u7684\u4f7f\u7528\u5982\u4e0b\u4f8b:<\/p>\n<div class=\"cnblogs_code\">\n<pre>def func(a):\n    if a &gt; 100:\n        return True\n    else:\n        return False\n\nprint filter(func,[10,56,101,500])<\/pre>\n<\/div>\n<h3>reduce()\u51fd\u6570<\/h3>\n<p>reduce\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e5f\u662f\u51fd\u6570\uff0c\u4f46\u6709\u4e00\u4e2a\u8981\u6c42\uff0c\u5c31\u662f\u8fd9\u4e2a\u51fd\u6570\u81ea\u8eab\u80fd\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\u3002reduce\u53ef\u4ee5\u7d2f\u8fdb\u5730\u5c06\u51fd\u6570\u4f5c\u7528\u4e8e\u5404\u4e2a\u53c2\u6570\u3002\u5982\u4e0b\u4f8b\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>print reduce((lambda x,y: x+y),[1,2,5,7,9])<\/pre>\n<\/div>\n<p>reduce\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662flambda\u51fd\u6570\uff0c\u5b83\u63a5\u6536\u4e24\u4e2a\u53c2\u6570x,y, \u8fd4\u56dex+y\u3002<\/p>\n<p>reduce\u5c06\u8868\u4e2d\u7684\u524d\u4e24\u4e2a\u5143\u7d20(1\u548c2)\u4f20\u9012\u7ed9lambda\u51fd\u6570\uff0c\u5f97\u52303\u3002\u8be5\u8fd4\u56de\u503c(3)\u5c06\u4f5c\u4e3alambda\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u800c\u8868\u4e2d\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20(5)\u4f5c\u4e3alambda\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u8fdb\u884c\u4e0b\u4e00\u6b21\u7684\u5bf9lambda\u51fd\u6570\u7684\u8c03\u7528\uff0c\u5f97\u52308\u3002\u4f9d\u6b21\u8c03\u7528lambda\u51fd\u6570\uff0c\u6bcf\u6b21lambda\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e0a\u4e00\u6b21\u8fd0\u7b97\u7ed3\u679c\uff0c\u800c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u8868\u4e2d\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\uff0c\u76f4\u5230\u8868\u4e2d\u6ca1\u6709\u5269\u4f59\u5143\u7d20\u3002<\/p>\n<p>\u4e0a\u9762\u4f8b\u5b50\uff0c\u76f8\u5f53\u4e8e(((1+2)+5)+7)+9<\/p>\n<p>\u6839\u636emmufhy\u7684\u63d0\u9192\uff1a reduce()\u51fd\u6570\u57283.0\u91cc\u9762\u4e0d\u80fd\u76f4\u63a5\u7528\u7684\uff0c\u5b83\u88ab\u5b9a\u4e49\u5728\u4e86functools\u5305\u91cc\u9762\uff0c\u9700\u8981\u5f15\u5165\u5305\uff0c\u89c1\u8bc4\u8bba\u533a\u3002<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u51fd\u6570\u662f\u4e00\u4e2a\u5bf9\u8c61<\/p>\n<p>\u7528lambda\u5b9a\u4e49\u51fd\u6570<\/p>\n<p>map()<\/p>\n<p>filter()<\/p>\n<p>reduce()<\/p>\n<\/div>\n<p>\u4f5c\u8005\uff1aVamei \u51fa\u5904\uff1ahttps:\/\/www.cnblogs.com\/vamei<\/p>\n","protected":false},"excerpt":{"rendered":"<p>lambda\u51fd\u6570 \u5728\u5c55\u5f00\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u63d0\u4e00\u4e0blambda\u51fd\u6570\u3002\u53ef\u4ee5\u5229\u7528lambda\u51fd\u6570\u7684\u8bed\u6cd5\uff0c\u5b9a\u4e49\u51fd\u6570\u3002lamb &#8230; <a title=\"Python\u8fdb\u963607 \u51fd\u6570\u5bf9\u8c61\" class=\"read-more\" href=\"https:\/\/cn.hostease.com\/xueyuan\/jishu\/python\/python%e8%bf%9b%e9%98%b607-%e5%87%bd%e6%95%b0%e5%af%b9%e8%b1%a1\/\" aria-label=\"\u9605\u8bfb Python\u8fdb\u963607 \u51fd\u6570\u5bf9\u8c61\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[654],"tags":[728],"class_list":["post-2475","post","type-post","status-publish","format-standard","hentry","category-python","tag-python"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/comments?post=2475"}],"version-history":[{"count":2,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2475\/revisions"}],"predecessor-version":[{"id":9195,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2475\/revisions\/9195"}],"wp:attachment":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/media?parent=2475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/categories?post=2475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/tags?post=2475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}