{"id":931113,"date":"2024-12-26T17:30:30","date_gmt":"2024-12-26T09:30:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/931113.html"},"modified":"2024-12-26T17:30:32","modified_gmt":"2024-12-26T09:30:32","slug":"python-%e5%a6%82%e4%bd%95%e8%ae%bf%e9%97%aemap","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/931113.html","title":{"rendered":"python \u5982\u4f55\u8bbf\u95eemap"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065544\/94735d6a-6056-42c6-8cd2-f99e755b06fa.webp\" alt=\"python \u5982\u4f55\u8bbf\u95eemap\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8bbf\u95ee<code>map<\/code>\u5bf9\u8c61\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3001\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u8fed\u4ee3\u3001\u4ee5\u53ca\u4f7f\u7528<code>next()<\/code>\u51fd\u6570\u83b7\u53d6\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\uff0c\u76f4\u63a5\u5c06<code>map<\/code>\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u6700\u4e3a\u4fbf\u6377\uff0c\u56e0\u4e3a\u8fd9\u6837\u4fbf\u4e8e\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c<code>map<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u6216\u5143\u7ec4\uff09\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u76f8\u540c\u7684\u51fd\u6570\u65f6\u3002<code>map<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a<code>map<\/code>\u5bf9\u8c61\uff0c\u8fd9\u662f\u4e00\u79cd\u8fed\u4ee3\u5668\uff0c\u5b83\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u5982\u5217\u8868\u6216\u5143\u7ec4\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u8bbf\u95ee\u548c\u4f7f\u7528<code>map<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>list()<\/code>\u8f6c\u6362<\/h3>\n<\/p>\n<p><p><code>map<\/code>\u5bf9\u8c61\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u6240\u6709\u5143\u7d20\uff0c\u901a\u5e38\u9700\u8981\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u8f6c\u6362\u4e3a\u5217\u8868<\/strong>\uff1a\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u5c06<code>map<\/code>\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u4ee5\u4fbf\u80fd\u591f\u8bbf\u95ee\u5176\u5143\u7d20\u5e76\u8fdb\u884c\u64cd\u4f5c\u3002\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b8c\u6210\u6b64\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>squared = map(lambda x: x2, numbers)<\/p>\n<p>squared_list = list(squared)<\/p>\n<p>print(squared_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>map<\/code>\u51fd\u6570\u5c06<code>lambda<\/code>\u51fd\u6570\u5e94\u7528\u4e8e\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a<code>map<\/code>\u5bf9\u8c61\u3002\u901a\u8fc7<code>list()<\/code>\u51fd\u6570\uff0c<code>map<\/code>\u5bf9\u8c61\u88ab\u8f6c\u6362\u4e3a\u5217\u8868<code>[1, 4, 9, 16, 25]<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u7f3a\u70b9<\/strong>\uff1a\u5c06<code>map<\/code>\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5143\u7d20\uff1b\u7f3a\u70b9\u662f\u5982\u679c\u6570\u636e\u96c6\u975e\u5e38\u5927\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u95ee\u9898\uff0c\u56e0\u4e3a\u6240\u6709\u5143\u7d20\u90fd\u540c\u65f6\u52a0\u8f7d\u5230\u4e86\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u8fed\u4ee3<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u7acb\u5373\u751f\u6210\u5b8c\u6574\u7684\u5217\u8868\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>for<\/code>\u5faa\u73af\u9010\u4e2a\u8bbf\u95ee<code>map<\/code>\u5bf9\u8c61\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u8fed\u4ee3\u8bbf\u95ee<\/strong>\uff1a\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u53ef\u4ee5\u9010\u4e2a\u8bbf\u95ee<code>map<\/code>\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\uff0c\u800c\u4e0d\u9700\u8981\u5c06\u5b83\u4eec\u5168\u90e8\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squared = map(lambda x: x2, numbers)<\/p>\n<p>for num in squared:<\/p>\n<p>    print(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>map<\/code>\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5143\u7d20\u5728\u5faa\u73af\u4e2d\u88ab\u9010\u4e2a\u8bbf\u95ee\u5e76\u6253\u5370\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u7f3a\u70b9<\/strong>\uff1a\u9010\u4e2a\u8fed\u4ee3\u7684\u4f18\u70b9\u662f\u8282\u7701\u5185\u5b58\uff0c\u56e0\u4e3a\u6ca1\u6709\u751f\u6210\u5b8c\u6574\u7684\u5217\u8868\uff1b\u7f3a\u70b9\u662f\u65e0\u6cd5\u5728\u8fed\u4ee3\u7ed3\u675f\u540e\u518d\u6b21\u8bbf\u95ee\u540c\u4e00<code>map<\/code>\u5bf9\u8c61\uff0c\u56e0\u4e3a\u8fed\u4ee3\u5668\u53ea\u80fd\u88ab\u6d88\u8017\u4e00\u6b21\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>next()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>next()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u8bbf\u95ee<code>map<\/code>\u5bf9\u8c61\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5355\u4e2a\u5143\u7d20\u8bbf\u95ee<\/strong>\uff1a\u5982\u679c\u53ea\u9700\u8bbf\u95ee\u4e00\u4e2a\u6216\u51e0\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>next()<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squared = map(lambda x: x2, numbers)<\/p>\n<p>print(next(squared))  # \u8f93\u51fa1<\/p>\n<p>print(next(squared))  # \u8f93\u51fa4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6bcf\u8c03\u7528\u4e00\u6b21<code>next()<\/code>\uff0c\u8fed\u4ee3\u5668\u5c31\u4f1a\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u7f3a\u70b9<\/strong>\uff1a\u4f7f\u7528<code>next()<\/code>\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u5408\u8bbf\u95ee\u5c11\u91cf\u5143\u7d20\uff1b\u7f3a\u70b9\u662f\u9700\u8981\u5c0f\u5fc3\u5904\u7406<code>StopIteration<\/code>\u5f02\u5e38\uff0c\u56e0\u4e3a\u5728\u8bbf\u95ee\u8d85\u51fa\u8303\u56f4\u65f6\u4f1a\u629b\u51fa\u6b64\u5f02\u5e38\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5355\u6b21\u8fed\u4ee3<\/strong>\uff1a<code>map<\/code>\u5bf9\u8c61\u662f\u60f0\u6027\u8ba1\u7b97\u7684\u8fed\u4ee3\u5668\uff0c\u5728\u8bbf\u95ee\u5143\u7d20\u65f6\u624d\u4f1a\u8ba1\u7b97\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u4e00\u65e6\u8fed\u4ee3\u5b8c\u6210\uff0c\u5c31\u65e0\u6cd5\u518d\u6b21\u8fed\u4ee3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u8003\u8651<\/strong>\uff1a\u5728\u9700\u8981\u591a\u6b21\u8bbf\u95ee\u6216\u5904\u7406\u7ed3\u679c\u65f6\uff0c\u5c06<code>map<\/code>\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u901a\u5e38\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u4f46\u8981\u6ce8\u610f\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u51fd\u6570\u526f\u4f5c\u7528<\/strong>\uff1a\u786e\u4fdd\u4f20\u9012\u7ed9<code>map<\/code>\u7684\u51fd\u6570\u662f\u65e0\u526f\u4f5c\u7528\u7684\uff0c\u5373\u4e0d\u5e94\u4fee\u6539\u8f93\u5165\u6570\u636e\u6216\u4f9d\u8d56\u5916\u90e8\u72b6\u6001\uff0c\u4ee5\u907f\u514d\u610f\u5916\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<ul>\n<li>\n<p><strong>\u6570\u636e\u8f6c\u6362<\/strong>\uff1a<code>map<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u8f6c\u6362\uff0c\u4f8b\u5982\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_numbers = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;]<\/p>\n<p>int_numbers = list(map(int, str_numbers))<\/p>\n<p>print(int_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u6e05\u7406<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u7406\u6216\u683c\u5f0f\u5316\uff0c\u4f8b\u5982\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">raw_data = [&#39;  hello &#39;, &#39; world &#39;, &#39; python &#39;]<\/p>\n<p>cleaned_data = list(map(str.strip, raw_data))<\/p>\n<p>print(cleaned_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ec4\u5408\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61<\/strong>\uff1a<code>map<\/code>\u51fd\u6570\u53ef\u4ee5\u7ed3\u5408<code>lambda<\/code>\u51fd\u6570\u548c\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = [1, 2, 3]<\/p>\n<p>b = [4, 5, 6]<\/p>\n<p>summed = list(map(lambda x, y: x + y, a, b))<\/p>\n<p>print(summed)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u8bbf\u95ee\u548c\u4f7f\u7528<code>map<\/code>\u5bf9\u8c61\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u65e2\u80fd\u5145\u5206\u5229\u7528<code>map<\/code>\u7684\u60f0\u6027\u8ba1\u7b97\u7279\u6027\uff0c\u53c8\u80fd\u6709\u6548\u7ba1\u7406\u5185\u5b58\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2aMap\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0cMap\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\uff08dictionary\uff09\u6765\u5b9e\u73b0\u3002\u5b57\u5178\u662f\u4e00\u79cd\u53ef\u53d8\u7684\u3001\u65e0\u5e8f\u7684\u96c6\u5408\uff0c\u4f7f\u7528\u952e\u503c\u5bf9\u6765\u5b58\u50a8\u6570\u636e\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u6765\u521b\u5efa\u5b57\u5178\uff0c\u6216\u8005\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_map = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}\n<\/code><\/pre>\n<p>\u901a\u8fc7\u952e\uff08\u5982 &#39;name&#39;\uff09\u53ef\u4ee5\u5feb\u901f\u8bbf\u95ee\u76f8\u5e94\u7684\u503c\uff08\u5982 &#39;Alice&#39;\uff09\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u7279\u5b9a\u503c\uff1f<\/strong><br \/>\u8981\u4ece\u5b57\u5178\u4e2d\u8bbf\u95ee\u7279\u5b9a\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u952e\u6765\u7d22\u5f15\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8bbf\u95ee\u4e0a\u8ff0\u5b57\u5178\u4e2d\u7684 &#39;age&#39;\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">age = my_map[&#39;age&#39;]\nprint(age)  # \u8f93\u51fa\uff1a30\n<\/code><\/pre>\n<p>\u5982\u679c\u5c1d\u8bd5\u8bbf\u95ee\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u952e\uff0cPython\u4f1a\u629b\u51fa <code>KeyError<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u904d\u5386\u4e00\u4e2aMap\uff08\u5b57\u5178\uff09\uff1f<\/strong><br \/>\u904d\u5386\u5b57\u5178\u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u6765\u83b7\u53d6\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\u3002\u4e0b\u9762\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u904d\u5386\u65b9\u5f0f\uff1a  <\/p>\n<ul>\n<li>\u53ea\u904d\u5386\u952e\uff1a\n<pre><code class=\"language-python\">for key in my_map:\n    print(key)  # \u8f93\u51fa\u6240\u6709\u952e\n<\/code><\/pre>\n<\/li>\n<li>\u53ea\u904d\u5386\u503c\uff1a\n<pre><code class=\"language-python\">for value in my_map.values():\n    print(value)  # \u8f93\u51fa\u6240\u6709\u503c\n<\/code><\/pre>\n<\/li>\n<li>\u904d\u5386\u952e\u503c\u5bf9\uff1a\n<pre><code class=\"language-python\">for key, value in my_map.items():\n    print(key, value)  # \u8f93\u51fa\u6240\u6709\u952e\u503c\u5bf9\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u8bbf\u95ee\u548c\u5904\u7406Map\u4e2d\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8bbf\u95eemap\u5bf9\u8c61\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528list()\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3001\u4f7f\u7528for\u5faa\u73af\u8fed\u4ee3\u3001\u4ee5\u53ca\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":931116,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/931113"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=931113"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/931113\/revisions"}],"predecessor-version":[{"id":931117,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/931113\/revisions\/931117"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/931116"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=931113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=931113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=931113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}