{"id":180415,"date":"2023-08-03T10:43:07","date_gmt":"2023-08-03T02:43:07","guid":{"rendered":"http:\/\/www.idc.net\/help\/180415\/"},"modified":"2023-08-03T10:43:07","modified_gmt":"2023-08-03T02:43:07","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython%e4%b8%ad%e6%9c%89map%e6%96%b9%e6%b3%95%e5%90%97","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/180415\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1apython\u4e2d\u6709map\u65b9\u6cd5\u5417"},"content":{"rendered":"<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/article\/000\/000\/004\/5dd35f8105c79343.gif\" class=\"aligncenter\"><\/p>\n<p>map()&nbsp;\u4f1a\u6839\u636e\u63d0\u4f9b\u7684\u51fd\u6570\u5bf9\u6307\u5b9a\u5e8f\u5217\u505a\u6620\u5c04\u3002<\/p>\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570 function \u4ee5\u53c2\u6570\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u8c03\u7528 function \u51fd\u6570\uff0c\u8fd4\u56de\u5305\u542b\u6bcf\u6b21 function \u51fd\u6570\u8fd4\u56de\u503c\u7684\u65b0\u5217\u8868\u3002<\/p>\n<p>map() \u51fd\u6570\u8bed\u6cd5\uff1a<\/p>\n<pre>map(function,&nbsp;iterable,&nbsp;...)<\/pre>\n<p>\u53c2\u6570<\/p>\n<pre>function&nbsp;--&nbsp;\u51fd\u6570\niterable&nbsp;--&nbsp;\u4e00\u4e2a\u6216\u591a\u4e2a\u5e8f\u5217<\/pre>\n<p>python\u5b66\u4e60\u7f51\uff0c\u514d\u8d39\u7684python\u5b66\u4e60\u7f51\u7ad9\uff0c\u6b22\u8fce\u5728\u7ebf\u5b66\u4e60\uff01<\/p>\n<p>\u8fd4\u56de\u503c<\/p>\n<pre>Python&nbsp;2.x&nbsp;\u8fd4\u56de\u5217\u8868\u3002\nPython&nbsp;3.x&nbsp;\u8fd4\u56de\u8fed\u4ee3\u5668\u3002<\/pre>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u5c55\u793a\u4e86 map() \u7684\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<pre>&gt;&gt;&gt;&nbsp;def&nbsp;square(x)&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u8ba1\u7b97\u5e73\u65b9\u6570\n...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;x&nbsp;**&nbsp;2\n...\n&gt;&gt;&gt;&nbsp;map(square,&nbsp;[1,2,3,4,5])&nbsp;&nbsp;&nbsp;#&nbsp;\u8ba1\u7b97\u5217\u8868\u5404\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\n[1,&nbsp;4,&nbsp;9,&nbsp;16,&nbsp;25]\n&gt;&gt;&gt;&nbsp;map(lambda&nbsp;x:&nbsp;x&nbsp;**&nbsp;2,&nbsp;[1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5])&nbsp;&nbsp;#&nbsp;\u4f7f\u7528&nbsp;lambda&nbsp;\u533f\u540d\u51fd\u6570\n[1,&nbsp;4,&nbsp;9,&nbsp;16,&nbsp;25]\n\n#&nbsp;\u63d0\u4f9b\u4e86\u4e24\u4e2a\u5217\u8868\uff0c\u5bf9\u76f8\u540c\u4f4d\u7f6e\u7684\u5217\u8868\u6570\u636e\u8fdb\u884c\u76f8\u52a0\n&gt;&gt;&gt;&nbsp;map(lambda&nbsp;x,&nbsp;y:&nbsp;x&nbsp;+&nbsp;y,&nbsp;[1,&nbsp;3,&nbsp;5,&nbsp;7,&nbsp;9],&nbsp;[2,&nbsp;4,&nbsp;6,&nbsp;8,&nbsp;10])\n[3,&nbsp;7,&nbsp;11,&nbsp;15,&nbsp;19]<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>map()&nbsp;\u4f1a\u6839\u636e\u63d0\u4f9b\u7684\u51fd\u6570\u5bf9\u6307\u5b9a\u5e8f\u5217\u505a\u6620\u5c04\u3002 \u7b2c\u4e00\u4e2a\u53c2\u6570 function \u4ee5\u53c2\u6570\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":180416,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-180415","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/180415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=180415"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/180415\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/180416"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=180415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=180415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=180415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}