{"id":1042228,"date":"2024-12-31T12:55:51","date_gmt":"2024-12-31T04:55:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1042228.html"},"modified":"2024-12-31T12:55:53","modified_gmt":"2024-12-31T04:55:53","slug":"python%e5%a6%82%e4%bd%95%e4%b8%80%e8%a1%8c%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\/1042228.html","title":{"rendered":"python\u5982\u4f55\u4e00\u884c\u8f93\u5165\u591a\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/6b5fd557-f795-48b8-8775-a587956ce5c9.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u4e00\u884c\u8f93\u5165\u591a\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528<code>split()<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>list comprehension<\/code><\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4e00\u884c\u8f93\u5165\u591a\u4e2a\u6570\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<strong>\u4f7f\u7528<code>split()<\/code>\u51fd\u6570<\/strong>\u662f\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u5b83\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u6309\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u6210\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6240\u9700\u7684\u6570\u503c\u7c7b\u578b\u3002\u5177\u4f53\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = list(map(int, input(&quot;Enter numbers separated by space: &quot;).split()))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u884c\u4ee3\u7801\u4e2d\uff0c<code>input()<\/code>\u51fd\u6570\u8bfb\u53d6\u8f93\u5165\u7684\u5b57\u7b26\u4e32\uff0c<code>split()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u6309\u7a7a\u683c\u5206\u5272\u6210\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c<code>map()<\/code>\u51fd\u6570\u5c06\u6bcf\u4e2a\u5b50\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6574\u6570\uff0c\u5e76\u6700\u7ec8\u5c06\u7ed3\u679c\u8f6c\u6362\u6210\u5217\u8868\u3002\u8fd9\u6837\u5c31\u80fd\u5728\u4e00\u884c\u5185\u8f93\u5165\u591a\u4e2a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528split()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong><code>split()<\/code>\u51fd\u6570<\/strong>\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u6309\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u6210\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>split()<\/code>\u51fd\u6570\u4f1a\u5c06\u5b57\u7b26\u4e32\u6309\u7a7a\u683c\u5206\u5272\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u5165\uff1a1 2 3 4 5<\/p>\n<p>numbers = input(&quot;Enter numbers separated by space: &quot;).split()<\/p>\n<p>print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u591a\u4e2a\u6570\uff0c\u7a0b\u5e8f\u4f1a\u5c06\u8fd9\u4e9b\u6570\u6309\u7a7a\u683c\u5206\u5272\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u8fdb\u4e00\u6b65\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u5c06\u8fd9\u4e9b\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6574\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u5165\uff1a1 2 3 4 5<\/p>\n<p>numbers = list(map(int, input(&quot;Enter numbers separated by space: &quot;).split()))<\/p>\n<p>print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>map(int, ...)<\/code>\u4f1a\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8f6c\u6362\u6210\u6574\u6570\uff0c\u6700\u7ec8\u751f\u6210\u4e00\u4e2a\u6574\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528map()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>map()<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u5185\u7f6e\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u4e8e\u4e00\u4e2a\u6216\u591a\u4e2a\u5e8f\u5217\uff08\u5982\u5217\u8868\uff09\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u8fed\u4ee3\u5668\u8f6c\u6362\u6210\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u5165\uff1a1 2 3 4 5<\/p>\n<p>numbers = list(map(int, input(&quot;Enter numbers separated by space: &quot;).split()))<\/p>\n<p>print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>map(int, ...)<\/code>\u4f1a\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8f6c\u6362\u6210\u6574\u6570\uff0c\u6700\u7ec8\u751f\u6210\u4e00\u4e2a\u6574\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u66f4\u591a\u5e94\u7528<\/h4>\n<\/p>\n<p><p><code>map()<\/code>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u6574\u6570\u8f6c\u6362\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5c06\u8f93\u5165\u7684\u591a\u4e2a\u6d6e\u70b9\u6570\u8f6c\u6362\u6210\u6d6e\u70b9\u6570\u5217\u8868\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u5165\uff1a1.1 2.2 3.3 4.4 5.5<\/p>\n<p>numbers = list(map(float, input(&quot;Enter numbers separated by space: &quot;).split()))<\/p>\n<p>print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528List Comprehension<\/h3>\n<\/p>\n<p><p><strong>List Comprehension<\/strong>\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u5f3a\u5927\u7684\u751f\u6210\u5217\u8868\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u7528\u5b83\u6765\u66ff\u4ee3<code>map()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u5165\uff1a1 2 3 4 5<\/p>\n<p>numbers = [int(x) for x in input(&quot;Enter numbers separated by space: &quot;).split()]<\/p>\n<p>print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>[int(x) for x in ...]<\/code>\u4f1a\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8f6c\u6362\u6210\u6574\u6570\uff0c\u6700\u7ec8\u751f\u6210\u4e00\u4e2a\u6574\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528numpy\u5e93<\/h3>\n<\/p>\n<p><p><strong><code>numpy<\/code>\u5e93<\/strong>\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7684\u6570\u5b66\u51fd\u6570\u548c\u64cd\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u7528\u5b83\u6765\u5904\u7406\u4e00\u884c\u8f93\u5165\u591a\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u8f93\u5165\uff1a1 2 3 4 5<\/strong><\/h2>\n<p>numbers = np.array(input(&quot;Enter numbers separated by space: &quot;).split(), dtype=int)<\/p>\n<p>print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>np.array(..., dtype=int)<\/code>\u4f1a\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8f6c\u6362\u6210\u6574\u6570\uff0c\u6700\u7ec8\u751f\u6210\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p><strong><code>pandas<\/code>\u5e93<\/strong>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7684\u6570\u636e\u7ed3\u6784\u548c\u64cd\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u7528\u5b83\u6765\u5904\u7406\u4e00\u884c\u8f93\u5165\u591a\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8f93\u5165\uff1a1 2 3 4 5<\/strong><\/h2>\n<p>numbers = pd.Series(input(&quot;Enter numbers separated by space: &quot;).split()).astype(int)<\/p>\n<p>print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>pd.Series(...).astype(int)<\/code>\u4f1a\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8f6c\u6362\u6210\u6574\u6570\uff0c\u6700\u7ec8\u751f\u6210\u4e00\u4e2a\u6574\u6570\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528sys.stdin<\/h3>\n<\/p>\n<p><p><strong><code>sys.stdin<\/code><\/strong>\u662f\u4e00\u79cd\u66f4\u5e95\u5c42\u7684\u8f93\u5165\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u8f93\u5165\uff1a1 2 3 4 5<\/strong><\/h2>\n<p>numbers = list(map(int, sys.stdin.read().split()))<\/p>\n<p>print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>sys.stdin.read().split()<\/code>\u4f1a\u8bfb\u53d6\u6807\u51c6\u8f93\u5165\u7684\u6240\u6709\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u6309\u7a7a\u683c\u5206\u5272\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u3002\u7136\u540e\uff0c<code>map(int, ...)<\/code>\u4f1a\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8f6c\u6362\u6210\u6574\u6570\uff0c\u6700\u7ec8\u751f\u6210\u4e00\u4e2a\u6574\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p><strong>\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/strong>\u662f\u4e00\u79cd\u60f0\u6027\u6c42\u503c\u7684\u751f\u6210\u5668\uff0c\u5b83\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u52a8\u6001\u751f\u6210\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u7528\u5b83\u6765\u5904\u7406\u4e00\u884c\u8f93\u5165\u591a\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u5165\uff1a1 2 3 4 5<\/p>\n<p>numbers = (int(x) for x in input(&quot;Enter numbers separated by space: &quot;).split())<\/p>\n<p>print(list(numbers))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>(int(x) for x in ...)<\/code>\u4f1a\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8f6c\u6362\u6210\u6574\u6570\uff0c\u751f\u6210\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>list()<\/code>\u51fd\u6570\u5c06\u751f\u6210\u5668\u5bf9\u8c61\u8f6c\u6362\u6210\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u63d0\u53d6\u548c\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u8f93\u5165\uff1a1 2 3 4 5<\/strong><\/h2>\n<p>numbers = list(map(int, re.findall(r&#39;\\d+&#39;, input(&quot;Enter numbers separated by space: &quot;))))<\/p>\n<p>print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>re.findall(r&#39;\\d+&#39;, ...)<\/code>\u4f1a\u5339\u914d\u8f93\u5165\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\uff0c\u5e76\u5c06\u5176\u63d0\u53d6\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u3002\u7136\u540e\uff0c<code>map(int, ...)<\/code>\u4f1a\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8f6c\u6362\u6210\u6574\u6570\uff0c\u6700\u7ec8\u751f\u6210\u4e00\u4e2a\u6574\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4e00\u884c\u8f93\u5165\u591a\u4e2a\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528<code>split()<\/code>\u51fd\u6570\u3001<code>map()<\/code>\u51fd\u6570\u3001List Comprehension\u3001<code>numpy<\/code>\u5e93\u3001<code>pandas<\/code>\u5e93\u3001<code>sys.stdin<\/code>\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u4e00\u884c\u8f93\u5165\u591a\u4e2a\u6570\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u6210\u6240\u9700\u7684\u6570\u503c\u7c7b\u578b\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u66f4\u52a0\u7b80\u6d01\u3001\u9ad8\u6548\u7684Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4e00\u884c\u8f93\u5165\u591a\u4e2a\u6570\u5b57\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u63a5\u6536\u7528\u6237\u8f93\u5165\uff0c\u7136\u540e\u5229\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u5b57\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = list(map(int, input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u5206\u9694\uff1a&quot;).split()))\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u4e00\u884c\u4e2d\u8f93\u5165\u591a\u4e2a\u6570\u5b57\uff0c\u6700\u7ec8\u8fd9\u4e9b\u6570\u5b57\u4f1a\u88ab\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<p><strong>\u4f7f\u7528\u4e00\u884c\u8f93\u5165\u591a\u4e2a\u6570\u5b57\u65f6\uff0c\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u8f93\u5165\u9519\u8bef\uff1f<\/strong><br \/>\u4e3a\u4e86\u5904\u7406\u8f93\u5165\u9519\u8bef\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u68c0\u67e5\u7528\u6237\u662f\u5426\u8f93\u5165\u4e86\u6709\u6548\u7684\u6570\u5b57\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    numbers = list(map(int, input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u5206\u9694\uff1a&quot;).split()))\nexcept ValueError:\n    print(&quot;\u8bf7\u8f93\u5165\u6709\u6548\u7684\u6570\u5b57\uff01&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\u63d0\u793a\u9519\u8bef\u5e76\u8981\u6c42\u91cd\u65b0\u8f93\u5165\u3002<\/p>\n<p><strong>\u5728\u4e00\u884c\u8f93\u5165\u591a\u4e2a\u6570\u5b57\u540e\uff0c\u5982\u4f55\u5bf9\u8fd9\u4e9b\u6570\u5b57\u8fdb\u884c\u6c42\u548c\u6216\u5176\u4ed6\u64cd\u4f5c\uff1f<\/strong><br \/>\u4e00\u65e6\u60a8\u5c06\u8f93\u5165\u7684\u6570\u5b57\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u603b\u548c\uff0c\u6216\u8005\u4f7f\u7528\u5faa\u73af\u8fdb\u884c\u5176\u4ed6\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">numbers = list(map(int, input(&quot;\u8bf7\u8f93\u5165\u591a\u4e2a\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u5206\u9694\uff1a&quot;).split()))\ntotal = sum(numbers)\nprint(&quot;\u6570\u5b57\u7684\u603b\u548c\u4e3a:&quot;, total)\n<\/code><\/pre>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u5229\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u51fd\u6570\u5bf9\u6bcf\u4e2a\u6570\u5b57\u8fdb\u884c\u5e73\u65b9\u3001\u8fc7\u6ee4\u7b49\u64cd\u4f5c\uff0c\u7075\u6d3b\u6027\u5f88\u9ad8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528split()\u51fd\u6570\u3001\u4f7f\u7528map()\u51fd\u6570\u3001\u4f7f\u7528list comprehension \u5728Python\u4e2d\uff0c\u4e00\u884c\u8f93 [&hellip;]","protected":false},"author":3,"featured_media":1042239,"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\/1042228"}],"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=1042228"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042228\/revisions"}],"predecessor-version":[{"id":1042244,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042228\/revisions\/1042244"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1042239"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1042228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1042228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1042228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}