{"id":1037988,"date":"2024-12-31T12:19:48","date_gmt":"2024-12-31T04:19:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1037988.html"},"modified":"2024-12-31T12:19:50","modified_gmt":"2024-12-31T04:19:50","slug":"python%e7%94%a8%e6%88%b7%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e4%b8%80%e4%b8%aa%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1037988.html","title":{"rendered":"python\u7528\u6237\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/bfb23d8e-8d67-4263-b1fe-9a8408f575ac.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u7528\u6237\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u5217\u8868\" \/><\/p>\n<p><p> <strong>Python\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8f93\u5165\u4e00\u4e2a\u5217\u8868\uff0c\u4f8b\u5982\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u3001\u6216\u8005\u901a\u8fc7\u7a0b\u5e8f\u5185\u5b9a\u4e49\u7b49\u3002<\/strong> \u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u8fdb\u884c\u8bf4\u660e\u3002\u4ee5\u4e0b\u662f\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\uff1a<\/strong> \u8fd9\u662f\u6700\u5e38\u89c1\u4e14\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u7528\u6237\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u8f93\u5165\u6570\u636e\uff0c\u901a\u8fc7 <code>input()<\/code> \u51fd\u6570\u8bfb\u53d6\u8f93\u5165\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4f8b\u5982\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u4e00\u4e32\u6570\u5b57\uff0c\u7a0b\u5e8f\u5c06\u5176\u5206\u5272\u5e76\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u6570\u5b57\uff0c\u5e76\u7528\u7a7a\u683c\u5206\u5f00: &quot;)<\/p>\n<p>num_list = [int(x) for x in user_input.split()]<\/p>\n<p>print(&quot;\u7528\u6237\u8f93\u5165\u7684\u5217\u8868\u662f:&quot;, num_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>input()<\/code> \u51fd\u6570\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\uff0c<code>split()<\/code> \u65b9\u6cd5\u5c06\u5176\u6309\u7a7a\u683c\u5206\u5272\u6210\u591a\u4e2a\u5b57\u7b26\u4e32\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u8fd9\u4e9b\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u5b58\u50a8\u5728 <code>num_list<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7 <code>input()<\/code> \u51fd\u6570\u4ece\u63a7\u5236\u53f0\u8f93\u5165\u6570\u636e\uff0c\u5e76\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u793a\u4f8b\u8bf4\u660e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32<\/p>\n<p>user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u6570\u5b57\uff0c\u5e76\u7528\u7a7a\u683c\u5206\u5f00: &quot;)<\/p>\n<h2><strong>\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u5e76\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868<\/strong><\/h2>\n<p>num_list = [int(x) for x in user_input.split()]<\/p>\n<h2><strong>\u8f93\u51fa\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u7528\u6237\u8f93\u5165\u7684\u5217\u8868\u662f:&quot;, num_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u901a\u8fc7 <code>input()<\/code> \u51fd\u6570\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u3002\u968f\u540e\uff0c<code>split()<\/code> \u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u6309\u7a7a\u683c\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u5f62\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u3002\u6700\u540e\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u8fd9\u4e9b\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u5b58\u50a8\u5728 <code>num_list<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u8fd8\u53ef\u4ee5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u91cd\u590d\u4f7f\u7528\u6570\u636e\u7684\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9<\/p>\n<p>with open(&#39;data.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<h2><strong>\u5c06\u8bfb\u53d6\u7684\u5185\u5bb9\u5206\u5272\u6210\u5217\u8868<\/strong><\/h2>\n<p>num_list = [int(x) for x in data.split()]<\/p>\n<h2><strong>\u8f93\u51fa\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u7684\u5217\u8868\u662f:&quot;, num_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u4f7f\u7528 <code>open()<\/code> \u51fd\u6570\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u968f\u540e\uff0c\u901a\u8fc7 <code>split()<\/code> \u65b9\u6cd5\u5c06\u5185\u5bb9\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u5f62\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u3002\u6700\u540e\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u8fd9\u4e9b\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u5b58\u50a8\u5728 <code>num_list<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u7a0b\u5e8f\u5185\u5b9a\u4e49<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u8fd8\u53ef\u4ee5\u76f4\u63a5\u5728\u7a0b\u5e8f\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5df2\u77e5\u6570\u636e\u4e14\u4e0d\u9700\u8981\u7528\u6237\u8f93\u5165\u7684\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/p>\n<p>num_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u8f93\u51fa\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u7a0b\u5e8f\u5185\u5b9a\u4e49\u7684\u5217\u8868\u662f:&quot;, num_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u5217\u8868 <code>num_list<\/code> \u76f4\u63a5\u5728\u7a0b\u5e8f\u5185\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u3002\u968f\u540e\uff0c\u901a\u8fc7 <code>print()<\/code> \u51fd\u6570\u8f93\u51fa\u5217\u8868\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5faa\u73af\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u9010\u4e2a\u8f93\u5165\u6570\u636e\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u8f93\u5165\u6570\u636e\u4e14\u8f93\u5165\u6570\u91cf\u4e0d\u786e\u5b9a\u7684\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5217\u8868<\/p>\n<p>num_list = []<\/p>\n<h2><strong>\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u5217\u8868\u957f\u5ea6<\/strong><\/h2>\n<p>n = int(input(&quot;\u8bf7\u8f93\u5165\u5217\u8868\u7684\u957f\u5ea6: &quot;))<\/p>\n<h2><strong>\u5faa\u73af\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u6570\u636e<\/strong><\/h2>\n<p>for i in range(n):<\/p>\n<p>    num = int(input(f&quot;\u8bf7\u8f93\u5165\u7b2c {i+1} \u4e2a\u6570\u5b57: &quot;))<\/p>\n<p>    num_list.append(num)<\/p>\n<h2><strong>\u8f93\u51fa\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u7528\u6237\u8f93\u5165\u7684\u5217\u8868\u662f:&quot;, num_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u901a\u8fc7 <code>input()<\/code> \u51fd\u6570\u8bfb\u53d6\u5217\u8868\u7684\u957f\u5ea6\u3002\u968f\u540e\uff0c\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u9010\u4e2a\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868 <code>num_list<\/code> \u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7 <code>print()<\/code> \u51fd\u6570\u8f93\u51fa\u5217\u8868\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528 <code>eval()<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u8fd8\u53ef\u4ee5\u901a\u8fc7 <code>eval()<\/code> \u51fd\u6570\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u76f4\u63a5\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7528\u6237\u8f93\u5165\u7b26\u5408 Python \u5217\u8868\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32<\/p>\n<p>user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5217\u8868\uff08\u4f8b\u5982\uff1a[1, 2, 3, 4, 5]\uff09: &quot;)<\/p>\n<h2><strong>\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>num_list = eval(user_input)<\/p>\n<h2><strong>\u8f93\u51fa\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u7528\u6237\u8f93\u5165\u7684\u5217\u8868\u662f:&quot;, num_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u901a\u8fc7 <code>input()<\/code> \u51fd\u6570\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u3002\u968f\u540e\uff0c\u901a\u8fc7 <code>eval()<\/code> \u51fd\u6570\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u76f4\u63a5\u8f6c\u6362\u4e3a\u5217\u8868 <code>num_list<\/code>\u3002\u6700\u540e\uff0c\u901a\u8fc7 <code>print()<\/code> \u51fd\u6570\u8f93\u51fa\u5217\u8868\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e00\u4e2a\u5217\u8868\uff0c\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6839\u636e\u4e00\u5b9a\u89c4\u5f8b\u751f\u6210\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e00\u4e2a\u5305\u542b\u524d10\u4e2a\u6b63\u6574\u6570\u7684\u5217\u8868<\/p>\n<p>num_list = [x for x in range(1, 11)]<\/p>\n<h2><strong>\u8f93\u51fa\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u7684\u5217\u8868\u662f:&quot;, num_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u6839\u636e\u4e00\u5b9a\u89c4\u5f8b\u751f\u6210\u4e00\u4e2a\u5305\u542b\u524d10\u4e2a\u6b63\u6574\u6570\u7684\u5217\u8868 <code>num_list<\/code>\u3002\u968f\u540e\uff0c\u901a\u8fc7 <code>print()<\/code> \u51fd\u6570\u8f93\u51fa\u5217\u8868\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4ece\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u8fd8\u53ef\u4ee5\u5c06\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff08\u5982\u5143\u7ec4\u3001\u96c6\u5408\u7b49\uff09\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5143\u7ec4<\/p>\n<p>num_tuple = (1, 2, 3, 4, 5)<\/p>\n<h2><strong>\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>num_list = list(num_tuple)<\/p>\n<h2><strong>\u8f93\u51fa\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u4ece\u5143\u7ec4\u8f6c\u6362\u800c\u6765\u7684\u5217\u8868\u662f:&quot;, num_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u5143\u7ec4 <code>num_tuple<\/code>\u3002\u968f\u540e\uff0c\u901a\u8fc7 <code>list()<\/code> \u51fd\u6570\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868 <code>num_list<\/code>\u3002\u6700\u540e\uff0c\u901a\u8fc7 <code>print()<\/code> \u51fd\u6570\u8f93\u51fa\u5217\u8868\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528 <code>map()<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u8fd8\u53ef\u4ee5\u901a\u8fc7 <code>map()<\/code> \u51fd\u6570\u5c06\u8f93\u5165\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32<\/p>\n<p>user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u6570\u5b57\uff0c\u5e76\u7528\u7a7a\u683c\u5206\u5f00: &quot;)<\/p>\n<h2><strong>\u4f7f\u7528 map() \u51fd\u6570\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868<\/strong><\/h2>\n<p>num_list = list(map(int, user_input.split()))<\/p>\n<h2><strong>\u8f93\u51fa\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u7528\u6237\u8f93\u5165\u7684\u5217\u8868\u662f:&quot;, num_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u901a\u8fc7 <code>input()<\/code> \u51fd\u6570\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u3002\u968f\u540e\uff0c\u901a\u8fc7 <code>split()<\/code> \u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u5f62\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u3002\u63a5\u7740\uff0c\u901a\u8fc7 <code>map()<\/code> \u51fd\u6570\u5c06\u8fd9\u4e9b\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u901a\u8fc7 <code>list()<\/code> \u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868 <code>num_list<\/code>\u3002\u6700\u540e\uff0c\u901a\u8fc7 <code>print()<\/code> \u51fd\u6570\u8f93\u51fa\u5217\u8868\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528 <code>numpy<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>numpy<\/code> \u6a21\u5757\u751f\u6210\u6216\u8f6c\u6362\u6570\u636e\u4e3a\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528 numpy \u751f\u6210\u4e00\u4e2a\u5305\u542b\u524d10\u4e2a\u6b63\u6574\u6570\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>num_array = np.arange(1, 11)<\/p>\n<h2><strong>\u5c06 numpy \u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>num_list = num_array.tolist()<\/p>\n<h2><strong>\u8f93\u51fa\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u4f7f\u7528 numpy \u751f\u6210\u7684\u5217\u8868\u662f:&quot;, num_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165 <code>numpy<\/code> \u6a21\u5757\u3002\u968f\u540e\uff0c\u4f7f\u7528 <code>numpy<\/code> \u7684 <code>arange()<\/code> \u51fd\u6570\u751f\u6210\u4e00\u4e2a\u5305\u542b\u524d10\u4e2a\u6b63\u6574\u6570\u7684\u6570\u7ec4 <code>num_array<\/code>\u3002\u63a5\u7740\uff0c\u901a\u8fc7 <code>tolist()<\/code> \u65b9\u6cd5\u5c06 <code>numpy<\/code> \u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868 <code>num_list<\/code>\u3002\u6700\u540e\uff0c\u901a\u8fc7 <code>print()<\/code> \u51fd\u6570\u8f93\u51fa\u5217\u8868\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528 <code>pandas<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>pandas<\/code> \u6a21\u5757\u751f\u6210\u6216\u8f6c\u6362\u6570\u636e\u4e3a\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4f7f\u7528 pandas \u751f\u6210\u4e00\u4e2a\u5305\u542b\u524d10\u4e2a\u6b63\u6574\u6570\u7684 Series<\/strong><\/h2>\n<p>num_series = pd.Series(range(1, 11))<\/p>\n<h2><strong>\u5c06 pandas Series \u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>num_list = num_series.tolist()<\/p>\n<h2><strong>\u8f93\u51fa\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;\u4f7f\u7528 pandas \u751f\u6210\u7684\u5217\u8868\u662f:&quot;, num_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165 <code>pandas<\/code> \u6a21\u5757\u3002\u968f\u540e\uff0c\u4f7f\u7528 <code>pandas<\/code> \u7684 <code>Series<\/code> \u51fd\u6570\u751f\u6210\u4e00\u4e2a\u5305\u542b\u524d10\u4e2a\u6b63\u6574\u6570\u7684 <code>Series<\/code> \u5bf9\u8c61 <code>num_series<\/code>\u3002\u63a5\u7740\uff0c\u901a\u8fc7 <code>tolist()<\/code> \u65b9\u6cd5\u5c06 <code>pandas Series<\/code> \u8f6c\u6362\u4e3a\u5217\u8868 <code>num_list<\/code>\u3002\u6700\u540e\uff0c\u901a\u8fc7 <code>print()<\/code> \u51fd\u6570\u8f93\u51fa\u5217\u8868\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u662fPython\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5217\u8868\u7684\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\u3002\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u548c\u573a\u666f\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u6765\u8f93\u5165\u6216\u751f\u6210\u5217\u8868\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8f93\u5165\u4e00\u4e2a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u6765\u8f93\u5165\u4e00\u4e2a\u5217\u8868\u3002\u901a\u5e38\uff0c\u7528\u6237\u4f1a\u8f93\u5165\u4ee5\u9017\u53f7\u6216\u7a7a\u683c\u5206\u9694\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5217\u8868\uff0c\u4ee5\u9017\u53f7\u5206\u9694: &quot;)\nmy_list = user_input.split(&#39;,&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5c31\u4f1a\u88ab\u5206\u5272\u6210\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\uff0c\u7528\u6237\u8fd8\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c <code>map()<\/code> \u51fd\u6570\u6765\u8f6c\u6362\u8f93\u5165\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5e0c\u671b\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u4ee5\u7a7a\u683c\u5206\u9694: &quot;)\nmy_list = list(map(int, user_input.split()))\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u521b\u5efa\u5217\u8868\uff0c\u8fd8\u53ef\u4ee5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u9519\u8bef\u4ee5\u907f\u514d\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u4f7f\u7528 <code>try-except<\/code> \u8bed\u53e5\u6765\u6355\u83b7\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\uff0c\u5e76\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u5217\u8868\uff0c\u4ee5\u9017\u53f7\u5206\u9694: &quot;)\n    my_list = [int(x) for x in user_input.split(&#39;,&#39;)]\nexcept ValueError:\n    print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u786e\u4fdd\u8f93\u5165\u7684\u662f\u6570\u5b57\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u7a0b\u5e8f\u56e0\u9519\u8bef\u8f93\u5165\u800c\u5d29\u6e83\uff0c\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8f93\u5165\u4e00\u4e2a\u5217\u8868\uff0c\u4f8b\u5982\u4f7f\u7528 input() \u51fd\u6570\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u3001\u6216\u8005\u901a\u8fc7\u7a0b\u5e8f\u5185\u5b9a\u4e49\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1037993,"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\/1037988"}],"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=1037988"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1037988\/revisions"}],"predecessor-version":[{"id":1037994,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1037988\/revisions\/1037994"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1037993"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1037988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1037988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1037988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}