{"id":513,"date":"2019-08-17T10:01:10","date_gmt":"2019-08-17T02:01:10","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=513"},"modified":"2019-10-15T16:17:20","modified_gmt":"2019-10-15T08:17:20","slug":"4mfhr","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/","title":{"rendered":"4.0 Map\uff0cFilter\u548cReduce"},"content":{"rendered":"\n

\u539f\u6587\uff1a https:\/\/docs.pythontab.com\/interpy\/Map_Filter\/Reduce\/<\/a> <\/p>\n\n\n\n

Map\uff0cFilter \u548c Reduce<\/h1>\n\n\n\n

Map\uff0cFilter \u548c Reduce \u4e09\u4e2a\u51fd\u6570\u80fd\u4e3a\u51fd\u6570\u5f0f\u7f16\u7a0b\u63d0\u4f9b\u4fbf\u5229\u3002\u6211\u4eec\u4f1a\u901a\u8fc7\u5b9e\u4f8b\u4e00\u4e2a\u4e00\u4e2a\u8ba8\u8bba\u5e76\u7406\u89e3\u5b83\u4eec\u3002<\/p>\n\n\n\n

Map<\/code><\/h1>\n\n\n\n

Map<\/code>\u4f1a\u5c06\u4e00\u4e2a\u51fd\u6570\u6620\u5c04\u5230\u4e00\u4e2a\u8f93\u5165\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u4e0a\u3002\u8fd9\u662f\u5b83\u7684\u89c4\u8303\uff1a<\/p>\n\n\n\n

\u89c4\u8303<\/strong><\/p>\n\n\n\n

map(function_to_apply, list_of_inputs)\n<\/code><\/pre>\n\n\n\n

\u5927\u591a\u6570\u65f6\u5019\uff0c\u6211\u4eec\u8981\u628a\u5217\u8868\u4e2d\u6240\u6709\u5143\u7d20\u4e00\u4e2a\u4e2a\u5730\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u6536\u96c6\u8f93\u51fa\u3002\u6bd4\u65b9\u8bf4\uff1a<\/p>\n\n\n\n

items = [1, 2, 3, 4, 5]\nsquared = []\nfor i in items:\n    squared.append(i**2)\n<\/code><\/pre>\n\n\n\n

Map<\/code>\u53ef\u4ee5\u8ba9\u6211\u4eec\u7528\u4e00\u79cd\u7b80\u5355\u800c\u6f02\u4eae\u5f97\u591a\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u5c31\u662f\u8fd9\u6837\uff1a<\/p>\n\n\n\n

items = [1, 2, 3, 4, 5]\nsquared = list(map(lambda x: x**2, items))\n<\/code><\/pre>\n\n\n\n

\u5927\u591a\u6570\u65f6\u5019\uff0c\u6211\u4eec\u4f7f\u7528\u533f\u540d\u51fd\u6570(lambdas)\u6765\u914d\u5408map<\/code>, \u6240\u4ee5\u6211\u5728\u4e0a\u9762\u4e5f\u662f\u8fd9\u4e48\u505a\u7684\u3002 \u4e0d\u4ec5\u7528\u4e8e\u4e00\u5217\u8868\u7684\u8f93\u5165\uff0c \u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u7528\u4e8e\u4e00\u5217\u8868\u7684\u51fd\u6570\uff01<\/p>\n\n\n\n

def multiply(x):\n        return (x*x)\ndef add(x):\n        return (x+x)\n\nfuncs = [multiply, add]\nfor i in range(5):\n    value = map(lambda x: x(i), funcs)\n    print(list(value))\n    # \u8bd1\u8005\u6ce8\uff1a\u4e0a\u9762print\u65f6\uff0c\u52a0\u4e86list\u8f6c\u6362\uff0c\u662f\u4e3a\u4e86python2\/3\u7684\u517c\u5bb9\u6027\n    #        \u5728python2\u4e2dmap\u76f4\u63a5\u8fd4\u56de\u5217\u8868\uff0c\u4f46\u5728python3\u4e2d\u8fd4\u56de\u8fed\u4ee3\u5668\n    #        \u56e0\u6b64\u4e3a\u4e86\u517c\u5bb9python3, \u9700\u8981list\u8f6c\u6362\u4e00\u4e0b\n\n# Output:\n# [0, 0]\n# [1, 2]\n# [4, 4]\n# [9, 6]\n# [16, 8]<\/code><\/pre>\n\n\n\n

Filter<\/code><\/h1>\n\n\n\n

\u987e\u540d\u601d\u4e49\uff0cfilter<\/code>\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u8fd4\u56de\u4e00\u4e2a\u7531\u6240\u6709\u7b26\u5408\u8981\u6c42\u7684\u5143\u7d20\u6240\u6784\u6210\u7684\u5217\u8868\uff0c\u7b26\u5408\u8981\u6c42<\/code>\u5373\u51fd\u6570\u6620\u5c04\u5230\u8be5\u5143\u7d20\u65f6\u8fd4\u56de\u503c\u4e3aTrue. \u8fd9\u91cc\u662f\u4e00\u4e2a\u7b80\u77ed\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n

number_list = range(-5, 5)\nless_than_zero = filter(lambda x: x < 0, number_list)\nprint(list(less_than_zero))  \n# \u8bd1\u8005\u6ce8\uff1a\u4e0a\u9762print\u65f6\uff0c\u52a0\u4e86list\u8f6c\u6362\uff0c\u662f\u4e3a\u4e86python2\/3\u7684\u517c\u5bb9\u6027\n#        \u5728python2\u4e2dfilter\u76f4\u63a5\u8fd4\u56de\u5217\u8868\uff0c\u4f46\u5728python3\u4e2d\u8fd4\u56de\u8fed\u4ee3\u5668\n#        \u56e0\u6b64\u4e3a\u4e86\u517c\u5bb9python3, \u9700\u8981list\u8f6c\u6362\u4e00\u4e0b\n\n# Output: [-5, -4, -3, -2, -1]\n<\/code><\/pre>\n\n\n\n

\u8fd9\u4e2afilter<\/code>\u7c7b\u4f3c\u4e8e\u4e00\u4e2afor<\/code>\u5faa\u73af\uff0c\u4f46\u5b83\u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u5e76\u4e14\u66f4\u5feb\u3002<\/p>\n\n\n\n

\u6ce8\u610f\uff1a\u5982\u679cmap<\/code>\u548cfilter<\/code>\u5bf9\u4f60\u6765\u8bf4\u770b\u8d77\u6765\u5e76\u4e0d\u4f18\u96c5\u7684\u8bdd\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u770b\u770b\u53e6\u5916\u4e00\u7ae0\uff1a\u5217\u8868\/\u5b57\u5178\/\u5143\u7ec4\u63a8\u5bfc\u5f0f\u3002<\/p>\n\n\n\n

\u8bd1\u8005\u6ce8\uff1a\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u63a8\u5bfc\u5f0f\u7684\u53ef\u8bfb\u6027\u66f4\u597d<\/p><\/blockquote>\n\n\n\n

Reduce<\/code><\/h1>\n\n\n\n

\u5f53\u9700\u8981\u5bf9\u4e00\u4e2a\u5217\u8868\u8fdb\u884c\u4e00\u4e9b\u8ba1\u7b97\u5e76\u8fd4\u56de\u7ed3\u679c\u65f6\uff0cReduce<\/code> \u662f\u4e2a\u975e\u5e38\u6709\u7528\u7684\u51fd\u6570\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5f53\u4f60\u9700\u8981\u8ba1\u7b97\u4e00\u4e2a\u6574\u6570\u5217\u8868\u7684\u4e58\u79ef\u65f6\u3002<\/p>\n\n\n\n

\u901a\u5e38\u5728 python \u4e2d\u4f60\u53ef\u80fd\u4f1a\u4f7f\u7528\u57fa\u672c\u7684 for \u5faa\u73af\u6765\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u3002<\/p>\n\n\n\n

\u73b0\u5728\u6211\u4eec\u6765\u8bd5\u8bd5 reduce\uff1a<\/p>\n\n\n\n

from functools import reduce\nproduct = reduce( (lambda x, y: x * y), [1, 2, 3, 4] )\n\n# Output: 24<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

\u539f\u6587\uff1a https:\/\/docs.pythontab.com\/interpy\/Map_Filter\/Reduc<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,13],"tags":[],"yoast_head":"\n4.0 Map\uff0cFilter\u548cReduce - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"4.0 Map\uff0cFilter\u548cReduce - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\" \/>\n<meta property=\"og:description\" content=\"\u539f\u6587\uff1a https:\/\/docs.pythontab.com\/interpy\/Map_Filter\/Reduc\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-17T02:01:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-10-15T08:17:20+00:00\" \/>\n<meta name=\"author\" content=\"\u7ba1\u7406\u5458\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/\"},\"author\":{\"name\":\"\u7ba1\u7406\u5458\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"headline\":\"4.0 Map\uff0cFilter\u548cReduce\",\"datePublished\":\"2019-08-17T02:01:10+00:00\",\"dateModified\":\"2019-10-15T08:17:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/\"},\"wordCount\":23,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"articleSection\":[\"Linux\",\"Python\"],\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/\",\"url\":\"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/\",\"name\":\"4.0 Map\uff0cFilter\u548cReduce - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\",\"isPartOf\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#website\"},\"datePublished\":\"2019-08-17T02:01:10+00:00\",\"dateModified\":\"2019-10-15T08:17:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/#breadcrumb\"},\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.linuxdevops.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\",\"item\":\"https:\/\/www.linuxdevops.cn\/linux\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"4.0 Map\uff0cFilter\u548cReduce\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#website\",\"url\":\"https:\/\/www.linuxdevops.cn\/\",\"name\":\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\",\"description\":\"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\u7b14\u8bb0\",\"publisher\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\"},\"alternateName\":\"linuxdevops\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.linuxdevops.cn\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-CN\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1\",\"name\":\"\u7ba1\u7406\u5458\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-CN\",\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg\",\"contentUrl\":\"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg\",\"width\":512,\"height\":512,\"caption\":\"\u7ba1\u7406\u5458\"},\"logo\":{\"@id\":\"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/\"},\"description\":\"\u7ba1\u7406\u5458\",\"url\":\"https:\/\/www.linuxdevops.cn\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"4.0 Map\uff0cFilter\u548cReduce - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/","og_locale":"zh_CN","og_type":"article","og_title":"4.0 Map\uff0cFilter\u548cReduce - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","og_description":"\u539f\u6587\uff1a https:\/\/docs.pythontab.com\/interpy\/Map_Filter\/Reduc","og_url":"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/","og_site_name":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","article_published_time":"2019-08-17T02:01:10+00:00","article_modified_time":"2019-10-15T08:17:20+00:00","author":"\u7ba1\u7406\u5458","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/#article","isPartOf":{"@id":"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/"},"author":{"name":"\u7ba1\u7406\u5458","@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"headline":"4.0 Map\uff0cFilter\u548cReduce","datePublished":"2019-08-17T02:01:10+00:00","dateModified":"2019-10-15T08:17:20+00:00","mainEntityOfPage":{"@id":"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/"},"wordCount":23,"commentCount":0,"publisher":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"articleSection":["Linux","Python"],"inLanguage":"zh-CN","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/","url":"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/","name":"4.0 Map\uff0cFilter\u548cReduce - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","isPartOf":{"@id":"https:\/\/www.linuxdevops.cn\/#website"},"datePublished":"2019-08-17T02:01:10+00:00","dateModified":"2019-10-15T08:17:20+00:00","breadcrumb":{"@id":"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/#breadcrumb"},"inLanguage":"zh-CN","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.linuxdevops.cn\/2019\/08\/4mfhr\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.linuxdevops.cn\/"},{"@type":"ListItem","position":2,"name":"Linux","item":"https:\/\/www.linuxdevops.cn\/linux\/"},{"@type":"ListItem","position":3,"name":"4.0 Map\uff0cFilter\u548cReduce"}]},{"@type":"WebSite","@id":"https:\/\/www.linuxdevops.cn\/#website","url":"https:\/\/www.linuxdevops.cn\/","name":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4","description":"Linux\u81ea\u52a8\u5316\u8fd0\u7ef4\u7b14\u8bb0","publisher":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1"},"alternateName":"linuxdevops","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.linuxdevops.cn\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-CN"},{"@type":["Person","Organization"],"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/3e206335d5796fdd8679e449df72a0d1","name":"\u7ba1\u7406\u5458","image":{"@type":"ImageObject","inLanguage":"zh-CN","@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/","url":"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg","contentUrl":"https:\/\/www.linuxdevops.cn\/wp-content\/uploads\/2019\/07\/cropped-index.jpg","width":512,"height":512,"caption":"\u7ba1\u7406\u5458"},"logo":{"@id":"https:\/\/www.linuxdevops.cn\/#\/schema\/person\/image\/"},"description":"\u7ba1\u7406\u5458","url":"https:\/\/www.linuxdevops.cn\/author\/root\/"}]}},"_links":{"self":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/513"}],"collection":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/comments?post=513"}],"version-history":[{"count":1,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/513\/revisions"}],"predecessor-version":[{"id":514,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/posts\/513\/revisions\/514"}],"wp:attachment":[{"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/media?parent=513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/categories?post=513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxdevops.cn\/wp-json\/wp\/v2\/tags?post=513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}