{"id":1111996,"date":"2025-01-08T17:34:32","date_gmt":"2025-01-08T09:34:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1111996.html"},"modified":"2025-01-08T17:34:34","modified_gmt":"2025-01-08T09:34:34","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e5%88%97%e8%a1%a8%e4%b8%ad%e6%9c%80%e5%90%8e%e4%b8%80%e4%b8%aa","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1111996.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u5217\u8868\u4e2d\u6700\u540e\u4e00\u4e2a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074218\/5c6bdcd0-39ea-436f-89bc-435c203703bd.webp\" alt=\"python\u5982\u4f55\u5220\u9664\u5217\u8868\u4e2d\u6700\u540e\u4e00\u4e2a\" \/><\/p>\n<p><p> Python\u5220\u9664\u5217\u8868\u4e2d\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528<code>pop()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u3001\u4f7f\u7528\u5207\u7247\u64cd\u4f5c<\/strong>\u3002\u5176\u4e2d\uff0c<code>pop()<\/code>\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\uff0c<code>pop()<\/code>\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u5220\u9664\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u8fd8\u53ef\u4ee5\u5c06\u5176\u8fd4\u56de\uff0c\u65b9\u4fbf\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>pop()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>pop()<\/code>\u65b9\u6cd5\u662fPython\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5185\u7f6e\u65b9\u6cd5\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5220\u9664\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u8fd8\u53ef\u4ee5\u5c06\u5176\u8fd4\u56de\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>last_element = my_list.pop()<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p>print(last_element)  # \u8f93\u51fa: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002\u5982\u679c\u4f60\u53ea\u9700\u8981\u5220\u9664\u5143\u7d20\uff0c\u4e0d\u9700\u8981\u8fd4\u56de\u503c\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528<code>pop()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>del<\/code>\u8bed\u53e5<\/h3>\n<\/p>\n<p><p><code>del<\/code>\u8bed\u53e5\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\uff0c\u5305\u62ec\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>del my_list[-1]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4f1a\u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\uff0c\u5982\u679c\u4f60\u53ea\u9700\u8981\u5220\u9664\u5143\u7d20\uff0c\u4e0d\u9700\u8981\u8fd4\u56de\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5207\u7247\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list = my_list[:-1]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u66f4\u591a\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u4f8b\u5982\u4fdd\u7559\u539f\u59cb\u5217\u8868\u7684\u526f\u672c\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4e09\u79cd\u65b9\u6cd5\u90fd\u6709\u5404\u81ea\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff1a<strong><code>pop()<\/code>\u65b9\u6cd5\u9002\u7528\u4e8e\u5220\u9664\u5e76\u8fd4\u56de\u5143\u7d20\u7684\u573a\u666f\uff0c<code>del<\/code>\u8bed\u53e5\u9002\u7528\u4e8e\u53ea\u9700\u8981\u5220\u9664\u5143\u7d20\u7684\u573a\u666f\uff0c\u5207\u7247\u64cd\u4f5c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u66f4\u591a\u64cd\u4f5c\u7684\u573a\u666f<\/strong>\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e09\u79cd\u65b9\u6cd5\u7684\u5177\u4f53\u4f7f\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<p><h2>\u4e00\u3001<code>pop()<\/code>\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5220\u9664\u5e76\u8fd4\u56de\u5143\u7d20<\/strong>\uff1a\u5982\u679c\u4f60\u9700\u8981\u5220\u9664\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u4e14\u9700\u8981\u8fd4\u56de\u8fd9\u4e2a\u5143\u7d20\uff0c<code>pop()<\/code>\u65b9\u6cd5\u662f\u6700\u5408\u9002\u7684\u9009\u62e9\u3002<\/li>\n<li><strong>\u5904\u7406\u5806\u6808\u6570\u636e\u7ed3\u6784<\/strong>\uff1a\u5728\u5b9e\u73b0\u5806\u6808\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u5220\u9664\u5e76\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c<code>pop()<\/code>\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u8fd9\u79cd\u573a\u666f\u3002<\/li>\n<\/ol>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5217\u8868\u4e3a\u7a7a\u65f6\u7684\u5904\u7406<\/strong>\uff1a\u5982\u679c\u5217\u8868\u4e3a\u7a7a\uff0c\u8c03\u7528<code>pop()<\/code>\u65b9\u6cd5\u4f1a\u5f15\u53d1<code>IndexError<\/code>\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u8c03\u7528<code>pop()<\/code>\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5217\u8868\u4e0d\u4e3a\u7a7a\u3002<\/li>\n<li><strong>\u65f6\u95f4\u590d\u6742\u5ea6<\/strong>\uff1a<code>pop()<\/code>\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u975e\u5e38\u9ad8\u6548\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def pop_example():<\/p>\n<p>    my_list = [1, 2, 3, 4, 5]<\/p>\n<p>    if my_list:<\/p>\n<p>        last_element = my_list.pop()<\/p>\n<p>        print(&quot;Updated List:&quot;, my_list)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p>        print(&quot;Popped Element:&quot;, last_element)  # \u8f93\u51fa: 5<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;List is empty&quot;)<\/p>\n<p>pop_example()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001<code>del<\/code>\u8bed\u53e5<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<ol>\n<li><strong>\u53ea\u9700\u8981\u5220\u9664\u5143\u7d20<\/strong>\uff1a\u5982\u679c\u4f60\u53ea\u9700\u8981\u5220\u9664\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u800c\u4e0d\u9700\u8981\u8fd4\u56de\u8fd9\u4e2a\u5143\u7d20\uff0c<code>del<\/code>\u8bed\u53e5\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/li>\n<li><strong>\u5220\u9664\u591a\u4e2a\u5143\u7d20<\/strong>\uff1a<code>del<\/code>\u8bed\u53e5\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\uff0c\u4f8b\u5982\u5220\u9664\u6307\u5b9a\u8303\u56f4\u5185\u7684\u5143\u7d20\u3002<\/li>\n<\/ol>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5217\u8868\u4e3a\u7a7a\u65f6\u7684\u5904\u7406<\/strong>\uff1a\u5982\u679c\u5217\u8868\u4e3a\u7a7a\uff0c\u8c03\u7528<code>del<\/code>\u8bed\u53e5\u5220\u9664\u5143\u7d20\u4f1a\u5f15\u53d1<code>IndexError<\/code>\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u8c03\u7528<code>del<\/code>\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5217\u8868\u4e0d\u4e3a\u7a7a\u3002<\/li>\n<li><strong>\u5220\u9664\u591a\u4e2a\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6<\/strong>\uff1a<code>del<\/code>\u8bed\u53e5\u5220\u9664\u591a\u4e2a\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u5220\u9664\u5143\u7d20\u7684\u6570\u91cf\uff0c\u56e0\u6b64\u5728\u5220\u9664\u5927\u91cf\u5143\u7d20\u65f6\u9700\u8981\u6ce8\u610f\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def del_example():<\/p>\n<p>    my_list = [1, 2, 3, 4, 5]<\/p>\n<p>    if my_list:<\/p>\n<p>        del my_list[-1]<\/p>\n<p>        print(&quot;Updated List:&quot;, my_list)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;List is empty&quot;)<\/p>\n<p>del_example()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u5207\u7247\u64cd\u4f5c<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<ol>\n<li><strong>\u4fdd\u7559\u539f\u59cb\u5217\u8868\u526f\u672c<\/strong>\uff1a\u5982\u679c\u4f60\u9700\u8981\u5220\u9664\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u4e14\u9700\u8981\u4fdd\u7559\u539f\u59cb\u5217\u8868\u7684\u526f\u672c\uff0c\u5207\u7247\u64cd\u4f5c\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/li>\n<li><strong>\u5bf9\u5217\u8868\u8fdb\u884c\u66f4\u591a\u64cd\u4f5c<\/strong>\uff1a\u5207\u7247\u64cd\u4f5c\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u7528\u4e8e\u5bf9\u5217\u8868\u8fdb\u884c\u66f4\u591a\u64cd\u4f5c\uff0c\u4f8b\u5982\u5220\u9664\u591a\u4e2a\u5143\u7d20\u3001\u590d\u5236\u5217\u8868\u7b49\u3002<\/li>\n<\/ol>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u751f\u6210\u65b0\u5217\u8868<\/strong>\uff1a\u5207\u7247\u64cd\u4f5c\u4f1a\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u56e0\u6b64\u5728\u5904\u7406\u5927\u5217\u8868\u65f6\u9700\u8981\u6ce8\u610f\u5185\u5b58\u6d88\u8017\u3002<\/li>\n<li><strong>\u65f6\u95f4\u590d\u6742\u5ea6<\/strong>\uff1a\u5207\u7247\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(k)\uff0c\u5176\u4e2dk\u662f\u5207\u7247\u7684\u957f\u5ea6\uff0c\u56e0\u6b64\u5728\u5904\u7406\u5927\u5217\u8868\u65f6\u9700\u8981\u6ce8\u610f\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def slice_example():<\/p>\n<p>    my_list = [1, 2, 3, 4, 5]<\/p>\n<p>    if my_list:<\/p>\n<p>        my_list = my_list[:-1]<\/p>\n<p>        print(&quot;Updated List:&quot;, my_list)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;List is empty&quot;)<\/p>\n<p>slice_example()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u4e09\u79cd\u5e38\u89c1\u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u4f8b\u5982\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u548c<code>list comprehension<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>remove()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\uff0c\u4f46\u5b83\u4f1a\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\uff0c\u56e0\u6b64\u9700\u8981\u7ed3\u5408\u5217\u8868\u7684\u7d22\u5f15\u6765\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_example():<\/p>\n<p>    my_list = [1, 2, 3, 4, 5]<\/p>\n<p>    if my_list:<\/p>\n<p>        my_list.remove(my_list[-1])<\/p>\n<p>        print(&quot;Updated List:&quot;, my_list)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;List is empty&quot;)<\/p>\n<p>remove_example()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528<code>list comprehension<\/code><\/h3>\n<\/p>\n<p><p><code>list comprehension<\/code>\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list comprehension<\/code>\u5220\u9664\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def comprehension_example():<\/p>\n<p>    my_list = [1, 2, 3, 4, 5]<\/p>\n<p>    if my_list:<\/p>\n<p>        my_list = [my_list[i] for i in range(len(my_list) - 1)]<\/p>\n<p>        print(&quot;Updated List:&quot;, my_list)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;List is empty&quot;)<\/p>\n<p>comprehension_example()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5220\u9664\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5404\u81ea\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387<\/strong>\u3002\u4f8b\u5982\uff0c<code>pop()<\/code>\u65b9\u6cd5\u9002\u7528\u4e8e\u5220\u9664\u5e76\u8fd4\u56de\u5143\u7d20\u7684\u573a\u666f\uff0c<code>del<\/code>\u8bed\u53e5\u9002\u7528\u4e8e\u53ea\u9700\u8981\u5220\u9664\u5143\u7d20\u7684\u573a\u666f\uff0c\u5207\u7247\u64cd\u4f5c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u66f4\u591a\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pop()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u4f1a\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u8fd8\u4f1a\u8fd4\u56de\u8be5\u5143\u7d20\u7684\u503c\u3002\u5982\u679c\u4f60\u4e0d\u9700\u8981\u8fd4\u56de\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]\nmy_list.pop()  # \u4f7f\u7528pop()\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\n# \u6216\u8005\u4f7f\u7528del\ndel my_list[-1]  # \u4f7f\u7528del\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528<code>slice<\/code>\u65b9\u6cd5\u662f\u5426\u53ef\u4ee5\u5220\u9664\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u53bb\u6389\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u5217\u8868\uff0c\u4f46\u5b83\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u4f7f\u7528\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]\nnew_list = my_list[:-1]  # \u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u4e0d\u5305\u542b\u6700\u540e\u4e00\u4e2a\u5143\u7d20\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5728\u5904\u7406\u9700\u8981\u4fdd\u7559\u539f\u5217\u8868\u7684\u573a\u666f\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u540e\uff0c\u5982\u4f55\u786e\u8ba4\u5217\u8868\u7684\u957f\u5ea6\uff1f<\/strong><br \/>\u5728\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u6765\u786e\u8ba4\u5217\u8868\u7684\u957f\u5ea6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]\nmy_list.pop()  # \u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\nlength = len(my_list)  # \u786e\u8ba4\u5217\u8868\u7684\u957f\u5ea6\nprint(length)  # \u8f93\u51fa\u65b0\u7684\u957f\u5ea6\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u786e\u4fdd\u5217\u8868\u7684\u5185\u5bb9\u548c\u957f\u5ea6\u7b26\u5408\u9884\u671f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5220\u9664\u5217\u8868\u4e2d\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\uff1a\u4f7f\u7528pop()\u65b9\u6cd5\u3001\u4f7f\u7528del\u8bed\u53e5\u3001\u4f7f\u7528\u5207 [&hellip;]","protected":false},"author":3,"featured_media":1112006,"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\/1111996"}],"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=1111996"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111996\/revisions"}],"predecessor-version":[{"id":1112007,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111996\/revisions\/1112007"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1112006"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1111996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1111996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1111996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}