{"id":2767,"date":"2025-05-28T17:19:54","date_gmt":"2025-06-04T09:45:54","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2767"},"modified":"2025-06-04T17:19:54","modified_gmt":"2025-06-04T09:45:54","slug":"%e4%bd%bf%e7%94%a8python%e7%9a%84map%e5%87%bd%e6%95%b8%e7%b0%a1%e5%96%ae%e6%93%8d%e4%bd%9c%e5%ba%8f%e5%88%97","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%bd%bf%e7%94%a8python%e7%9a%84map%e5%87%bd%e6%95%b8%e7%b0%a1%e5%96%ae%e6%93%8d%e4%bd%9c%e5%ba%8f%e5%88%97\/","title":{"rendered":"2025 \u6700\u65b0\u7248 Python \u7a0b\u5f0f\u6559\u5b78\uff1a\u6df1\u5165\u4e86\u89e3 map() \u51fd\u6578\u64cd\u4f5c\u5e8f\u5217"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, map()\u51fd\u6578, \u5e8f\u5217, \u64cd\u4f5c, Python\u6559\u5b78, 2025\"><\/p>\n<h1>2025 \u6700\u65b0\u7248 Python \u7a0b\u5f0f\u6559\u5b78\uff1a\u6df1\u5165\u4e86\u89e3 map() \u51fd\u6578\u64cd\u4f5c\u5e8f\u5217<\/h1>\n<p>Python\u7684 `map()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5c0d\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u9032\u884c\u64cd\u4f5c\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u5e8f\u5217\u3002\u9019\u4f7f\u5f97\u5b83\u5728\u8655\u7406\u8cc7\u6599\u6642\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u9032\u884c\u6279\u91cf\u64cd\u4f5c\u7684\u60c5\u6cc1\u4e0b\uff0c\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u7a0b\u5f0f\u7684\u6548\u7387\uff0c\u8b93\u6211\u5011\u4e0d\u5fc5\u624b\u52d5\u4f7f\u7528\u8ff4\u5708\u9032\u884c\u8655\u7406\u3002<\/p>\n<p>## map() \u51fd\u6578\u7684\u8a9e\u6cd5<\/p>\n<p>`map()` \u51fd\u6578\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmap(function, iterable, &#8230;)<br \/>\n&#8220;`<\/p>\n<p>&#8211; **function**\uff1a\u8981\u57f7\u884c\u7684\u51fd\u6578\uff0c\u53ef\u4ee5\u662f\u5167\u5efa\u51fd\u6578\u6216\u81ea\u5b9a\u7fa9\u51fd\u6578\u3002<br \/>\n&#8211; **iterable**\uff1a\u8981\u88ab\u64cd\u4f5c\u7684\u5e8f\u5217\uff0c\u4f8b\u5982 `list`\u3001`tuple`\u3001`dict` \u7b49\u3002<\/p>\n<p>## \u5be6\u4f5c\u7bc4\u4f8b<\/p>\n<p>### \u5c07\u5217\u8868\u4e2d\u7684\u5b57\u4e32\u8f49\u63db\u70ba\u5c0f\u5beb<\/p>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u5217\u8868\uff0c\u88e1\u9762\u7684\u5143\u7d20\u90fd\u662f\u5b57\u4e32\uff0c\u6211\u5011\u60f3\u8981\u5c07\u5b83\u5011\u8f49\u63db\u70ba\u5c0f\u5beb\u3002\u53ef\u4ee5\u4f7f\u7528 `map()` \u51fd\u6578\u4f86\u9054\u6210\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_list = [&#8216;Apple&#8217;, &#8216;Banana&#8217;, &#8216;Orange&#8217;]\n<p>lower_list = list(map(str.lower, my_list))<\/p>\n<p>print(lower_list)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\uff1a<\/p>\n<p>&#8220;`python<br \/>\n[&#8216;apple&#8217;, &#8216;banana&#8217;, &#8216;orange&#8217;]\n&#8220;`<\/p>\n<p>\u5982\u4e0a\u6240\u793a\uff0c`map()` \u51fd\u6578\u6210\u529f\u5c07 `my_list` \u4e2d\u7684\u6bcf\u500b\u5143\u7d20\u8f49\u63db\u70ba\u5c0f\u5beb\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u5217\u8868\u3002<\/p>\n<p>### \u5c0d\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u9032\u884c\u64cd\u4f5c<\/p>\n<p>\u9664\u4e86\u5c0d\u5217\u8868\u9032\u884c\u64cd\u4f5c\uff0c`map()` \u51fd\u6578\u4e5f\u53ef\u4ee5\u5c0d\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u9032\u884c\u8655\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u5c07\u5b57\u5178\u4e2d\u6240\u6709\u5b57\u4e32\u503c\u8f49\u63db\u70ba\u5c0f\u5beb\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_dict = {&#8216;A&#8217;: &#8216;Apple&#8217;, &#8216;B&#8217;: &#8216;Banana&#8217;, &#8216;O&#8217;: &#8216;Orange&#8217;}<\/p>\n<p>lower_dict = dict(map(lambda x: (x[0], x[1].lower()), my_dict.items()))<\/p>\n<p>print(lower_dict)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\uff1a<\/p>\n<p>&#8220;`python<br \/>\n{&#8216;A&#8217;: &#8216;apple&#8217;, &#8216;B&#8217;: &#8216;banana&#8217;, &#8216;O&#8217;: &#8216;orange&#8217;}<br \/>\n&#8220;`<\/p>\n<p>\u5f9e\u4ee5\u4e0a\u7bc4\u4f8b\u6211\u5011\u53ef\u4ee5\u770b\u5230\uff0c`map()` \u51fd\u6578\u5c0d\u5b57\u5178\u4e2d\u7684\u6bcf\u500b\u5143\u7d20\u9032\u884c\u4e86\u5c0f\u5beb\u8f49\u63db\uff0c\u4e26\u6210\u529f\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u5b57\u5178\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u7576\u4f7f\u7528 `map()` \u51fd\u6578\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<br \/>\n1. **Function \u4e0d\u53ef\u7528**\uff1a\u78ba\u4fdd\u50b3\u5165\u7684\u51fd\u6578\u662f\u6b63\u78ba\u7684\uff0c\u4e26\u4e14\u53ef\u4ee5\u63a5\u53d7\u50b3\u5165\u7684\u53c3\u6578\u985e\u578b\u3002<br \/>\n2. **Iterable \u7a7a\u503c**\uff1a\u5982\u679c\u50b3\u5165\u7684 iterable \u662f\u7a7a\u7684\uff0c`map()` \u51fd\u6578\u5c07\u8fd4\u56de\u7a7a\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`map()` \u51fd\u6578\u4e0d\u50c5\u9650\u65bc\u7c21\u55ae\u7684\u6578\u64da\u8f49\u63db\uff0c\u9084\u53ef\u4ee5\u7d50\u5408\u5176\u4ed6\u51fd\u6578\uff0c\u5982 `filter()` \u548c `reduce()`\uff0c\u4f86\u9032\u884c\u66f4\u8907\u96dc\u7684\u6578\u64da\u8655\u7406\u3002\u4f60\u53ef\u4ee5\u53c3\u8003 [\u9019\u7bc7\u6587\u7ae0](https:\/\/vocus.cc\/article\/5f0b1a4e8fd0a800012c8d18) \u77ad\u89e3\u5982\u4f55\u4f7f\u7528\u9019\u4e9b\u51fd\u6578\u4f86\u9032\u884c\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cPython \u7684 `map()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u8b93\u6211\u5011\u66f4\u5feb\u901f\u5730\u5c0d\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u9032\u884c\u64cd\u4f5c\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528\u8ff4\u5708\u3002\u5b83\u53ef\u4ee5\u9748\u6d3b\u5730\u61c9\u7528\u65bc\u5217\u8868\u548c\u5b57\u5178\u4e2d\uff0c\u4e26\u8fd4\u56de\u65b0\u7684\u5e8f\u5217\u6216\u5b57\u5178\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>### Q1: `map()` \u51fd\u6578\u662f\u5426\u80fd\u8207\u81ea\u5b9a\u7fa9\u51fd\u6578\u4e00\u8d77\u4f7f\u7528\uff1f<br \/>\n**A1:** \u662f\u7684\uff0c`map()` \u51fd\u6578\u53ef\u4ee5\u8207\u4efb\u4f55\u53ef\u4ee5\u63a5\u53d7\u4e00\u500b\u53c3\u6578\u7684\u51fd\u6578\u4e00\u8d77\u4f7f\u7528\uff0c\u5305\u62ec\u81ea\u5b9a\u7fa9\u51fd\u6578\u3002<\/p>\n<p>### Q2: \u5982\u4f55\u5c07 `map()` \u51fd\u6578\u7684\u7d50\u679c\u8f49\u63db\u70ba\u5176\u4ed6\u8cc7\u6599\u985e\u578b\uff1f<br \/>\n**A2:** \u53ef\u4ee5\u4f7f\u7528 `list()`\u3001`tuple()` \u6216 `set()` \u5c07 `map()` \u51fd\u6578\u7684\u8fd4\u56de\u7d50\u679c\u8f49\u63db\u70ba\u76f8\u61c9\u7684\u8cc7\u6599\u985e\u578b\u3002<\/p>\n<p>### Q3: \u4f7f\u7528 `map()` \u51fd\u6578\u6642\u5982\u4f55\u8655\u7406\u591a\u500b iterable\uff1f<br \/>\n**A3:** `map()` \u51fd\u6578\u53ef\u4ee5\u63a5\u53d7\u591a\u500b iterable\uff0c\u4e26\u5c07\u5b83\u5011\u6309\u4f4d\u7f6e\u50b3\u905e\u7d66\u6307\u5b9a\u7684\u51fd\u6578\u3002\u4f8b\u5982\uff0c`map(func, iterable1, iterable2)` \u5c07\u4f9d\u6b21\u5c07 `iterable1` \u548c `iterable2` \u7684\u5143\u7d20\u50b3\u905e\u7d66 `func`\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7684map()\u51fd\u6578\u53ef\u4ee5\u8b93\u4f60\u8f15\u9b06\u5730\u5c0d\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u9032\u884c\u64cd\u4f5c\uff0c\u8b93\u4f60\u7684\u7a0b\u5f0f\u78bc\u66f4\u52a0\u7c21\u6f54\u3001\u9ad8\u6548\u3002\u672c\u6587\u5c07\u4ecb\u7d39map()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u4f86\u64cd\u4f5c\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-2767","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-ID","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2767"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2767\/revisions"}],"predecessor-version":[{"id":2768,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2767\/revisions\/2768"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}