{"id":1179726,"date":"2025-01-15T18:27:41","date_gmt":"2025-01-15T10:27:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179726.html"},"modified":"2025-01-15T18:27:41","modified_gmt":"2025-01-15T10:27:41","slug":"python-%e5%88%97%e8%a1%a8%e5%a6%82%e4%bd%95%e5%a2%9e%e5%8a%a0%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179726.html","title":{"rendered":"python \u5217\u8868\u5982\u4f55\u589e\u52a0\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25114046\/ceacba88-5ca7-4a26-9dc1-d11afdb51ae5.webp\" alt=\"python \u5217\u8868\u5982\u4f55\u589e\u52a0\u5143\u7d20\" \/><\/p>\n<p><p> Python\u5217\u8868\u589e\u52a0\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528append()\u65b9\u6cd5\u3001extend()\u65b9\u6cd5\u3001insert()\u65b9\u6cd5\u3001\u52a0\u6cd5\u64cd\u4f5c\u7b26\uff08+\uff09\u548c\u5207\u7247\u8d4b\u503c\u3002<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528append()\u65b9\u6cd5\u3001\u4f7f\u7528extend()\u65b9\u6cd5\u3001\u4f7f\u7528insert()\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528append()\u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u548c\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7append()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5217\u8868\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5143\u7d20\u3002append()\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u8981\u6dfb\u52a0\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u9644\u52a0\u5230\u5217\u8868\u7684\u672b\u5c3e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3]<\/p>\n<p>my_list.append(4)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>append()\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u548c\u6613\u7528<\/strong>\uff0c\u9002\u5408\u7528\u4e8e\u9700\u8981\u9010\u4e2a\u6dfb\u52a0\u5143\u7d20\u7684\u573a\u666f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u5217\u8868\u589e\u52a0\u5143\u7d20\u7684\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528append()\u65b9\u6cd5<\/strong><\/h2>\n<p><p>append()\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\u7684\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u4f1a\u5c06\u4e00\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u7684\u672b\u5c3e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868<\/p>\n<p>my_list = []<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>my_list.append(&#39;a&#39;)<\/p>\n<p>my_list.append(&#39;b&#39;)<\/p>\n<p>my_list.append(&#39;c&#39;)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>append()\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u7b80\u5355\u76f4\u63a5\uff0c\u975e\u5e38\u9002\u5408\u9010\u4e2a\u6dfb\u52a0\u5143\u7d20\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528extend()\u65b9\u6cd5<\/strong><\/h2>\n<p><p>extend()\u65b9\u6cd5\u7528\u4e8e\u5411\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\u3002\u4e0eappend()\u4e0d\u540c\uff0cextend()\u63a5\u53d7\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\uff0c\u5e76\u5c06\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u539f\u5217\u8868\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<h2><strong>\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>my_list.extend([4, 5, 6])<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>extend()\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\uff0c\u9002\u5408\u6279\u91cf\u6dfb\u52a0\u5143\u7d20\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528insert()\u65b9\u6cd5<\/strong><\/h2>\n<p><p>insert()\u65b9\u6cd5\u7528\u4e8e\u5728\u5217\u8868\u7684\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u63d2\u5165\u4f4d\u7f6e\u7684\u7d22\u5f15\u548c\u8981\u63d2\u5165\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<h2><strong>\u5728\u7d22\u5f151\u7684\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20<\/strong><\/h2>\n<p>my_list.insert(1, &#39;a&#39;)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, &#39;a&#39;, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>insert()\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u5728\u5217\u8868\u7684\u4efb\u610f\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\uff0c\u9002\u5408\u9700\u8981\u5728\u7279\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u5143\u7d20\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528\u52a0\u6cd5\u64cd\u4f5c\u7b26\uff08+\uff09<\/strong><\/h2>\n<p><p>\u52a0\u6cd5\u64cd\u4f5c\u7b26\uff08+\uff09\u53ef\u4ee5\u7528\u4e8e\u5c06\u4e24\u4e2a\u5217\u8868\u5408\u5e76\uff0c\u4ece\u800c\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e24\u4e2a\u5217\u8868<\/p>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<h2><strong>\u5408\u5e76\u5217\u8868<\/strong><\/h2>\n<p>combined_list = list1 + list2<\/p>\n<p>print(combined_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u52a0\u6cd5\u64cd\u4f5c\u7b26\uff08+\uff09\u7684\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u76f4\u89c2\u5730\u5408\u5e76\u4e24\u4e2a\u5217\u8868\uff0c\u9002\u5408\u9700\u8981\u5408\u5e76\u591a\u4e2a\u5217\u8868\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u4f7f\u7528\u5207\u7247\u8d4b\u503c<\/strong><\/h2>\n<p><p>\u5207\u7247\u8d4b\u503c\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u5728\u5217\u8868\u4e2d\u63d2\u5165\u3001\u5220\u9664\u6216\u66ff\u6362\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3]<\/p>\n<h2><strong>\u5728\u7d22\u5f151\u7684\u4f4d\u7f6e\u63d2\u5165\u591a\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>my_list[1:1] = [&#39;a&#39;, &#39;b&#39;]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, &#39;a&#39;, &#39;b&#39;, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5207\u7247\u8d4b\u503c\u7684\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u7075\u6d3b\u5730\u64cd\u4f5c\u5217\u8868\u7684\u4efb\u610f\u90e8\u5206\uff0c\u9002\u5408\u590d\u6742\u7684\u5217\u8868\u64cd\u4f5c\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u7528\u4e8e\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u867d\u7136\u5217\u8868\u63a8\u5bfc\u5f0f\u4e3b\u8981\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u5217\u8868\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5411\u73b0\u6709\u5217\u8868\u6dfb\u52a0\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868<\/p>\n<p>my_list = []<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>my_list += [x for x in range(5)]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [0, 1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u4f18\u52bf\u5728\u4e8e\u8bed\u6cd5\u7b80\u6d01\uff0c\u9002\u5408\u9700\u8981\u6839\u636e\u67d0\u4e9b\u89c4\u5219\u751f\u6210\u5143\u7d20\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u4f7f\u7528\u96c6\u5408\u8fd0\u7b97<\/strong><\/h2>\n<p><p>\u96c6\u5408\u8fd0\u7b97\u53ef\u4ee5\u7528\u4e8e\u5408\u5e76\u3001\u4ea4\u96c6\u548c\u5dee\u96c6\u64cd\u4f5c\uff0c\u867d\u7136\u96c6\u5408\uff08set\uff09\u4e0e\u5217\u8868\uff08list\uff09\u4e0d\u540c\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u8f6c\u6362\u6765\u5b9e\u73b0\u5217\u8868\u5143\u7d20\u7684\u589e\u52a0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e24\u4e2a\u96c6\u5408<\/p>\n<p>set1 = {1, 2, 3}<\/p>\n<p>set2 = {4, 5, 6}<\/p>\n<h2><strong>\u5408\u5e76\u96c6\u5408<\/strong><\/h2>\n<p>combined_set = set1 | set2<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>combined_list = list(combined_set)<\/p>\n<p>print(combined_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u96c6\u5408\u8fd0\u7b97\u7684\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u96c6\u5408\u64cd\u4f5c\uff0c\u9002\u5408\u9700\u8981\u8fdb\u884c\u53bb\u91cd\u548c\u5408\u5e76\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u4f7f\u7528itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n<\/strong><\/h2>\n<p><p>itertools.chain\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5c06\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fde\u63a5\u6210\u4e00\u4e2a\u957f\u5e8f\u5217\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u521b\u5efa\u591a\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>list3 = [7, 8, 9]<\/p>\n<h2><strong>\u5408\u5e76\u5217\u8868<\/strong><\/h2>\n<p>combined_list = list(itertools.chain(list1, list2, list3))<\/p>\n<p>print(combined_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>itertools.chain\u7684\u4f18\u52bf\u5728\u4e8e\u9ad8\u6548\u4e14\u6613\u7528\uff0c\u9002\u5408\u9700\u8981\u5408\u5e76\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e5d\u3001\u4f7f\u7528 numpy \u5e93<\/strong><\/h2>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u5927\u91cf\u7684\u6570\u503c\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528numpy\u5e93\u3002numpy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a numpy \u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([1, 2, 3])<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>array = np.append(array, [4, 5, 6])<\/p>\n<p>print(array)  # \u8f93\u51fa: [1 2 3 4 5 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>numpy\u5e93\u7684\u4f18\u52bf\u5728\u4e8e\u9ad8\u6548\u4e14\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6570\u503c\u8ba1\u7b97\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u3001\u4f7f\u7528 deque \u6570\u636e\u7ed3\u6784<\/strong><\/h2>\n<p><p>collections.deque\u662f\u4e00\u4e2a\u53cc\u7aef\u961f\u5217\u6570\u636e\u7ed3\u6784\uff0c\u652f\u6301\u5728\u4e24\u7aef\u9ad8\u6548\u5730\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a deque<\/strong><\/h2>\n<p>dq = deque([1, 2, 3])<\/p>\n<h2><strong>\u5728\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>dq.append(4)<\/p>\n<h2><strong>\u5728\u5f00\u5934\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>dq.appendleft(0)<\/p>\n<p>print(dq)  # \u8f93\u51fa: deque([0, 1, 2, 3, 4])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>deque\u7684\u4f18\u52bf\u5728\u4e8e\u9ad8\u6548\u4e14\u7075\u6d3b\uff0c\u9002\u5408\u9700\u8981\u5728\u4e24\u7aef\u9891\u7e41\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e00\u3001\u4f7f\u7528\u94fe\u8868\u6570\u636e\u7ed3\u6784<\/strong><\/h2>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u9891\u7e41\u5730\u5728\u5217\u8868\u7684\u4e2d\u95f4\u8fdb\u884c\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u94fe\u8868\u6570\u636e\u7ed3\u6784\u3002\u867d\u7136Python\u6807\u51c6\u5e93\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b\u94fe\u8868\u5b9e\u73b0\uff0c\u4f46\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u6216\u81ea\u5df1\u5b9e\u73b0\u94fe\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5355\u5411\u94fe\u8868\u5b9e\u73b0\uff1a<\/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>        else:<\/p>\n<p>            current = self.head<\/p>\n<p>            while current.next:<\/p>\n<p>                current = current.next<\/p>\n<p>            current.next = new_node<\/p>\n<p>    def __str__(self):<\/p>\n<p>        values = []<\/p>\n<p>        current = self.head<\/p>\n<p>        while current:<\/p>\n<p>            values.append(current.value)<\/p>\n<p>            current = current.next<\/p>\n<p>        return &#39; -&gt; &#39;.join(map(str, values))<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u94fe\u8868<\/strong><\/h2>\n<p>ll = LinkedList()<\/p>\n<p>ll.append(1)<\/p>\n<p>ll.append(2)<\/p>\n<p>ll.append(3)<\/p>\n<p>print(ll)  # \u8f93\u51fa: 1 -&gt; 2 -&gt; 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u94fe\u8868\u7684\u4f18\u52bf\u5728\u4e8e\u9ad8\u6548\u7684\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\uff0c\u9002\u5408\u9700\u8981\u9891\u7e41\u5728\u4e2d\u95f4\u4f4d\u7f6e\u8fdb\u884c\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e8c\u3001\u4f7f\u7528 pandas \u5e93<\/strong><\/h2>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528pandas\u5e93\u3002pandas\u63d0\u4f9b\u4e86DataFrame\u548cSeries\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5411\u5176\u4e2d\u6dfb\u52a0\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a DataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]})<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u5217<\/strong><\/h2>\n<p>df[&#39;C&#39;] = [7, 8, 9]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pandas\u5e93\u7684\u4f18\u52bf\u5728\u4e8e\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u9002\u5408\u9700\u8981\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e09\u3001\u4f7f\u7528 defaultdict \u6570\u636e\u7ed3\u6784<\/strong><\/h2>\n<p><p>collections.defaultdict\u662f\u4e00\u4e2a\u5b57\u5178\u5b50\u7c7b\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u503c\u5de5\u5382\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u81ea\u52a8\u521d\u59cb\u5316\u952e\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a defaultdict<\/strong><\/h2>\n<p>dd = defaultdict(list)<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>dd[&#39;a&#39;].append(1)<\/p>\n<p>dd[&#39;a&#39;].append(2)<\/p>\n<p>dd[&#39;b&#39;].append(3)<\/p>\n<p>print(dd)  # \u8f93\u51fa: defaultdict(&lt;class &#39;list&#39;&gt;, {&#39;a&#39;: [1, 2], &#39;b&#39;: [3]})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>defaultdict\u7684\u4f18\u52bf\u5728\u4e8e\u81ea\u52a8\u521d\u59cb\u5316\u952e\u503c\uff0c\u9002\u5408\u9700\u8981\u9891\u7e41\u6dfb\u52a0\u65b0\u952e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u56db\u3001\u4f7f\u7528 counter \u6570\u636e\u7ed3\u6784<\/strong><\/h2>\n<p><p>collections.Counter\u662f\u4e00\u4e2a\u5b57\u5178\u5b50\u7c7b\uff0c\u7528\u4e8e\u8ba1\u6570\u53ef\u54c8\u5e0c\u5bf9\u8c61\u7684\u4e2a\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a Counter<\/strong><\/h2>\n<p>counter = Counter()<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>counter.update([&#39;a&#39;, &#39;b&#39;, &#39;a&#39;, &#39;c&#39;, &#39;b&#39;, &#39;a&#39;])<\/p>\n<p>print(counter)  # \u8f93\u51fa: Counter({&#39;a&#39;: 3, &#39;b&#39;: 2, &#39;c&#39;: 1})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Counter\u7684\u4f18\u52bf\u5728\u4e8e\u65b9\u4fbf\u5730\u8fdb\u884c\u8ba1\u6570\u64cd\u4f5c\uff0c\u9002\u5408\u9700\u8981\u7edf\u8ba1\u5143\u7d20\u9891\u7387\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e94\u3001\u4f7f\u7528 heapq \u6a21\u5757<\/strong><\/h2>\n<p><p>heapq\u6a21\u5757\u63d0\u4f9b\u4e86\u5806\u961f\u5217\u7b97\u6cd5\u7684\u5b9e\u73b0\uff0c\u53ef\u4ee5\u7528\u4e8e\u9ad8\u6548\u5730\u7ba1\u7406\u4f18\u5148\u7ea7\u961f\u5217\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import heapq<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>heap = [1, 3, 5, 7, 9]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5806<\/strong><\/h2>\n<p>heapq.heapify(heap)<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>heapq.heappush(heap, 4)<\/p>\n<p>print(heap)  # \u8f93\u51fa: [1, 3, 4, 7, 9, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>heapq\u6a21\u5757\u7684\u4f18\u52bf\u5728\u4e8e\u9ad8\u6548\u7684\u4f18\u5148\u7ea7\u961f\u5217\u64cd\u4f5c\uff0c\u9002\u5408\u9700\u8981\u7ba1\u7406\u4f18\u5148\u7ea7\u961f\u5217\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u516d\u3001\u4f7f\u7528 bisect \u6a21\u5757<\/strong><\/h2>\n<p><p>bisect\u6a21\u5757\u63d0\u4f9b\u4e86\u4fdd\u6301\u6709\u5e8f\u5217\u8868\u7684\u63d2\u5165\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u5728\u6709\u5e8f\u5217\u8868\u4e2d\u9ad8\u6548\u5730\u63d2\u5165\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import bisect<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6709\u5e8f\u5217\u8868<\/strong><\/h2>\n<p>sorted_list = [1, 2, 4, 5]<\/p>\n<h2><strong>\u63d2\u5165\u5143\u7d20<\/strong><\/h2>\n<p>bisect.insort(sorted_list, 3)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>bisect\u6a21\u5757\u7684\u4f18\u52bf\u5728\u4e8e\u9ad8\u6548\u7684\u6709\u5e8f\u63d2\u5165\u64cd\u4f5c\uff0c\u9002\u5408\u9700\u8981\u4fdd\u6301\u5217\u8868\u6709\u5e8f\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e03\u3001\u4f7f\u7528 array \u6a21\u5757<\/strong><\/h2>\n<p><p>array\u6a21\u5757\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u548c\u64cd\u4f5c\u6570\u503c\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import array<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/strong><\/h2>\n<p>arr = array.array(&#39;i&#39;, [1, 2, 3])<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>arr.append(4)<\/p>\n<p>print(arr)  # \u8f93\u51fa: array(&#39;i&#39;, [1, 2, 3, 4])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>array\u6a21\u5757\u7684\u4f18\u52bf\u5728\u4e8e\u9ad8\u6548\u7684\u6570\u503c\u6570\u636e\u5b58\u50a8\u548c\u64cd\u4f5c\uff0c\u9002\u5408\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u503c\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u516b\u3001\u4f7f\u7528 blist \u6a21\u5757<\/strong><\/h2>\n<p><p>blist\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u5217\u8868\u5b9e\u73b0\uff0c\u63d0\u4f9b\u4e86\u6bd4\u5185\u7f6e\u5217\u8868\u66f4\u5feb\u7684\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from blist import blist<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a blist<\/strong><\/h2>\n<p>bl = blist([1, 2, 3])<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>bl.append(4)<\/p>\n<p>print(bl)  # \u8f93\u51fa: blist([1, 2, 3, 4])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>blist\u7684\u4f18\u52bf\u5728\u4e8e\u9ad8\u6548\u7684\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\uff0c\u9002\u5408\u9700\u8981\u9891\u7e41\u8fdb\u884c\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e5d\u3001\u4f7f\u7528 linked_list \u6a21\u5757<\/strong><\/h2>\n<p><p>linked_list\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u94fe\u8868\u6570\u636e\u7ed3\u6784\u7684\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from linked_list import LinkedList<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u94fe\u8868<\/strong><\/h2>\n<p>ll = LinkedList()<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>ll.append(1)<\/p>\n<p>ll.append(2)<\/p>\n<p>ll.append(3)<\/p>\n<p>print(ll)  # \u8f93\u51fa: LinkedList([1, 2, 3])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>linked_list\u5e93\u7684\u4f18\u52bf\u5728\u4e8e\u63d0\u4f9b\u4e86\u94fe\u8868\u6570\u636e\u7ed3\u6784\uff0c\u9002\u5408\u9700\u8981\u4f7f\u7528\u94fe\u8868\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u5341\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6570\u636e\u7ed3\u6784<\/strong><\/h2>\n<p><p>\u5982\u679c\u5185\u7f6e\u6570\u636e\u7ed3\u6784\u548c\u7b2c\u4e09\u65b9\u5e93\u4e0d\u80fd\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u8003\u8651\u5b9e\u73b0\u81ea\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\uff1a<\/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>        self.prev = None<\/p>\n<p>class DoublyLinkedList:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.head = None<\/p>\n<p>        self.tail = 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>            self.tail = new_node<\/p>\n<p>        else:<\/p>\n<p>            self.tail.next = new_node<\/p>\n<p>            new_node.prev = self.tail<\/p>\n<p>            self.tail = new_node<\/p>\n<p>    def __str__(self):<\/p>\n<p>        values = []<\/p>\n<p>        current = self.head<\/p>\n<p>        while current:<\/p>\n<p>            values.append(current.value)<\/p>\n<p>            current = current.next<\/p>\n<p>        return &#39; &lt;-&gt; &#39;.join(map(str, values))<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u53cc\u5411\u94fe\u8868<\/strong><\/h2>\n<p>dll = DoublyLinkedList()<\/p>\n<p>dll.append(1)<\/p>\n<p>dll.append(2)<\/p>\n<p>dll.append(3)<\/p>\n<p>print(dll)  # \u8f93\u51fa: 1 &lt;-&gt; 2 &lt;-&gt; 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\u7684\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u5b8c\u5168\u6839\u636e\u9700\u6c42\u8fdb\u884c\u8bbe\u8ba1\uff0c\u9002\u5408\u9700\u8981\u7279\u6b8a\u6570\u636e\u7ed3\u6784\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u591a\u79cd\u5411Python\u5217\u8868\u589e\u52a0\u5143\u7d20\u7684\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u8fd0\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5217\u8868\u4e2d\u6dfb\u52a0\u5355\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>append()<\/code>\u65b9\u6cd5\u5411\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u5355\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3]<\/code>\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>my_list.append(4)<\/code>\u5c06\u6570\u5b574\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u6267\u884c\u540e\uff0c<code>my_list<\/code>\u5c06\u53d8\u4e3a<code>[1, 2, 3, 4]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u5217\u8868\u4e2d\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>extend()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\uff0c\u5e76\u5c06\u5176\u6240\u6709\u5143\u7d20\u6dfb\u52a0\u5230\u76ee\u6807\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>my_list.extend([5, 6, 7])<\/code>\u4f1a\u5c065\u30016\u30017\u6dfb\u52a0\u5230<code>my_list<\/code>\u4e2d\uff0c\u4f7f\u5176\u53d8\u4e3a<code>[1, 2, 3, 4, 5, 6, 7]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u5217\u8868\u7684\u7279\u5b9a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\uff1f<\/strong><br \/>\u5982\u679c\u60f3\u5728\u5217\u8868\u7684\u7279\u5b9a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>insert()<\/code>\u65b9\u6cd5\u3002\u6b64\u65b9\u6cd5\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u7d22\u5f15\u4f4d\u7f6e\uff0c\u7b2c\u4e8c\u4e2a\u662f\u8981\u63d2\u5165\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>my_list.insert(1, &#39;a&#39;)<\/code>\u4f1a\u5728\u7d22\u5f151\u7684\u4f4d\u7f6e\u63d2\u5165\u5b57\u7b26&#39;a&#39;\uff0c\u4ece\u800c\u4f7f<code>my_list<\/code>\u53d8\u4e3a<code>[1, &#39;a&#39;, 2, 3, 4]<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5217\u8868\u589e\u52a0\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528append()\u65b9\u6cd5\u3001extend()\u65b9\u6cd5\u3001insert()\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179726"}],"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=1179726"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179726\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}