{"id":1041558,"date":"2024-12-31T12:50:13","date_gmt":"2024-12-31T04:50:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1041558.html"},"modified":"2024-12-31T12:50:15","modified_gmt":"2024-12-31T04:50:15","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%90%8c%e6%97%b6%e8%be%93%e5%85%a5%e4%b8%a4%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1041558.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u540c\u65f6\u8f93\u5165\u4e24\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/88162b90-b857-4191-874a-17865df0ecd6.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u540c\u65f6\u8f93\u5165\u4e24\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u540c\u65f6\u8f93\u5165\u4e24\u4e2a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528input\u51fd\u6570\u3001split\u65b9\u6cd5\u3001\u6216\u8005\u901a\u8fc7map\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u7684\u5b9e\u73b0\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528input\u51fd\u6570\u548csplit\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u4e14\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528input\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528split\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u5206\u5272<\/p>\n<p>input_str = input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u6570\uff0c\u4ee5\u7a7a\u683c\u5206\u9694: &quot;)<\/p>\n<p>num1, num2 = map(int, input_str.split())<\/p>\n<p>print(&quot;\u7b2c\u4e00\u4e2a\u6570\u662f:&quot;, num1)<\/p>\n<p>print(&quot;\u7b2c\u4e8c\u4e2a\u6570\u662f:&quot;, num2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u7684\u4e24\u4e2a\u6570\u901a\u8fc7\u7a7a\u683c\u5206\u9694\uff0c\u7136\u540e\u4f7f\u7528split\u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e24\u4e2a\u90e8\u5206\uff0c\u518d\u5c06\u8fd9\u4e24\u4e2a\u90e8\u5206\u8f6c\u6362\u4e3a\u6574\u6570\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528map\u51fd\u6570\u548csplit\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u4f7f\u7528split\u65b9\u6cd5\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u7ed3\u5408map\u51fd\u6570\u6765\u5b9e\u73b0\u540c\u6837\u7684\u529f\u80fd\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u591a\u4e2a\u8f93\u5165\u65f6\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u5206\u5272<\/p>\n<p>num1, num2 = map(int, input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u6570\uff0c\u4ee5\u7a7a\u683c\u5206\u9694: &quot;).split())<\/p>\n<p>print(&quot;\u7b2c\u4e00\u4e2a\u6570\u662f:&quot;, num1)<\/p>\n<p>print(&quot;\u7b2c\u4e8c\u4e2a\u6570\u662f:&quot;, num2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528map\u51fd\u6570\u76f4\u63a5\u5c06\u5206\u5272\u540e\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u51cf\u5c11\u4e86\u4e2d\u95f4\u53d8\u91cf\u7684\u4f7f\u7528\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u66f4\u52a0\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u591a\u4e2a\u8f93\u5165\u7684\u60c5\u51b5\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u66f4\u52a0\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u5206\u5272<\/p>\n<p>nums = [int(x) for x in input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u6570\uff0c\u4ee5\u7a7a\u683c\u5206\u9694: &quot;).split()]<\/p>\n<p>print(&quot;\u7b2c\u4e00\u4e2a\u6570\u662f:&quot;, nums[0])<\/p>\n<p>print(&quot;\u7b2c\u4e8c\u4e2a\u6570\u662f:&quot;, nums[1])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u5217\u8868\u89e3\u6790\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5206\u5272\u5e76\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u7136\u540e\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5404\u4e2a\u6570\u503c\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u8fdb\u884c\u590d\u6742\u5904\u7406\u7684\u60c5\u51b5\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7528\u6237\u8f93\u5165\u7684\u683c\u5f0f\u53ef\u80fd\u4e0d\u56fa\u5b9a\uff0c\u4f8b\u5982\u53ef\u80fd\u5305\u542b\u591a\u4e2a\u7a7a\u683c\u6216\u8005\u5176\u4ed6\u5206\u9694\u7b26\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u5206\u5272<\/strong><\/h2>\n<p>input_str = input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u6570\uff0c\u4ee5\u4efb\u610f\u7a7a\u683c\u5206\u9694: &quot;)<\/p>\n<p>nums = list(map(int, re.split(r&#39;\\s+&#39;, input_str.strip())))<\/p>\n<p>print(&quot;\u7b2c\u4e00\u4e2a\u6570\u662f:&quot;, nums[0])<\/p>\n<p>print(&quot;\u7b2c\u4e8c\u4e2a\u6570\u662f:&quot;, nums[1])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528re.split\u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u6309\u7167\u4efb\u610f\u591a\u4e2a\u7a7a\u683c\u5206\u5272\uff0c\u5e76\u8f6c\u6362\u4e3a\u6574\u6570\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u5904\u7406\u8f93\u5165\u683c\u5f0f\u4e0d\u56fa\u5b9a\u7684\u60c5\u51b5\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528input\u51fd\u6570\u548csplit\u65b9\u6cd5\u3001map\u51fd\u6570\u548csplit\u65b9\u6cd5\u3001\u5217\u8868\u89e3\u6790\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u540c\u65f6\u8f93\u5165\u4e24\u4e2a\u6570<\/strong>\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u8f93\u5165\u683c\u5f0f\u3002\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u60c5\u51b5\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u540c\u65f6\u8f93\u5165\u591a\u4e2a\u6570\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u63a5\u53d7\u7528\u6237\u8f93\u5165\uff0c\u5e76\u7ed3\u5408<code>split()<\/code>\u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u540c\u65f6\u8f93\u5165\u591a\u4e2a\u6570\u503c\u3002\u4f8b\u5982\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u201c3 5\u201d\uff0c\u7136\u540e\u901a\u8fc7\u4ee3\u7801\u5c06\u5176\u5206\u5272\u5e76\u8f6c\u6362\u4e3a\u6574\u6570\uff1a<\/p>\n<pre><code class=\"language-python\">a, b = map(int, input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u9694\u5f00: &quot;).split())\n<\/code><\/pre>\n<p><strong>\u6211\u53ef\u4ee5\u5728\u8f93\u5165\u65f6\u4f7f\u7528\u5176\u4ed6\u5206\u9694\u7b26\u5417\uff1f<\/strong><br \/>\u7edd\u5bf9\u53ef\u4ee5\uff01\u867d\u7136\u7a7a\u683c\u662f\u6700\u5e38\u7528\u7684\u5206\u9694\u7b26\uff0c\u4f46\u4f60\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u7528\u6237\u7528\u9017\u53f7\u5206\u9694\u8f93\u5165\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u5728<code>split()<\/code>\u65b9\u6cd5\u4e2d\u6307\u5b9a\u5206\u9694\u7b26\uff1a<\/p>\n<pre><code class=\"language-python\">a, b = map(int, input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u6570\u5b57\uff0c\u7528\u9017\u53f7\u9694\u5f00: &quot;).split(&#39;,&#39;))\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\uff0c\u53ef\u4ee5\u5728\u8f93\u5165\u65f6\u52a0\u5165\u5f02\u5e38\u5904\u7406\u3002\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u53ef\u4ee5\u6355\u83b7\u9519\u8bef\u5e76\u7ed9\u51fa\u53cb\u597d\u7684\u63d0\u793a\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    a, b = map(int, input(&quot;\u8bf7\u8f93\u5165\u4e24\u4e2a\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u9694\u5f00: &quot;).split())\nexcept ValueError:\n    print(&quot;\u8f93\u5165\u683c\u5f0f\u9519\u8bef\uff0c\u8bf7\u786e\u4fdd\u8f93\u5165\u4e24\u4e2a\u6574\u6570\uff0c\u5e76\u7528\u7a7a\u683c\u9694\u5f00\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u4e86\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u7a0b\u5e8f\u5c06\u4e0d\u4f1a\u5d29\u6e83\uff0c\u800c\u662f\u4f1a\u63d0\u793a\u4ed6\u4eec\u91cd\u65b0\u8f93\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u540c\u65f6\u8f93\u5165\u4e24\u4e2a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528input\u51fd\u6570\u3001split\u65b9\u6cd5\u3001\u6216\u8005\u901a\u8fc7map\u51fd\u6570\u6765\u5b9e\u73b0\u3002 \u4e0b\u9762\u5c06\u8be6\u7ec6 [&hellip;]","protected":false},"author":3,"featured_media":1041569,"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\/1041558"}],"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=1041558"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1041558\/revisions"}],"predecessor-version":[{"id":1041572,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1041558\/revisions\/1041572"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1041569"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1041558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1041558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1041558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}