{"id":1136607,"date":"2025-01-08T21:37:58","date_gmt":"2025-01-08T13:37:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1136607.html"},"modified":"2025-01-08T21:38:13","modified_gmt":"2025-01-08T13:38:13","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%9c%a8%e4%b8%80%e8%a1%8c%e4%b8%ad%e8%be%93%e5%85%a5%e5%a4%9a%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1136607.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u5728\u4e00\u884c\u4e2d\u8f93\u5165\u591a\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100712\/06f7228b-b903-441a-bd2e-6247c967f7ad.webp\" alt=\"Python\u4e2d\u5982\u4f55\u5728\u4e00\u884c\u4e2d\u8f93\u5165\u591a\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>input().split()<\/code>\u3001<code>map()<\/code>\u51fd\u6570\u3001\u5217\u8868\u89e3\u6790\u7b49\u65b9\u5f0f\u53ef\u4ee5\u5728\u4e00\u884c\u4e2d\u8f93\u5165\u591a\u4e2a\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7<code>input().split()<\/code>\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u7136\u540e\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u5c06\u5206\u5272\u540e\u7684\u5b57\u7b26\u4e32\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u3001\u6613\u61c2\u4e14\u975e\u5e38\u5b9e\u7528\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\uff0c\u5e76\u63a2\u8ba8\u5176\u4ed6\u51e0\u79cd\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>input().split()<\/code> \u548c <code>map()<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u8fd9\u662f\u4e00\u79cd\u975e\u5e38\u6d41\u884c\u4e14\u65b9\u4fbf\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5feb\u901f\u5c06\u4e00\u884c\u8f93\u5165\u7684\u591a\u4e2a\u6570\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">input_string = input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u6570\uff0c\u4ee5\u7a7a\u683c\u5206\u9694: &quot;)<\/p>\n<p>numbers = list(map(int, input_string.split()))<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u5b57\u662f:&quot;, numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8be6\u7ec6\u89e3\u91ca<\/h4>\n<\/p>\n<ul>\n<li><strong><code>input()<\/code><\/strong>: \u8be5\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/li>\n<li><strong><code>.split()<\/code><\/strong>: \u8be5\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u7a7a\u683c\u5206\u5272\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\u7684\u5217\u8868\u3002<\/li>\n<li><strong><code>map(int, ...)<\/code><\/strong>: \u8be5\u51fd\u6570\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u4e0a\u8ff0\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [int(x) for x in input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u6570\uff0c\u4ee5\u7a7a\u683c\u5206\u9694: &quot;).split()]<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u5b57\u662f:&quot;, numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 <code>sys.stdin.read()<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u5927\u91cf\u6570\u636e\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>sys.stdin.read()<\/code> \u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>input_data = sys.stdin.read().strip()<\/p>\n<p>numbers = list(map(int, input_data.split()))<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u5b57\u662f:&quot;, numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u8f93\u5165\u5206\u9694\u7b26<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u8f93\u5165\u7684\u591a\u4e2a\u6570\u53ef\u80fd\u4f7f\u7528\u4e0d\u540c\u7684\u5206\u9694\u7b26\uff0c\u6bd4\u5982\u9017\u53f7\u3001\u5206\u53f7\u7b49\u3002\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>input_string = input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u6570\uff0c\u4ee5\u9017\u53f7\u5206\u9694: &quot;)<\/p>\n<p>numbers = list(map(int, re.split(r&#39;[,\\s]+&#39;, input_string)))<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u5b57\u662f:&quot;, numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u8f93\u5165\u9519\u8bef\u548c\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u53ef\u80fd\u4e0d\u7b26\u5408\u9884\u671f\uff0c\u56e0\u6b64\u9700\u8981\u5904\u7406\u8f93\u5165\u9519\u8bef\u548c\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    try:<\/p>\n<p>        input_string = input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u6570\uff0c\u4ee5\u7a7a\u683c\u5206\u9694: &quot;)<\/p>\n<p>        numbers = list(map(int, input_string.split()))<\/p>\n<p>        break<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(&quot;\u8f93\u5165\u7684\u5185\u5bb9\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u5b57\u662f:&quot;, numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e00\u884c\u4e2d\u8f93\u5165\u591a\u4e2a\u6570\uff0c\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528<code>input().split()<\/code>\u548c<code>map()<\/code>\u51fd\u6570\u7684\u7ec4\u5408\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u3001\u6613\u61c2\u4e14\u975e\u5e38\u5b9e\u7528<\/strong>\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001<code>sys.stdin.read()<\/code>\u65b9\u6cd5\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u8f93\u5165\u5206\u9694\u7b26\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u6ce8\u610f\u5904\u7406\u8f93\u5165\u9519\u8bef\u548c\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u5728\u4e00\u884c\u4e2d\u8f93\u5165\u591a\u4e2a\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u6765\u63a5\u6536\u7528\u6237\u8f93\u5165\uff0c\u5e76\u901a\u8fc7<code>split()<\/code>\u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8f93\u5165\u7684\u6570\u503c\u4f1a\u88ab\u4f5c\u4e3a\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u6570\u503c\u7c7b\u578b\uff0c\u6bd4\u5982\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u591a\u4e2a\u6570\u5b57\uff0c\u4f7f\u7528<code>input().split()<\/code>\u5c06\u8fd9\u4e9b\u6570\u5b57\u5206\u5272\u5f00\uff0c\u5e76\u901a\u8fc7<code>map(int, ...)<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5f53\u7528\u6237\u8f93\u5165\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u9519\u8bef\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\u8f93\u5165\u5b57\u6bcd\u6216\u7279\u6b8a\u5b57\u7b26\u800c\u4e0d\u662f\u6570\u5b57\u3002\u4e3a\u4e86\u63d0\u5347\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u6765\u6355\u6349\u8f93\u5165\u9519\u8bef\uff0c\u7ed9\u51fa\u76f8\u5e94\u7684\u63d0\u793a\u4fe1\u606f\uff0c\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u8f93\u5165\u9519\u8bef\u800c\u5d29\u6e83\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u591a\u4e2a\u6570\u503c\u65f6\uff0c\u5982\u4f55\u9009\u62e9\u5206\u9694\u7b26\uff1f<\/strong><br \/>\u867d\u7136\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>split()<\/code>\u65b9\u6cd5\u4f1a\u4ee5\u7a7a\u683c\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u4f46\u7528\u6237\u53ef\u4ee5\u5728\u8f93\u5165\u65f6\u9009\u62e9\u4e0d\u540c\u7684\u5206\u9694\u7b26\uff0c\u4f8b\u5982\u9017\u53f7\u6216\u5206\u53f7\u3002\u901a\u8fc7\u5728<code>split()<\/code>\u4e2d\u6307\u5b9a\u5206\u9694\u7b26\uff0c\u53ef\u4ee5\u7075\u6d3b\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u8f93\u5165\u3002\u4f8b\u5982\uff0c<code>input().split(&#39;,&#39;)<\/code>\u5c06\u4ee5\u9017\u53f7\u4e3a\u5206\u9694\u7b26\uff0c\u9002\u5408\u8f93\u5165\u4ee5\u9017\u53f7\u5206\u9694\u7684\u6570\u503c\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528input().split()\u3001map()\u51fd\u6570\u3001\u5217\u8868\u89e3\u6790\u7b49\u65b9\u5f0f\u53ef\u4ee5\u5728\u4e00\u884c\u4e2d\u8f93\u5165\u591a\u4e2a\u6570\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1136631,"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\/1136607"}],"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=1136607"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136607\/revisions"}],"predecessor-version":[{"id":1136635,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136607\/revisions\/1136635"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1136631"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1136607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1136607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1136607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}