﻿{"id":1468,"date":"2020-06-10T12:29:42","date_gmt":"2020-06-10T04:29:42","guid":{"rendered":"https:\/\/www.tansoo.cn\/?p=1468"},"modified":"2020-06-10T12:29:42","modified_gmt":"2020-06-10T04:29:42","slug":"phpbb-34","status":"publish","type":"post","link":"https:\/\/www.tansoo.cn\/?p=1468","title":{"rendered":"python\u4e2dmap\u7684\u7528\u6cd5"},"content":{"rendered":"<p><span style=\"font-size:150%;line-height:116%\"><strong class=\"text-strong\"><span style=\"color:#FF0000\">Map<\/span><\/strong><\/span><br \/>\nMap\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<p><strong class=\"text-strong\">\u89c4\u8303<\/strong><br \/>\nmap(function_to_apply, list_of_inputs)<br \/>\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<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">items <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: black;\">&#91;<\/span><span style=\"color: #ff4500;\">1<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">3<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">4<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">5<\/span><span style=\"color: black;\">&#93;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">squared <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: black;\">&#91;<\/span><span style=\"color: black;\">&#93;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">for<\/span> i <span style=\"color: #ff7700;font-weight:bold;\">in<\/span> items:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; squared.<span style=\"color: black;\">append<\/span><span style=\"color: black;\">&#40;<\/span>i**<span style=\"color: #ff4500;\">2<\/span><span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p>Map\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<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">items <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: black;\">&#91;<\/span><span style=\"color: #ff4500;\">1<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">3<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">4<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff4500;\">5<\/span><span style=\"color: black;\">&#93;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">squared <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #008000;\">list<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #008000;\">map<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff7700;font-weight:bold;\">lambda<\/span> x: x**<span style=\"color: #ff4500;\">2<\/span><span style=\"color: #66cc66;\">,<\/span> items<span style=\"color: black;\">&#41;<\/span><span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p>\u5927\u591a\u6570\u65f6\u5019\uff0c\u6211\u4eec\u4f7f\u7528\u533f\u540d\u51fd\u6570(lambdas)\u6765\u914d\u5408map, \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<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">def<\/span> multiply<span style=\"color: black;\">&#40;<\/span>x<span style=\"color: black;\">&#41;<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">return<\/span> <span style=\"color: black;\">&#40;<\/span>x*x<span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">def<\/span> add<span style=\"color: black;\">&#40;<\/span>x<span style=\"color: black;\">&#41;<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">return<\/span> <span style=\"color: black;\">&#40;<\/span>x+x<span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">funcs <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: black;\">&#91;<\/span>multiply<span style=\"color: #66cc66;\">,<\/span> add<span style=\"color: black;\">&#93;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">for<\/span> i <span style=\"color: #ff7700;font-weight:bold;\">in<\/span> <span style=\"color: #008000;\">range<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff4500;\">5<\/span><span style=\"color: black;\">&#41;<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; value <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #008000;\">map<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff7700;font-weight:bold;\">lambda<\/span> x: x<span style=\"color: black;\">&#40;<\/span>i<span style=\"color: black;\">&#41;<\/span><span style=\"color: #66cc66;\">,<\/span> funcs<span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">print<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #008000;\">list<\/span><span style=\"color: black;\">&#40;<\/span>value<span style=\"color: black;\">&#41;<\/span><span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #808080; font-style: italic;\"># \u8bd1\u8005\u6ce8\uff1a\u4e0a\u9762print\u65f6\uff0c\u52a0\u4e86list\u8f6c\u6362\uff0c\u662f\u4e3a\u4e86python2\/3\u7684\u517c\u5bb9\u6027<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #808080; font-style: italic;\"># &nbsp; &nbsp; &nbsp; &nbsp;\u5728python2\u4e2dmap\u76f4\u63a5\u8fd4\u56de\u5217\u8868\uff0c\u4f46\u5728python3\u4e2d\u8fd4\u56de\u8fed\u4ee3\u5668<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #808080; font-style: italic;\"># &nbsp; &nbsp; &nbsp; &nbsp;\u56e0\u6b64\u4e3a\u4e86\u517c\u5bb9python3, \u9700\u8981list\u8f6c\u6362\u4e00\u4e0b<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># Output:<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># [0, 0]<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># [1, 2]<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># [4, 4]<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># [9, 6]<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># [16, 8]<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p><small>\u539f\u6587\u4f5c\u8005: BG6RSH | <a href=\"https:\/\/bbs.tansoo.cn\/viewtopic.php?t=34\" target=\"_blank\" rel=\"noopener\">\u67e5\u770b\u539f\u5e16<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Map Map\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 \u89c4\u8303  &hellip; <a href=\"https:\/\/www.tansoo.cn\/?p=1468\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[],"class_list":["post-1468","post","type-post","status-publish","format-standard","hentry","category-105"],"_links":{"self":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1468"}],"version-history":[{"count":0,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1468\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}