{"id":989644,"date":"2024-12-27T08:13:19","date_gmt":"2024-12-27T00:13:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/989644.html"},"modified":"2024-12-27T08:13:23","modified_gmt":"2024-12-27T00:13:23","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%bb%ba%e7%ab%8b%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/989644.html","title":{"rendered":"\u5982\u4f55\u7528Python\u5efa\u7acb\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064733\/c128b36b-8c1c-4d9b-9ab0-5ce4462bd8d4.webp\" alt=\"\u5982\u4f55\u7528Python\u5efa\u7acb\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u7528Python\u5efa\u7acb\u5217\u8868\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u65b9\u62ec\u53f7\u3001\u4f7f\u7528list()\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u65b9\u62ec\u53f7\uff0c\u5b83\u80fd\u591f\u5feb\u901f\u3001\u76f4\u89c2\u5730\u521b\u5efa\u4e00\u4e2a\u5217\u8868\u3002\u901a\u8fc7\u5728\u65b9\u62ec\u53f7\u4e2d\u6dfb\u52a0\u9017\u53f7\u5206\u9694\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u4e00\u4e2a\u5217\u8868\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u5217\u8868\uff1a[1, 2, 3, 4]\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u65b9\u62ec\u53f7\u521b\u5efa\u5217\u8868<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u65b9\u62ec\u53f7\u662fPython\u4e2d\u6700\u76f4\u63a5\u548c\u5e38\u7528\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u6cd5\u3002\u65b9\u62ec\u53f7\u5185\u53ef\u4ee5\u5305\u542b\u4efb\u610f\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u751a\u81f3\u53ef\u4ee5\u662f\u6df7\u5408\u7c7b\u578b\u3002\u5217\u8868\u4e2d\u7684\u5143\u7d20\u901a\u8fc7\u9017\u53f7\u5206\u9694\uff0c\u5217\u8868\u7684\u957f\u5ea6\u53ef\u4ee5\u52a8\u6001\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><p>\u5217\u8868\u4e0d\u4ec5\u9650\u4e8e\u5b58\u50a8\u6574\u6570\u6216\u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u662f\u4efb\u610f\u5bf9\u8c61\u7684\u96c6\u5408\uff0c\u5305\u62ec\u5176\u4ed6\u5217\u8868\uff08\u5373\u5d4c\u5957\u5217\u8868\uff09\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u65b9\u62ec\u53f7\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6574\u6570\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5217\u8868\u5305\u542b\u4e94\u4e2a\u6574\u6570\u3002\u60a8\u53ef\u4ee5\u968f\u65f6\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\uff0c\u8fd9\u4f7f\u5f97\u5b83\u975e\u5e38\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528list()\u51fd\u6570\u521b\u5efa\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ece\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5143\u7ec4\u6216\u5b57\u7b26\u4e32\uff09\u4e2d\u521b\u5efa\u5217\u8868\u3002Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684<code>list()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u76f8\u540c\u5143\u7d20\u7684\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4ece\u4e00\u4e2a\u5b57\u7b26\u4e32\u521b\u5efa\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u90fd\u662f\u4e00\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;hello&quot;<\/p>\n<p>char_list = list(my_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u5c06\u751f\u6210\u4e00\u4e2a\u5217\u8868<code>[&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u7279\u6027\uff0c\u5141\u8bb8\u60a8\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u521b\u5efa\u5217\u8868\u3002\u5b83\u4e0d\u4ec5\u7b80\u6d01\u800c\u4e14\u9ad8\u6548\uff0c\u5c24\u5176\u5728\u9700\u8981\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u8fd0\u7b97\u6216\u7b5b\u9009\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u662f\uff1a<code>[expression for item in iterable]<\/code>\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b1\u523010\u5e73\u65b9\u6570\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = [x2 for x in range(1, 11)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5217\u8868\u5c06\u5305\u542b\u5143\u7d20<code>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u521b\u5efa\u5217\u8868<\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u662f\u60f0\u6027\u7684\uff0c\u4e0d\u4f1a\u7acb\u5373\u751f\u6210\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002\u76f8\u53cd\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u53ef\u4ee5\u6309\u9700\u751f\u6210\u5143\u7d20\u3002\u8fd9\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u4e00\u6b21\u6027\u5360\u7528\u5927\u91cf\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><p>\u8981\u4ece\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u521b\u5efa\u5217\u8868\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">gen_exp = (x2 for x in range(1, 11))<\/p>\n<p>squares = list(gen_exp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u751f\u6210\u4e0e\u524d\u9762\u4f8b\u5b50\u76f8\u540c\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5217\u8868\u7684\u57fa\u672c\u64cd\u4f5c<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bbf\u95ee\u5217\u8868\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\uff0c\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002\u8d1f\u7d22\u5f15\u7528\u4e8e\u4ece\u5217\u8868\u672b\u5c3e\u5f00\u59cb\u8ba1\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>print(my_list[0])  # \u8f93\u51fa\uff1a1<\/p>\n<p>print(my_list[-1]) # \u8f93\u51fa\uff1a5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fee\u6539\u5217\u8868\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u4fee\u6539\u5176\u5185\u5bb9\u3002\u901a\u8fc7\u7d22\u5f15\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list[0] = 10<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[10, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>append()<\/code>\u65b9\u6cd5\u5728\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20\uff0c\u6216\u8005\u4f7f\u7528<code>insert()<\/code>\u65b9\u6cd5\u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list.append(6)<\/p>\n<p>my_list.insert(1, 15)<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[10, 15, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5220\u9664\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u5220\u9664\u9996\u6b21\u51fa\u73b0\u7684\u6307\u5b9a\u503c\uff0c\u6216\u8005\u4f7f\u7528<code>pop()<\/code>\u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u7d22\u5f15\u5904\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list.remove(15)<\/p>\n<p>my_list.pop(0)<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u5217\u8868\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5217\u8868\u5207\u7247<\/strong><\/p>\n<\/p>\n<p><p>\u5207\u7247\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u5217\u8868\u7684\u5b50\u96c6\u3002\u5207\u7247\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>list[start:end:step]<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sub_list = my_list[1:4]<\/p>\n<p>print(sub_list)  # \u8f93\u51fa\uff1a[3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5217\u8868\u904d\u5386<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for item in my_list:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5217\u8868\u6392\u5e8f<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u53ef\u4ee5\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\u3002<code>sort()<\/code>\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u5217\u8868\uff0c\u800c<code>sorted()<\/code>\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list.sort()<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5217\u8868\u7684\u590d\u5236<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u6216<code>copy()<\/code>\u65b9\u6cd5\u590d\u5236\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_list = my_list[:]<\/p>\n<p>another_list = my_list.copy()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u5217\u8868\u5728Python\u4e2d\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u5728Python\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff0c\u5176\u7075\u6d3b\u6027\u548c\u529f\u80fd\u4f7f\u5f97\u5b83\u5728\u6570\u636e\u5904\u7406\u3001\u5b58\u50a8\u548c\u64cd\u4f5c\u4e2d\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u96c6\u5408\u5b58\u50a8<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u662f\u5b58\u50a8\u76f8\u5173\u6570\u636e\u96c6\u7684\u7406\u60f3\u9009\u62e9\u3002\u4f8b\u5982\uff0c\u5b58\u50a8\u5b66\u751f\u7684\u59d3\u540d\u3001\u8003\u8bd5\u6210\u7ee9\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u7ed3\u6784\u6784\u5efa<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u53ef\u4ee5\u4f5c\u4e3a\u6784\u5efa\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u57fa\u7840\uff0c\u4f8b\u5982\u6808\u3001\u961f\u5217\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u5206\u6790<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5217\u8868\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u548c\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u4f8b\u5982\u4ece\u6570\u636e\u5e93\u4e2d\u63d0\u53d6\u7684\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u52a8\u6001\u6570\u7ec4<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u53ef\u4ee5\u52a8\u6001\u589e\u957f\u548c\u7f29\u51cf\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f5c\u4e3a\u52a8\u6001\u6570\u7ec4\u6765\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516b\u3001\u4f18\u5316\u5217\u8868\u4f7f\u7528\u7684\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n<\/p>\n<p><p>\u867d\u7136\u5217\u8868\u975e\u5e38\u7075\u6d3b\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u53ef\u80fd\u66f4\u5408\u9002\u3002\u4f8b\u5982\uff0c\u9700\u8981\u5feb\u901f\u67e5\u627e\u65f6\uff0c\u5b57\u5178\u53ef\u80fd\u66f4\u597d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u590d\u5236<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u5217\u8868\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u590d\u5236\u64cd\u4f5c\uff0c\u4ee5\u8282\u7701\u5185\u5b58\u548c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u751f\u6210\u5668\u8282\u7701\u5185\u5b58<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\u65f6\uff0c\u8003\u8651\u4f7f\u7528\u751f\u6210\u5668\u4ee5\u907f\u514d\u5c06\u6240\u6709\u6570\u636e\u540c\u65f6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5bf9\u5217\u8868\u7684\u6df1\u5165\u7406\u89e3\u548c\u719f\u7ec3\u8fd0\u7528\uff0c\u60a8\u53ef\u4ee5\u5728Python\u7f16\u7a0b\u4e2d\u5b9e\u73b0\u9ad8\u6548\u7684\u6570\u636e\u5b58\u50a8\u548c\u5904\u7406\uff0c\u5145\u5206\u53d1\u6325Python\u7684\u5f3a\u5927\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u975e\u5e38\u7b80\u5355\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u65b9\u62ec\u53f7\u6765\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<code>my_list = []<\/code>\u3002\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>my_list<\/code>\u7684\u7a7a\u5217\u8868\uff0c\u4e4b\u540e\u4f60\u53ef\u4ee5\u5411\u5176\u4e2d\u6dfb\u52a0\u5143\u7d20\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u7528\u54ea\u4e9b\u65b9\u6cd5\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f<code>append()<\/code>\u548c<code>extend()<\/code>\u3002<code>append()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u5355\u4e2a\u5143\u7d20\uff0c\u800c<code>extend()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u53e6\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u6216\u5143\u7ec4\uff09\u4e2d\u7684\u6240\u6709\u5143\u7d20\u6dfb\u52a0\u5230\u539f\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>my_list.append(5)<\/code>\u4f1a\u5728<code>my_list<\/code>\u7684\u672b\u5c3e\u6dfb\u52a0\u6570\u5b575\uff0c\u800c<code>my_list.extend([1, 2, 3])<\/code>\u4f1a\u5c061\u30012\u548c3\u6dfb\u52a0\u5230<code>my_list<\/code>\u4e2d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u5217\u8868\u4e2d\u5220\u9664\u5143\u7d20\uff1f<\/strong><br \/>\u5220\u9664\u5143\u7d20\u7684\u65b9\u5f0f\u6709\u5f88\u591a\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u901a\u8fc7\u503c\u5220\u9664\u7279\u5b9a\u5143\u7d20\uff0c\u6216\u8005\u4f7f\u7528<code>pop()<\/code>\u65b9\u6cd5\u6839\u636e\u7d22\u5f15\u5220\u9664\u5143\u7d20\u3002<code>remove()<\/code>\u65b9\u6cd5\u4f1a\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u503c\uff0c\u800c<code>pop()<\/code>\u65b9\u6cd5\u5219\u4f1a\u8fd4\u56de\u5e76\u5220\u9664\u6307\u5b9a\u7d22\u5f15\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>my_list.remove(2)<\/code>\u4f1a\u5220\u9664\u503c\u4e3a2\u7684\u5143\u7d20\uff0c\u800c<code>my_list.pop(0)<\/code>\u4f1a\u5220\u9664\u5e76\u8fd4\u56de\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u5efa\u7acb\u5217\u8868\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u65b9\u62ec\u53f7\u3001\u4f7f\u7528list()\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u3002\u5176\u4e2d\uff0c\u6700 [&hellip;]","protected":false},"author":3,"featured_media":989654,"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\/989644"}],"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=989644"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/989644\/revisions"}],"predecessor-version":[{"id":989655,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/989644\/revisions\/989655"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/989654"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=989644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=989644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=989644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}