{"id":1084481,"date":"2025-01-08T13:07:42","date_gmt":"2025-01-08T05:07:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1084481.html"},"modified":"2025-01-08T13:07:45","modified_gmt":"2025-01-08T05:07:45","slug":"python%e5%a6%82%e4%bd%95%e5%be%80%e5%88%97%e8%a1%a8%e9%87%8c%e8%be%93%e5%85%a5%e7%ba%af%e6%95%b0%e5%ad%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1084481.html","title":{"rendered":"python\u5982\u4f55\u5f80\u5217\u8868\u91cc\u8f93\u5165\u7eaf\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194817\/68d1621e-4163-45c8-8ce3-8ab3e20c8e0d.webp\" alt=\"python\u5982\u4f55\u5f80\u5217\u8868\u91cc\u8f93\u5165\u7eaf\u6570\u5b57\" \/><\/p>\n<p><p> <strong>Python\u5f80\u5217\u8868\u91cc\u8f93\u5165\u7eaf\u6570\u5b57\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u7684<code>append<\/code>\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4ee5\u53ca\u5faa\u73af\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528<code>append<\/code>\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9010\u4e2a\u5c06\u6570\u5b57\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528<code>append<\/code>\u65b9\u6cd5\u5f80\u5217\u8868\u91cc\u8f93\u5165\u7eaf\u6570\u5b57\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>append<\/code>\u65b9\u6cd5\u5c06\u7eaf\u6570\u5b57\u9010\u4e2a\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = []<\/p>\n<p>numbers.append(1)<\/p>\n<p>numbers.append(2)<\/p>\n<p>numbers.append(3)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u4ece\u591a\u4e2a\u65b9\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5f80\u5217\u8868\u91cc\u8f93\u5165\u7eaf\u6570\u5b57\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528<code>append<\/code>\u65b9\u6cd5<\/strong><\/h2>\n<p><p>\u4f7f\u7528<code>append<\/code>\u65b9\u6cd5\u662f\u5f80\u5217\u8868\u91cc\u8f93\u5165\u7eaf\u6570\u5b57\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<code>append<\/code>\u65b9\u6cd5\u5c06\u4e00\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u7684\u672b\u5c3e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868<\/p>\n<p>numbers = []<\/p>\n<h2><strong>\u4f7f\u7528append\u65b9\u6cd5\u6dfb\u52a0\u7eaf\u6570\u5b57<\/strong><\/h2>\n<p>numbers.append(1)<\/p>\n<p>numbers.append(2)<\/p>\n<p>numbers.append(3)<\/p>\n<p>numbers.append(4)<\/p>\n<p>numbers.append(5)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u7a7a\u5217\u8868<code>numbers<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>append<\/code>\u65b9\u6cd5\u9010\u4e2a\u5c06\u7eaf\u6570\u5b57\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u6700\u7ec8\uff0c\u5217\u8868<code>numbers<\/code>\u5305\u542b\u4e86\u6211\u4eec\u6dfb\u52a0\u7684\u6240\u6709\u6570\u5b57\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u5f0f\u3002\u53ef\u4ee5\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u76f4\u63a5\u751f\u6210\u5305\u542b\u7eaf\u6570\u5b57\u7684\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u7eaf\u6570\u5b57\u7684\u5217\u8868<\/p>\n<p>numbers = [i for i in range(1, 6)]<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>range<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u5305\u542b\u4ece1\u52305\u7684\u6570\u5b57\u5e8f\u5217\uff0c\u5e76\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5217\u8868<code>numbers<\/code>\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u5faa\u73af<\/strong><\/h2>\n<p><p>\u4f7f\u7528\u5faa\u73af\u53ef\u4ee5\u5c06\u4e00\u7ec4\u7eaf\u6570\u5b57\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6216\u8005<code>while<\/code>\u5faa\u73af\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528for\u5faa\u73af\u5c06\u7eaf\u6570\u5b57\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/p>\n<p>numbers = []<\/p>\n<p>for i in range(1, 6):<\/p>\n<p>    numbers.append(i)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u4ece1\u52305\u7684\u6570\u5b57\uff0c\u5e76\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u4f7f\u7528<code>append<\/code>\u65b9\u6cd5\u5c06\u5f53\u524d\u6570\u5b57\u6dfb\u52a0\u5230\u5217\u8868<code>numbers<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528<code>extend<\/code>\u65b9\u6cd5<\/strong><\/h2>\n<p><p>\u5982\u679c\u5df2\u7ecf\u6709\u4e00\u4e2a\u5305\u542b\u7eaf\u6570\u5b57\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u4f8b\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528<code>extend<\/code>\u65b9\u6cd5\u5c06\u8fd9\u4e9b\u6570\u5b57\u4e00\u6b21\u6027\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528extend\u65b9\u6cd5\u5c06\u7eaf\u6570\u5b57\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/p>\n<p>numbers = []<\/p>\n<p>numbers.extend([1, 2, 3, 4, 5])<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>extend<\/code>\u65b9\u6cd5\u5c06\u4e00\u4e2a\u5305\u542b\u7eaf\u6570\u5b57\u7684\u5217\u8868<code>[1, 2, 3, 4, 5]<\/code>\u6dfb\u52a0\u5230\u7a7a\u5217\u8868<code>numbers<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u4f7f\u7528<code>insert<\/code>\u65b9\u6cd5<\/strong><\/h2>\n<p><p><code>insert<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5728\u5217\u8868\u7684\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u3002\u867d\u7136<code>insert<\/code>\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u5728\u5217\u8868\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5728\u5217\u8868\u672b\u5c3e\u63d2\u5165\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528insert\u65b9\u6cd5\u5c06\u7eaf\u6570\u5b57\u6dfb\u52a0\u5230\u5217\u8868\u672b\u5c3e<\/p>\n<p>numbers = []<\/p>\n<p>numbers.insert(len(numbers), 1)<\/p>\n<p>numbers.insert(len(numbers), 2)<\/p>\n<p>numbers.insert(len(numbers), 3)<\/p>\n<p>numbers.insert(len(numbers), 4)<\/p>\n<p>numbers.insert(len(numbers), 5)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>insert<\/code>\u65b9\u6cd5\u5728\u5217\u8868<code>numbers<\/code>\u7684\u672b\u5c3e\u63d2\u5165\u7eaf\u6570\u5b57\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u4f7f\u7528<code>+<\/code>\u8fd0\u7b97\u7b26<\/strong><\/h2>\n<p><p><code>+<\/code>\u8fd0\u7b97\u7b26\u53ef\u4ee5\u7528\u4e8e\u8fde\u63a5\u4e24\u4e2a\u5217\u8868\u3002\u5982\u679c\u6709\u4e00\u4e2a\u5305\u542b\u7eaf\u6570\u5b57\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528<code>+<\/code>\u8fd0\u7b97\u7b26\u5c06\u5176\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u672b\u5c3e\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528+\u8fd0\u7b97\u7b26\u5c06\u7eaf\u6570\u5b57\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/p>\n<p>numbers = []<\/p>\n<p>numbers = numbers + [1, 2, 3, 4, 5]<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>+<\/code>\u8fd0\u7b97\u7b26\u5c06\u4e00\u4e2a\u5305\u542b\u7eaf\u6570\u5b57\u7684\u5217\u8868<code>[1, 2, 3, 4, 5]<\/code>\u6dfb\u52a0\u5230\u7a7a\u5217\u8868<code>numbers<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u4f7f\u7528<code>*<\/code>\u8fd0\u7b97\u7b26<\/strong><\/h2>\n<p><p><code>*<\/code>\u8fd0\u7b97\u7b26\u53ef\u4ee5\u7528\u4e8e\u5c06\u4e00\u4e2a\u5217\u8868\u91cd\u590d\u591a\u6b21\u3002\u5982\u679c\u9700\u8981\u5728\u5217\u8868\u4e2d\u5305\u542b\u591a\u4e2a\u76f8\u540c\u7684\u7eaf\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528<code>*<\/code>\u8fd0\u7b97\u7b26\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528*\u8fd0\u7b97\u7b26\u5c06\u591a\u4e2a\u76f8\u540c\u7684\u7eaf\u6570\u5b57\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/p>\n<p>numbers = [1] * 5<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 1, 1, 1, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>*<\/code>\u8fd0\u7b97\u7b26\u5c06\u6570\u5b57<code>1<\/code>\u91cd\u590d5\u6b21\uff0c\u751f\u6210\u4e00\u4e2a\u5305\u542b5\u4e2a\u6570\u5b57<code>1<\/code>\u7684\u5217\u8868<code>numbers<\/code>\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u4f7f\u7528<code>map<\/code>\u51fd\u6570<\/strong><\/h2>\n<p><p><code>map<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u4e8e\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u53ef\u4ee5\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u5c06\u4e00\u7ec4\u6570\u5b57\u8f6c\u6362\u4e3a\u5305\u542b\u7eaf\u6570\u5b57\u7684\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528map\u51fd\u6570\u5c06\u4e00\u7ec4\u6570\u5b57\u8f6c\u6362\u4e3a\u5305\u542b\u7eaf\u6570\u5b57\u7684\u5217\u8868<\/p>\n<p>numbers = list(map(int, range(1, 6)))<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u5c06<code>range<\/code>\u51fd\u6570\u751f\u6210\u7684\u6570\u5b57\u5e8f\u5217\u8f6c\u6362\u4e3a\u5305\u542b\u7eaf\u6570\u5b57\u7684\u5217\u8868<code>numbers<\/code>\u3002<\/p>\n<\/p>\n<h2><strong>\u4e5d\u3001\u4f7f\u7528<code>itertools<\/code>\u6a21\u5757<\/strong><\/h2>\n<p><p><code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7ec4\u7528\u4e8e\u9ad8\u6548\u5904\u7406\u8fed\u4ee3\u5668\u7684\u51fd\u6570\u3002\u53ef\u4ee5\u4f7f\u7528<code>itertools<\/code>\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u751f\u6210\u5305\u542b\u7eaf\u6570\u5b57\u7684\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u4f7f\u7528itertools\u6a21\u5757\u751f\u6210\u5305\u542b\u7eaf\u6570\u5b57\u7684\u5217\u8868<\/strong><\/h2>\n<p>numbers = list(itertools.islice(itertools.count(1), 5))<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>itertools.count<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u4ece1\u5f00\u59cb\u7684\u65e0\u9650\u8fed\u4ee3\u5668\uff0c\u5e76\u4f7f\u7528<code>itertools.islice<\/code>\u51fd\u6570\u622a\u53d6\u524d5\u4e2a\u5143\u7d20\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u5305\u542b\u7eaf\u6570\u5b57\u7684\u5217\u8868<code>numbers<\/code>\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u3001\u4f7f\u7528<code>numpy<\/code>\u5e93<\/strong><\/h2>\n<p><p><code>numpy<\/code>\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u5904\u7406\u6570\u7ec4\u7684\u51fd\u6570\u3002\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u751f\u6210\u5305\u542b\u7eaf\u6570\u5b57\u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528numpy\u5e93\u751f\u6210\u5305\u542b\u7eaf\u6570\u5b57\u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>numbers = np.arange(1, 6).tolist()<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>numpy.arange<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u5305\u542b\u4ece1\u52305\u7684\u6570\u5b57\u7684\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528<code>tolist<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868<code>numbers<\/code>\u3002<\/p>\n<\/p>\n<h2><strong>\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u5f80\u5217\u8868\u91cc\u8f93\u5165\u7eaf\u6570\u5b57\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>append<\/code>\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5faa\u73af\u3001<code>extend<\/code>\u65b9\u6cd5\u3001<code>insert<\/code>\u65b9\u6cd5\u3001<code>+<\/code>\u8fd0\u7b97\u7b26\u3001<code>*<\/code>\u8fd0\u7b97\u7b26\u3001<code>map<\/code>\u51fd\u6570\u3001<code>itertools<\/code>\u6a21\u5757\u4ee5\u53ca<code>numpy<\/code>\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u5982\u679c\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u4ea4\u6d41\u8ba8\u8bba\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u7eaf\u6570\u5b57\u7684\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u7eaf\u6570\u5b57\u7684\u5217\u8868\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u4f7f\u7528\u65b9\u62ec\u53f7\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868\uff0c\u4f8b\u5982 <code>numbers = [1, 2, 3, 4, 5]<\/code>\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u751f\u6210\u6570\u5b57\u5217\u8868\uff0c\u5982 <code>numbers = [x for x in range(1, 6)]<\/code>\uff0c\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u5305\u542b\u4ece1\u52305\u7684\u6570\u5b57\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u5982\u4f55\u4ece\u7528\u6237\u8f93\u5165\u4e2d\u83b7\u53d6\u7eaf\u6570\u5b57\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u5e76\u7ed3\u5408 <code>isdigit()<\/code> \u65b9\u6cd5\u9a8c\u8bc1\u8f93\u5165\u662f\u5426\u4e3a\u7eaf\u6570\u5b57\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = []\nuser_input = input(&quot;\u8bf7\u8f93\u5165\u6570\u5b57\uff08\u7528\u7a7a\u683c\u5206\u9694\uff09\uff1a&quot;)\nfor item in user_input.split():\n    if item.isdigit():\n        numbers.append(int(item))\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u7528\u6237\u8f93\u5165\u7684\u6bcf\u4e2a\u6570\u5b57\u90fd\u4f1a\u88ab\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u6570\u5b57\u662f\u552f\u4e00\u7684\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u6570\u5b57\u4e0d\u91cd\u590d\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u5b58\u50a8\u6570\u5b57\uff0c\u56e0\u4e3a\u96c6\u5408\u672c\u8eab\u4e0d\u5141\u8bb8\u91cd\u590d\u9879\u3002\u53ef\u4ee5\u5728\u6dfb\u52a0\u6570\u5b57\u4e4b\u524d\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u518d\u8f6c\u6362\u56de\u5217\u8868\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = set()\nuser_input = input(&quot;\u8bf7\u8f93\u5165\u6570\u5b57\uff08\u7528\u7a7a\u683c\u5206\u9694\uff09\uff1a&quot;)\nfor item in user_input.split():\n    if item.isdigit():\n        numbers.add(int(item))\nunique_numbers = list(numbers)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c<code>unique_numbers<\/code> \u5217\u8868\u5c06\u4ec5\u5305\u542b\u552f\u4e00\u7684\u6570\u5b57\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5f80\u5217\u8868\u91cc\u8f93\u5165\u7eaf\u6570\u5b57\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u7684append\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4ee5\u53ca\u5faa\u73af\u7b49\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1084493,"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\/1084481"}],"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=1084481"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084481\/revisions"}],"predecessor-version":[{"id":1084496,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084481\/revisions\/1084496"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1084493"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1084481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1084481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1084481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}