{"id":1160964,"date":"2025-01-13T19:11:11","date_gmt":"2025-01-13T11:11:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1160964.html"},"modified":"2025-01-13T19:11:14","modified_gmt":"2025-01-13T11:11:14","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8%e6%95%b0%e7%bb%84%e8%8e%b7%e5%8f%96%e8%be%93%e5%85%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1160964.html","title":{"rendered":"python\u5982\u4f55\u7528\u6570\u7ec4\u83b7\u53d6\u8f93\u5165"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202337\/8c2b860a-0dbd-4bb8-9cec-0153ab4fdfca.webp\" alt=\"python\u5982\u4f55\u7528\u6570\u7ec4\u83b7\u53d6\u8f93\u5165\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u901a\u8fc7\u6570\u7ec4\uff08\u5373\u5217\u8868\uff09\u83b7\u53d6\u8f93\u5165\uff0c\u6bd4\u5982\u4f7f\u7528input\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001map\u51fd\u6570\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u901a\u8fc7input\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff1ainput()\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001map()\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001input()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>split()<\/code>\u65b9\u6cd5\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7528\u6237\u8f93\u5165<\/p>\n<p>user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u5206\u9694: &quot;)<\/p>\n<h2><strong>\u5c06\u8f93\u5165\u5206\u5272\u6210\u6570\u7ec4<\/strong><\/h2>\n<p>array = user_input.split()<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u7ec4\u4e3a: &quot;, array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5217\u8868\u3002\u4f60\u53ef\u4ee5\u7ed3\u5408<code>input()<\/code>\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u83b7\u53d6\u5e76\u8f6c\u6362\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u8f6c\u6362\u4e3a\u6574\u6570\u6570\u7ec4<\/p>\n<p>array = [int(x) for x in input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u5206\u9694: &quot;).split()]<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6574\u6570\u6570\u7ec4\u4e3a: &quot;, array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001map()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>map()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u8f93\u5165\u7684\u6bcf\u4e2a\u90e8\u5206\u6620\u5c04\u5230\u4e00\u4e2a\u51fd\u6570\u4e0a\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u4f7f\u7528map()\u51fd\u6570\u8f6c\u6362\u4e3a\u6574\u6570\u6570\u7ec4<\/p>\n<p>array = list(map(int, input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u5206\u9694: &quot;).split()))<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6574\u6570\u6570\u7ec4\u4e3a: &quot;, array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u8f6c\u6362\u4e3anumpy\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([int(x) for x in input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u5206\u9694: &quot;).split()])<\/p>\n<p>print(&quot;\u8f93\u5165\u7684numpy\u6570\u7ec4\u4e3a: &quot;, array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u591a\u884c\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u4ece\u591a\u884c\u8f93\u5165\u4e2d\u83b7\u53d6\u6570\u7ec4\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u5904\u7406\u6bcf\u4e00\u884c\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = []<\/p>\n<p>print(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u6570\u5b57\uff0c\u6bcf\u884c\u4e00\u4e2a\u6570\u5b57\uff0c\u8f93\u5165\u7a7a\u884c\u7ed3\u675f:&quot;)<\/p>\n<p>while True:<\/p>\n<p>    line = input()<\/p>\n<p>    if line == &quot;&quot;:<\/p>\n<p>        break<\/p>\n<p>    array.append(int(line))<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u7ec4\u4e3a: &quot;, array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8f93\u5165\u6570\u636e\u53ef\u80fd\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u8f93\u5165\u6570\u636e\u5b58\u50a8\u5728input.txt\u6587\u4ef6\u4e2d<\/p>\n<p>with open(&quot;input.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    array = [int(line.strip()) for line in file]<\/p>\n<p>print(&quot;\u4ece\u6587\u4ef6\u8bfb\u53d6\u7684\u6570\u7ec4\u4e3a: &quot;, array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528json\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u8f93\u5165\u6570\u636e\u4ee5JSON\u683c\u5f0f\u63d0\u4f9b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u5e93\u6765\u89e3\u6790\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5047\u8bbe\u8f93\u5165\u6570\u636e\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_input = &#39;[1, 2, 3, 4, 5]&#39;<\/p>\n<h2><strong>\u89e3\u6790JSON\u5b57\u7b26\u4e32\u4e3a\u6570\u7ec4<\/strong><\/h2>\n<p>array = json.loads(json_input)<\/p>\n<p>print(&quot;\u89e3\u6790\u7684\u6570\u7ec4\u4e3a: &quot;, array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528sys.stdin<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8f93\u5165\u53ef\u80fd\u662f\u901a\u8fc7\u6807\u51c6\u8f93\u5165\uff08<code>sys.stdin<\/code>\uff09\u63d0\u4f9b\u7684\uff0c\u4f8b\u5982\u5728\u4e00\u4e9b\u7ade\u8d5b\u7f16\u7a0b\u73af\u5883\u4e2d\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>sys.stdin<\/code>\u6765\u83b7\u53d6\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u8f93\u5165<\/strong><\/h2>\n<p>input_data = sys.stdin.read()<\/p>\n<p>array = [int(x) for x in input_data.split()]<\/p>\n<p>print(&quot;\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u7684\u6570\u7ec4\u4e3a: &quot;, array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528argparse\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>argparse<\/code>\u5e93\u6765\u83b7\u53d6\u8f93\u5165\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<h2><strong>\u521b\u5efa\u89e3\u6790\u5668<\/strong><\/h2>\n<p>parser = argparse.ArgumentParser(description=&quot;\u83b7\u53d6\u6570\u7ec4\u8f93\u5165&quot;)<\/p>\n<h2><strong>\u6dfb\u52a0\u53c2\u6570<\/strong><\/h2>\n<p>parser.add_argument(&#39;numbers&#39;, metavar=&#39;N&#39;, type=int, nargs=&#39;+&#39;, help=&#39;\u8f93\u5165\u7684\u6570\u5b57&#39;)<\/p>\n<h2><strong>\u89e3\u6790\u53c2\u6570<\/strong><\/h2>\n<p>args = parser.parse_args()<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u7ec4\u4e3a: &quot;, args.numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u5217\u8868\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u751f\u6210\u5668\u6765\u52a8\u6001\u751f\u6210\u5217\u8868\uff0c\u8fd9\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a8\u6001\u751f\u6210\u6570\u7ec4<\/p>\n<p>array = [i for i in range(10)]<\/p>\n<p>print(&quot;\u751f\u6210\u7684\u6570\u7ec4\u4e3a: &quot;, array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u8f93\u5165\u6570\u636e\u53ef\u80fd\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u63d0\u53d6\u6570\u5b57\u5e76\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u6570\u636e\uff0c\u5305\u542b\u6570\u5b57\u548c\u5176\u4ed6\u5b57\u7b26: &quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u5b57<\/strong><\/h2>\n<p>array = list(map(int, re.findall(r&#39;\\d+&#39;, user_input)))<\/p>\n<p>print(&quot;\u63d0\u53d6\u7684\u6570\u7ec4\u4e3a: &quot;, array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u552f\u4e00\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08<code>set<\/code>\uff09\u6765\u81ea\u52a8\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u8f6c\u6362\u4e3a\u96c6\u5408<\/p>\n<p>array = list(set(int(x) for x in input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u5206\u9694: &quot;).split()))<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u552f\u4e00\u5143\u7d20\u6570\u7ec4\u4e3a: &quot;, array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u8bfb\u53d6<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u83b7\u53d6\u8f93\u5165\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>sys.argv<\/code>\u6765\u8bfb\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u8bfb\u53d6\u8f93\u5165<\/strong><\/h2>\n<p>array = [int(x) for x in sys.argv[1:]]<\/p>\n<p>print(&quot;\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u8bfb\u53d6\u7684\u6570\u7ec4\u4e3a: &quot;, array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8bfb\u53d6\u4e8c\u7ef4\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u8bfb\u53d6\u4e8c\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u6765\u83b7\u53d6\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows = int(input(&quot;\u8bf7\u8f93\u5165\u884c\u6570: &quot;))<\/p>\n<p>cols = int(input(&quot;\u8bf7\u8f93\u5165\u5217\u6570: &quot;))<\/p>\n<p>array = []<\/p>\n<p>for i in range(rows):<\/p>\n<p>    row = [int(x) for x in input(f&quot;\u8bf7\u8f93\u5165\u7b2c{i+1}\u884c\u7684{cols}\u4e2a\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u5206\u9694: &quot;).split()]<\/p>\n<p>    array.append(row)<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u4e8c\u7ef4\u6570\u7ec4\u4e3a: &quot;)<\/p>\n<p>for row in array:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u7ed3\u5408defaultdict\u5904\u7406\u591a\u91cd\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u91cd\u8f93\u5165\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>collections.defaultdict<\/code>\u6765\u7b80\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u521b\u5efa\u9ed8\u8ba4\u5b57\u5178<\/strong><\/h2>\n<p>array = defaultdict(list)<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>print(&quot;\u8bf7\u8f93\u5165\u952e\u503c\u5bf9\uff0c\u6bcf\u884c\u4e00\u4e2a\u952e\u503c\u5bf9\uff08\u952e:\u503c\uff09\uff0c\u8f93\u5165\u7a7a\u884c\u7ed3\u675f:&quot;)<\/p>\n<p>while True:<\/p>\n<p>    line = input()<\/p>\n<p>    if line == &quot;&quot;:<\/p>\n<p>        break<\/p>\n<p>    key, value = line.split(&quot;:&quot;)<\/p>\n<p>    array[key.strip()].append(int(value.strip()))<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u7ec4\u4e3a: &quot;, dict(array))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u4f7f\u7528\u961f\u5217\u5904\u7406\u5b9e\u65f6\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5b9e\u65f6\u5e94\u7528\u4e2d\uff0c\u4f7f\u7528\u961f\u5217\uff08<code>queue.Queue<\/code>\uff09\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5904\u7406\u8fde\u7eed\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from queue import Queue<\/p>\n<h2><strong>\u521b\u5efa\u961f\u5217<\/strong><\/h2>\n<p>queue = Queue()<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>print(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u6570\u5b57\uff0c\u6bcf\u884c\u4e00\u4e2a\u6570\u5b57\uff0c\u8f93\u5165\u7a7a\u884c\u7ed3\u675f:&quot;)<\/p>\n<p>while True:<\/p>\n<p>    line = input()<\/p>\n<p>    if line == &quot;&quot;:<\/p>\n<p>        break<\/p>\n<p>    queue.put(int(line))<\/p>\n<h2><strong>\u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>array = []<\/p>\n<p>while not queue.empty():<\/p>\n<p>    array.append(queue.get())<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u7ec4\u4e3a: &quot;, array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u4f7f\u7528\u94fe\u8868\u5904\u7406\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u94fe\u8868\uff08<code>linked list<\/code>\uff09\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9891\u7e41\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Node:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>        self.next = None<\/p>\n<p>class LinkedList:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.head = None<\/p>\n<p>    def append(self, value):<\/p>\n<p>        new_node = Node(value)<\/p>\n<p>        if not self.head:<\/p>\n<p>            self.head = new_node<\/p>\n<p>            return<\/p>\n<p>        last_node = self.head<\/p>\n<p>        while last_node.next:<\/p>\n<p>            last_node = last_node.next<\/p>\n<p>        last_node.next = new_node<\/p>\n<p>    def to_list(self):<\/p>\n<p>        array = []<\/p>\n<p>        current_node = self.head<\/p>\n<p>        while current_node:<\/p>\n<p>            array.append(current_node.value)<\/p>\n<p>            current_node = current_node.next<\/p>\n<p>        return array<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>linked_list = LinkedList()<\/p>\n<p>print(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u6570\u5b57\uff0c\u6bcf\u884c\u4e00\u4e2a\u6570\u5b57\uff0c\u8f93\u5165\u7a7a\u884c\u7ed3\u675f:&quot;)<\/p>\n<p>while True:<\/p>\n<p>    line = input()<\/p>\n<p>    if line == &quot;&quot;:<\/p>\n<p>        break<\/p>\n<p>    linked_list.append(int(line))<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u7ec4\u4e3a: &quot;, linked_list.to_list())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u4f7f\u7528\u5806\u6808\u5904\u7406\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5806\u6808\uff08<code>stack<\/code>\uff09\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u540e\u8fdb\u5148\u51fa\uff08LIFO\uff09\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Stack:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.stack = []<\/p>\n<p>    def push(self, value):<\/p>\n<p>        self.stack.append(value)<\/p>\n<p>    def pop(self):<\/p>\n<p>        if not self.is_empty():<\/p>\n<p>            return self.stack.pop()<\/p>\n<p>    def is_empty(self):<\/p>\n<p>        return len(self.stack) == 0<\/p>\n<p>    def to_list(self):<\/p>\n<p>        return self.stack[::-1]<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>stack = Stack()<\/p>\n<p>print(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u6570\u5b57\uff0c\u6bcf\u884c\u4e00\u4e2a\u6570\u5b57\uff0c\u8f93\u5165\u7a7a\u884c\u7ed3\u675f:&quot;)<\/p>\n<p>while True:<\/p>\n<p>    line = input()<\/p>\n<p>    if line == &quot;&quot;:<\/p>\n<p>        break<\/p>\n<p>    stack.push(int(line))<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u7ec4\u4e3a: &quot;, stack.to_list())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e5d\u3001\u4f7f\u7528\u53cc\u7aef\u961f\u5217\u5904\u7406\u53cc\u5411\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u53cc\u7aef\u961f\u5217\uff08<code>deque<\/code>\uff09\u662f\u4e00\u79cd\u53cc\u5411\u961f\u5217\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5728\u4e24\u7aef\u8fdb\u884c\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<h2><strong>\u521b\u5efa\u53cc\u7aef\u961f\u5217<\/strong><\/h2>\n<p>deque_array = deque()<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>print(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u6570\u5b57\uff0c\u6bcf\u884c\u4e00\u4e2a\u6570\u5b57\uff0c\u8f93\u5165\u7a7a\u884c\u7ed3\u675f:&quot;)<\/p>\n<p>while True:<\/p>\n<p>    line = input()<\/p>\n<p>    if line == &quot;&quot;:<\/p>\n<p>        break<\/p>\n<p>    deque_array.append(int(line))<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u7ec4\u4e3a: &quot;, list(deque_array))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u3001\u4f7f\u7528\u6709\u5e8f\u5b57\u5178\u5904\u7406\u6709\u5e8f\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u6709\u5e8f\u5b57\u5178\uff08<code>OrderedDict<\/code>\uff09\u53ef\u4ee5\u4fdd\u6301\u5143\u7d20\u7684\u63d2\u5165\u987a\u5e8f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u6301\u8f93\u5165\u987a\u5e8f\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import OrderedDict<\/p>\n<h2><strong>\u521b\u5efa\u6709\u5e8f\u5b57\u5178<\/strong><\/h2>\n<p>ordered_dict = OrderedDict()<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>print(&quot;\u8bf7\u8f93\u5165\u952e\u503c\u5bf9\uff0c\u6bcf\u884c\u4e00\u4e2a\u952e\u503c\u5bf9\uff08\u952e:\u503c\uff09\uff0c\u8f93\u5165\u7a7a\u884c\u7ed3\u675f:&quot;)<\/p>\n<p>while True:<\/p>\n<p>    line = input()<\/p>\n<p>    if line == &quot;&quot;:<\/p>\n<p>        break<\/p>\n<p>    key, value = line.split(&quot;:&quot;)<\/p>\n<p>    ordered_dict[key.strip()] = int(value.strip())<\/p>\n<p>print(&quot;\u8f93\u5165\u7684\u6570\u7ec4\u4e3a: &quot;, dict(ordered_dict))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u548c\u573a\u666f\uff0c\u4ece\u7528\u6237\u3001\u6587\u4ef6\u6216\u5176\u4ed6\u8f93\u5165\u6e90\u4e2d\u83b7\u53d6\u6570\u7ec4\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002\u5e0c\u671b\u8fd9\u4e9b\u793a\u4f8b\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u6765\u5904\u7406\u6570\u7ec4\u8f93\u5165\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u6570\u7ec4\u63a5\u6536\u7528\u6237\u8f93\u5165\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>input()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u5e76\u5c06\u8f93\u5165\u5b58\u50a8\u5728\u6570\u7ec4\uff08\u6216\u5217\u8868\uff09\u4e2d\u3002\u9996\u5148\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u7136\u540e\u901a\u8fc7\u5faa\u73af\u53cd\u590d\u8c03\u7528<code>input()<\/code>\u6765\u6536\u96c6\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8981\u6c42\u7528\u6237\u8f93\u5165\u6570\u5b57\uff0c\u76f4\u5230\u4ed6\u4eec\u8f93\u5165\u4e00\u4e2a\u7279\u5b9a\u7684\u7ed3\u675f\u6807\u5fd7\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">inputs = []\nwhile True:\n    value = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff08\u8f93\u5165&#39;\u7ed3\u675f&#39;\u4ee5\u7ed3\u675f\uff09\uff1a&quot;)\n    if value.lower() == &#39;\u7ed3\u675f&#39;:\n        break\n    inputs.append(float(value))  # \u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u5e76\u6dfb\u52a0\u5230\u5217\u8868\nprint(&quot;\u60a8\u8f93\u5165\u7684\u6570\u5b57\u662f\uff1a&quot;, inputs)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5c06\u7528\u6237\u8f93\u5165\u7684\u6570\u7ec4\u8f6c\u6362\u4e3a\u7279\u5b9a\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5728\u63a5\u6536\u7528\u6237\u8f93\u5165\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u7279\u5b9a\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5982\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002\u53ef\u4ee5\u5728\u5c06\u8f93\u5165\u6dfb\u52a0\u5230\u6570\u7ec4\u4e4b\u524d\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u53ef\u4ee5\u7b80\u5316\u8fd9\u4e00\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8981\u6c42\u7528\u6237\u8f93\u5165\u591a\u4e2a\u6570\u5b57\uff0c\u5e76\u4f7f\u7528\u7a7a\u683c\u5206\u9694\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff1a<\/p>\n<pre><code class=\"language-python\">input_string = input(&quot;\u8bf7\u8f93\u5165\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u5206\u9694\uff1a&quot;)\nnumbers = list(map(int, input_string.split()))\nprint(&quot;\u60a8\u8f93\u5165\u7684\u6570\u5b57\u662f\uff1a&quot;, numbers)\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528NumPy\u6570\u7ec4\u6765\u5904\u7406\u7528\u6237\u8f93\u5165\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u7684NumPy\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u5904\u7406\u80fd\u529b\uff0c\u9002\u5408\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u3002\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>array()<\/code>\u51fd\u6570\u5c06\u7528\u6237\u8f93\u5165\u7684\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u7136\u540e\u53ef\u4ee5\u50cf\u8fd9\u6837\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\ninput_string = input(&quot;\u8bf7\u8f93\u5165\u6570\u5b57\uff0c\u7528\u7a7a\u683c\u5206\u9694\uff1a&quot;)\nnumbers = np.array(input_string.split(), dtype=float)  # \u521b\u5efaNumPy\u6570\u7ec4\nprint(&quot;\u60a8\u8f93\u5165\u7684NumPy\u6570\u7ec4\u662f\uff1a&quot;, numbers)\n<\/code><\/pre>\n<p>\u8fd9\u4f7f\u5f97\u540e\u7eed\u7684\u6570\u503c\u8fd0\u7b97\u66f4\u52a0\u9ad8\u6548\u548c\u4fbf\u6377\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u901a\u8fc7\u6570\u7ec4\uff08\u5373\u5217\u8868\uff09\u83b7\u53d6\u8f93\u5165\uff0c\u6bd4\u5982\u4f7f\u7528input\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001map\u51fd\u6570\u7b49\u3002\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1160973,"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\/1160964"}],"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=1160964"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160964\/revisions"}],"predecessor-version":[{"id":1160974,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160964\/revisions\/1160974"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1160973"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1160964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1160964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1160964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}