﻿{"id":14599,"date":"2022-04-04T08:25:02","date_gmt":"2022-04-04T00:25:02","guid":{"rendered":"https:\/\/www.linuxmi.com\/?p=14599"},"modified":"2022-04-03T11:56:53","modified_gmt":"2022-04-03T03:56:53","slug":"3-keywords-python-code","status":"publish","type":"post","link":"https:\/\/www.linuxmi.com\/3-keywords-python-code.html","title":{"rendered":"3 \u4e2a\u5173\u952e\u5b57\u8ba9\u4f60\u7684 Python \u4ee3\u7801\u66f4\u4f18\u96c5"},"content":{"rendered":"<p>\u5c3d\u7ba1 Python \u4e0d\u662f\u4e3a\u51fd\u6570\u5f0f\u7f16\u7a0b\u800c\u6784\u5efa\u7684\uff0c\u4f46\u5b83\u786e\u5b9e\u652f\u6301\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u4e00\u4e9b\u975e\u5e38\u68d2\u7684\u7279\u6027\u3002\u5982\u679c\u60a8\u5e0c\u671b\u60a8\u7684\u7a0b\u5e8f\u66f4\u4f18\u96c5\u3001\u66f4\u53ef\u9760\u548c\u65e0\u72b6\u6001\uff0c\u90a3\u4e48\u51fd\u6570\u5f0f\u7f16\u7a0b\u53ef\u80fd\u662f\u60a8\u7684\u8003\u8651\u56e0\u7d20\u3002<\/p>\n<p>\u672c\u6587\u5c06\u4ecb\u7ecd Python \u4e2d\u7684 3 \u4e2a\u5de5\u5177\/\u51fd\u6570\uff0c\u5b83\u4eec\u53ef\u4ee5\u4e3a\u60a8\u63d0\u4f9b\u5f3a\u5927\u7684\u51fd\u6570\u548c\u4f18\u96c5\u5730\u91cd\u6784\u7a0b\u5e8f\u7684\u80fd\u529b\u3002<\/p>\n<p>\u867d\u7136Python\u4e0d\u662f\u4e3a\u51fd\u6570\u5f0f\u7f16\u7a0b\u800c\u6784\u5efa\u7684\uff0c\u4f46\u5b83\u786e\u5b9e\u652f\u6301\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u4e00\u4e9b\u975e\u5e38\u68d2\u7684\u7279\u6027\u3002\u5982\u679c\u60a8\u5e0c\u671b\u60a8\u7684\u7a0b\u5e8f\u66f4\u52a0\u4f18\u96c5\u3001\u53ef\u9760\u548c\u65e0\u72b6\u6001\uff0c\u90a3\u4e48\u51fd\u6570\u5f0f\u7f16\u7a0b\u53ef\u80fd\u662f\u60a8\u7684\u8003\u8651\u56e0\u7d20\u3002<\/p>\n<h2 id=\"b408\" class=\"mg mh iz bo mi mj mk ml mm mn mo mp mq lm mr ms mt lq mu mv mw lu mx my mz na gb\" data-selectable-paragraph=\"\"><strong class=\"ba\">1. Map<\/strong><\/h2>\n<p>\u4ec0\u4e48\u662f<code class=\"ng nh ni nj nk b\">map<\/code>\u51fd\u6570\uff1f<code class=\"ng nh ni nj nk b\">map<\/code>\u51fd\u6570\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u60a8\u8981\u5e94\u7528\u7684\u51fd\u6570\uff0c\u7b2c\u4e8c\u4e2a\u6216\u591a\u4e2a\u540e\u7eed\u53c2\u6570\u662f\u60a8\u5e0c\u671b\u8be5\u51fd\u6570\u5e94\u7528\u5230\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u8ba9\u6211\u4eec\u770b\u4e0b\u4e00\u4e2a\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14600\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2022\/04\/1.png\" alt=\"\" width=\"866\" height=\"580\" \/><\/p>\n<p id=\"184b\" class=\"pw-post-body-paragraph ld le iz lf b lg lh ka li lj lk kd ll lm ln lo lp lq lr ls lt lu lv lw lx ly is gb\" data-selectable-paragraph=\"\">\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u60f3\u8981\u5bf9\u5217\u8868\u4e2d\u7684\u6570\u5b57\u8fdb\u884c\u5e73\u65b9\u3002\u65b9\u6cd5\u662f\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u4f7f\u7528<code class=\"ng nh ni nj nk b\">map(function_you_want_to_apply, your_list)<\/code>.\u00a0\u8bf7\u6ce8\u610f\uff0c\u60a8\u5f53\u7136\u53ef\u4ee5\u5c06\u60a8\u7684\u51fd\u6570\u5e94\u7528\u4e8e\u5217\u8868\u4e4b\u5916\u7684\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u6b64\u5916\uff0c\u8be5<code class=\"ng nh ni nj nk b\">map<\/code>\u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u60a8\u9700\u8981\u8fed\u4ee3\u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u4ee5\u83b7\u53d6\u6240\u6709\u7ed3\u679c\u3002<\/p>\n<p id=\"9611\" class=\"pw-post-body-paragraph ld le iz lf b lg lh ka li lj lk kd ll lm ln lo lp lq lr ls lt lu lv lw lx ly is gb\" data-selectable-paragraph=\"\">\u53e6\u4e00\u79cd\u7528\u6cd5\u662f\u60a8\u53ef\u4ee5\u5c06\u8be5\u51fd\u6570\u5e94\u7528\u4e8e\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u7136\u800c\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u5fc5\u987b\u8ba9\u4f60\u7684\u51fd\u6570\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\u2014\u2014\u6bcf\u4e2a\u53ef\u8fed\u4ee3\u7684\u53c2\u6570\u3002<\/p>\n<p data-selectable-paragraph=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14606\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2022\/04\/6.png\" alt=\"\" width=\"1364\" height=\"580\" \/><\/p>\n<p id=\"1f95\" class=\"pw-post-body-paragraph ld le iz lf b lg lh ka li lj lk kd ll lm ln lo lp lq lr ls lt lu lv lw lx ly is gb\" data-selectable-paragraph=\"\">\u5982\u679c\u4f60\u4ecd\u7136\u50cf\u4ee5\u524d\u4e00\u6837\u4f7f\u7528\u5355\u53c2\u6570\u51fd\u6570<code class=\"ng nh ni nj nk b\">square<\/code>\uff0cPython \u4f1a\u62b1\u6028\u7684\u3002<\/p>\n<p id=\"32df\" class=\"pw-post-body-paragraph ld le iz lf b lg lh ka li lj lk kd ll lm ln lo lp lq lr ls lt lu lv lw lx ly is gb\" data-selectable-paragraph=\"\">\u53e6\u4e00\u4e2a\u6280\u5de7\u662f\u7ecf\u5e38\u5c06 lambda \u8868\u8fbe\u5f0f\u4e0e<code class=\"ng nh ni nj nk b\">map<\/code>\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u4f7f\u60a8\u7684\u4ee3\u7801\u7b80\u6d01\u3002Python \u4e2d\u7684 lambda \u8868\u8fbe\u5f0f\u91c7\u7528<code class=\"ng nh ni nj nk b\">lambda your_arguments_to_take_in : expression_to_return<\/code>.\u00a0\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e0a\u8ff0 2 \u4e2a\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14605\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2022\/04\/5.png\" alt=\"\" width=\"1386\" height=\"628\" \/><\/p>\n<h2 id=\"d1b2\" class=\"mg mh iz bo mi mj mk ml mm mn mo mp mq lm mr ms mt lq mu mv mw lu mx my mz na gb\" data-selectable-paragraph=\"\">2.\u00a0<strong class=\"ba\">Reduce<\/strong><\/h2>\n<p>\u8be5<code class=\"ng nh ni nj nk b\">reduce<\/code>\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u7c7b\u4f3c\u4e8e map-reduce \u4e2d\u7684 reduce \u6982\u5ff5\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u6570\u636e\u6d41\uff0c\u6216\u8005\u5728 Python \u8bed\u8a00\u4e2d\uff0c\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5e76\u5c06\u51fd\u6570\u4ece\u5de6\u5230\u53f3\u5e94\u7528\u4e8e\u8fed\u4ee3\u5668\u3002\u4f7f\u7528<code class=\"ng nh ni nj nk b\">reduce<\/code>\u5173\u952e\u5b57\u53ef\u4ee5\u50cf<code class=\"ng nh ni nj nk b\">map<\/code>\u5173\u952e\u5b57\u4e00\u6837\u521b\u5efa\u7b80\u6d01\u7684\u8bed\u53e5\u3002<code class=\"ng nh ni nj nk b\">reduce<\/code>\u548c\u4e4b\u95f4\u7684\u4e00\u4e2a\u5173\u952e\u533a\u522b<code class=\"ng nh ni nj nk b\">map<\/code>\u662f<code class=\"ng nh ni nj nk b\">reduce<\/code>\u5c06\u51fd\u6570\u7d2f\u79ef\u5730\u5e94\u7528\u4e8e\u60a8\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u8fd9\u610f\u5473\u7740\u5b83\u9996\u5148\u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u83b7\u53d6 n \u4e2a\u53c2\u6570\uff0c\u7136\u540e\u8fd4\u56de 1 \u4e2a\u7ed3\u679c\uff0c\u7136\u540e\u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u83b7\u53d6\u4e0b\u4e00\u4e2a n-1 \u4e2a\u53c2\u6570\uff0c\u5e76\u4e0e\u4e4b\u524d\u7684 1 \u4e2a\u7ed3\u679c\u7ed3\u5408\u5f62\u6210\u4e0b\u4e00\u4e2a n \u4e2a\u53c2\u6570\u4ee5\u4f20\u9012\u7ed9\u60a8\u7684\u51fd\u6570\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c<code class=\"ng nh ni nj nk b\">reduce<\/code>\u5173\u952e\u5b57\u65e8\u5728\u5c06\u60a8\u7684\u53ef\u8fed\u4ee3\u7ed3\u679c\u51cf\u5c11\u5230 1 \u4e2a\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14603\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2022\/04\/4.png\" alt=\"\" width=\"1212\" height=\"484\" \/><\/p>\n<p id=\"effe\" class=\"pw-post-body-paragraph ld le iz lf b lg lh ka li lj lk kd ll lm ln lo lp lq lr ls lt lu lv lw lx ly is gb\" data-selectable-paragraph=\"\">\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4ece<code class=\"ng nh ni nj nk b\">map<\/code>to<code class=\"ng nh ni nj nk b\">reduce<\/code>\u5173\u952e\u5b57\u53d8\u4e3a\u6211\u4eec\u7684\u7ed3\u679c\u4ece\u4e00\u4e2a\u8fed\u4ee3\u5668\u53d8\u4e3a\u4e00\u4e2a int\u3002<\/p>\n<p id=\"2178\" class=\"pw-post-body-paragraph ld le iz lf b lg lh ka li lj lk kd ll lm ln lo lp lq lr ls lt lu lv lw lx ly is gb\" data-selectable-paragraph=\"\">\u8981\u4e86\u89e3\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u53c2\u8003\u5173\u4e8e reduce \u7684\u5b98\u65b9 Python \u6587\u6863\u3002\u6839\u636e\u5b83\uff0c<code class=\"ng nh ni nj nk b\">reduce<\/code>\u5173\u952e\u5b57\u7684\u4f5c\u7528\u5927\u81f4\u76f8\u5f53\u4e8e\u4ee5\u4e0b\u51fd\u6570\uff1a<\/p>\n<p data-selectable-paragraph=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14609\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2022\/04\/7.png\" alt=\"\" width=\"1364\" height=\"724\" \/><\/p>\n<p>\u60a8\u4f1a\u770b\u5230<code class=\"ng nh ni nj nk b\">reduce<\/code>\u6700\u7ec8\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u800c\u4e0d\u662f <code class=\"ng nh ni nj nk b\">map<\/code>\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u5f53\u7136\uff0c\u4f60\u603b\u662f\u53ef\u4ee5\u5c06<code class=\"ng nh ni nj nk b\">map<\/code>\u548c<code class=\"ng nh ni nj nk b\">reduce<\/code>\u7ec4\u5408\u5728\u4e00\u8d77\u4ea7\u751f\u4e00\u4e2a\u5f3a\u6709\u529b\u7684\u58f0\u660e\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\u53ef\u4ee5\u5728\u4e00\u884c\u5185\u8ba1\u7b97\u5217\u8868\u7684\u6b27\u51e0\u91cc\u5f97\u8303\u6570\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14611\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2022\/04\/8.png\" alt=\"\" width=\"1580\" height=\"532\" \/><\/p>\n<p><code class=\"ng nh ni nj nk b\">map<\/code>\u548c\u5728<code class=\"ng nh ni nj nk b\">reduce<\/code>Python \u548c\u4e00\u822c\u7f16\u7a0b\u4e2d\u8d85\u7ea7\u6709\u7528\u548c\u5f3a\u5927\uff0c\u8bf7\u968f\u610f\u4f7f\u7528\u5b83\uff01<\/p>\n<h2 id=\"a820\" class=\"mg mh iz bo mi mj mk ml mm mn mo mp mq lm mr ms mt lq mu mv mw lu mx my mz na gb\" data-selectable-paragraph=\"\">3.\u00a0<strong class=\"ba\">Filter<\/strong><\/h2>\n<p id=\"e1d9\" class=\"pw-post-body-paragraph ld le iz lf b lg nb ka li lj nc kd ll lm nd lo lp lq ne ls lt lu nf lw lx ly is gb\" data-selectable-paragraph=\"\">\u5728\u4e0a\u9762\u7684\u4e24\u4e2a\u8ba8\u8bba\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u5c06\u51fd\u6570\u5e94\u7528\u4e8e\u4e00\u4e2a\u53ef\u8fed\u4ee3\u7684\u548c\u8f93\u51fa\u5177\u6709\u76f8\u540c\u957f\u5ea6\u7684\u7ed3\u679c\u548c\u4e00\u4e2a\u503c\u3002\u600e\u4e48\u6837\uff1a\u6211\u4eec\u60f3\u8fc7\u6ee4\u6570\u7ec4\u7684\u6240\u6709\u5947\u6570\u503c\uff0c\u5e76\u5728\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7559\u4e0b\u5076\u6570\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c<code class=\"ng nh ni nj nk b\">map<\/code>and\u90fd\u4e0d\u80fd<code class=\"ng nh ni nj nk b\">reduce<\/code>\u6b63\u5e38\u5de5\u4f5c\uff0c\u8fd9\u5c31\u662f<code class=\"ng nh ni nj nk b\">filter<\/code>\u5173\u952e\u5b57\u7684\u7528\u6b66\u4e4b\u5730\u3002\u57fa\u672c\u8bed\u6cd5\u4ecd\u7136\u4e0e <code class=\"ng nh ni nj nk b\">map<\/code> \u548c <code class=\"ng nh ni nj nk b\">reduce<\/code>\u76f8\u540c\uff0c\u53ea\u662f\u9664\u4e86<code class=\"ng nh ni nj nk b\">filter<\/code>\u7684\u51fd\u6570\u53c2\u6570\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u5728 Python \u4e2d\u79f0\u4e3a\u8c13\u8bcd\u3002\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u8f93\u5165\u4ee3\u7801:<\/p>\n<p data-selectable-paragraph=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14612\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2022\/04\/9.png\" alt=\"\" width=\"1256\" height=\"436\" \/><\/p>\n<p>\u53e6\u5916\uff0c\u8bf7\u6ce8\u610f\uff0c<code class=\"ng nh ni nj nk b\">filter<\/code>\u5173\u952e\u5b57\u8fd4\u56de\u4e00\u4e2a\u53ef\u8fed\u4ee3\u7684\uff0c\u5c31\u50cf<code class=\"ng nh ni nj nk b\">map<\/code>\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u503c\u3002\u5f53\u7136\uff0c\u5728Python\u4e2d\uff0c\u5c06<code class=\"ng nh ni nj nk b\">filter, map, reduce<\/code>\u7ed3\u5408\u8d77\u6765\u521b\u5efa\u66f4\u590d\u6742\u7684\u4e00\u884c\u7a0b\u5e8f\u662f\u5f88\u81ea\u7136\u7684\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u8981\u8ba1\u7b97\u6240\u6709\u5076\u6570\u7684\u5e73\u65b9\u548c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14614\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2022\/04\/10.png\" alt=\"\" width=\"2274\" height=\"532\" \/><\/p>\n<p>\u73b0\u5728\uff0c\u60a8\u5df2\u7ecf\u4ecePython\u5b66\u4e60\u4e86\u8fd93\u4e2a\u5f3a\u5927\u7684\u5173\u952e\u5b57\u3002\u5728\u7a0b\u5e8f\u4e2d\u9002\u5f53\u5730\u4f7f\u7528\u5b83\u4eec\u53ef\u4ee5\u8ba9\u4ee3\u7801\u770b\u8d77\u6765\u5f88\u4f18\u96c5\u3002\u73b0\u5728\uff0c\u8f6e\u5230\u4f60\u5e94\u7528\u5b83\u4eec\u4e86\uff01\u5feb\u4e50\u5b66\u4e60\u548c\u5feb\u4e50\u7f16\u7801\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c3d\u7ba1 Python \u4e0d\u662f\u4e3a\u51fd\u6570\u5f0f\u7f16\u7a0b\u800c\u6784\u5efa\u7684\uff0c\u4f46\u5b83\u786e\u5b9e\u652f\u6301\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u4e00\u4e9b\u975e\u5e38\u68d2\u7684\u7279\u6027\u3002\u5982\u679c\u60a8\u5e0c\u671b\u60a8 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[44],"class_list":["post-14599","post","type-post","status-publish","format-standard","hentry","category-python","tag-python"],"aioseo_notices":[],"views":487,"_links":{"self":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/14599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/comments?post=14599"}],"version-history":[{"count":7,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/14599\/revisions"}],"predecessor-version":[{"id":14616,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/14599\/revisions\/14616"}],"wp:attachment":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/media?parent=14599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/categories?post=14599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/tags?post=14599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}