{"id":1187695,"date":"2025-01-15T20:08:35","date_gmt":"2025-01-15T12:08:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1187695.html"},"modified":"2025-01-15T20:08:40","modified_gmt":"2025-01-15T12:08:40","slug":"python-list%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1187695.html","title":{"rendered":"python list\u5982\u4f55\u5220\u9664\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140230\/09bf890c-4475-4d66-9140-71fa27c2eb3b.webp\" alt=\"python list\u5982\u4f55\u5220\u9664\u6570\u636e\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u5220\u9664\u5217\u8868\u4e2d\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528remove()\u65b9\u6cd5\u3001\u4f7f\u7528pop()\u65b9\u6cd5\u3001\u4f7f\u7528del\u8bed\u53e5\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/strong>\u3002\u5176\u4e2d\uff0c<strong>remove()\u65b9\u6cd5<\/strong>\u53ef\u4ee5\u6309\u503c\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528remove()\u65b9\u6cd5<\/strong>\uff1a\u8fd9\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u6839\u636e\u503c\u5220\u9664\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u3002\u5982\u679c\u5217\u8868\u4e2d\u6709\u591a\u4e2a\u76f8\u540c\u7684\u503c\uff0c\u5b83\u53ea\u5220\u9664\u7b2c\u4e00\u4e2a\u3002\u5982\u679c\u503c\u4e0d\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\uff0c\u4f1a\u5f15\u53d1ValueError\u9519\u8bef\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 3, 5]<\/p>\n<p>my_list.remove(3)  # \u5220\u9664\u7b2c\u4e00\u4e2a\u51fa\u73b0\u76843<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[1, 2, 4, 3, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u5217\u8868\u5220\u9664\u6570\u636e\u7684\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001REMOVE()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><strong>remove()\u65b9\u6cd5<\/strong>\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002remove()\u65b9\u6cd5\u4f1a\u904d\u5386\u5217\u8868\uff0c\u627e\u5230\u7b2c\u4e00\u4e2a\u4e0e\u53c2\u6570\u503c\u5339\u914d\u7684\u5143\u7d20\u5e76\u5c06\u5176\u5220\u9664\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u5143\u7d20\uff0cremove()\u65b9\u6cd5\u4f1a\u5f15\u53d1ValueError\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 3, 5]<\/p>\n<p>my_list.remove(3)  # \u5220\u9664\u7b2c\u4e00\u4e2a\u51fa\u73b0\u76843<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[1, 2, 4, 3, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u53ea\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20<\/strong>\uff1a\u5982\u679c\u5217\u8868\u4e2d\u5b58\u5728\u591a\u4e2a\u76f8\u540c\u7684\u503c\uff0cremove()\u65b9\u6cd5\u53ea\u4f1a\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002<\/li>\n<li><strong>\u5f15\u53d1\u9519\u8bef<\/strong>\uff1a\u5982\u679c\u8981\u5220\u9664\u7684\u5143\u7d20\u4e0d\u5728\u5217\u8868\u4e2d\uff0cremove()\u65b9\u6cd5\u4f1a\u5f15\u53d1ValueError\u9519\u8bef\u3002\u56e0\u6b64\u5728\u4f7f\u7528remove()\u65b9\u6cd5\u4e4b\u524d\uff0c\u53ef\u4ee5\u5148\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5728\u5217\u8868\u4e2d\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">if 3 in my_list:<\/p>\n<p>    my_list.remove(3)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5143\u7d20\u4e0d\u5728\u5217\u8868\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001POP()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><strong>pop()\u65b9\u6cd5<\/strong>\u6839\u636e\u7d22\u5f15\u5220\u9664\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u7d22\u5f15\uff0cpop()\u65b9\u6cd5\u9ed8\u8ba4\u5220\u9664\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>removed_element = my_list.pop(2)  # \u5220\u9664\u7d22\u5f15\u4e3a2\u7684\u5143\u7d20<\/p>\n<p>print(removed_element)  # \u8f93\u51fa\uff1a3<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[1, 2, 4, 5]<\/p>\n<p>last_element = my_list.pop()  # \u4e0d\u6307\u5b9a\u7d22\u5f15\uff0c\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/p>\n<p>print(last_element)  # \u8f93\u51fa\uff1a5<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[1, 2, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u7d22\u5f15\u8303\u56f4<\/strong>\uff1a\u786e\u4fdd\u7d22\u5f15\u5728\u5217\u8868\u7684\u6709\u6548\u8303\u56f4\u5185\uff0c\u5426\u5219\u4f1a\u5f15\u53d1IndexError\u9519\u8bef\u3002<\/li>\n<li><strong>\u8fd4\u56de\u503c<\/strong>\uff1apop()\u65b9\u6cd5\u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\uff0c\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u63a5\u6536\u8fd4\u56de\u503c\u3002<\/li>\n<\/ol>\n<p><h2>\u4e09\u3001DEL\u8bed\u53e5<\/h2>\n<\/p>\n<p><p><strong>del\u8bed\u53e5<\/strong>\u53ef\u4ee5\u6839\u636e\u7d22\u5f15\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u6216\u8005\u5220\u9664\u6574\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>del my_list[2]  # \u5220\u9664\u7d22\u5f15\u4e3a2\u7684\u5143\u7d20<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[1, 2, 4, 5]<\/p>\n<p>del my_list  # \u5220\u9664\u6574\u4e2a\u5217\u8868<\/p>\n<h2><strong>print(my_list)  # \u8fd9\u884c\u4ee3\u7801\u4f1a\u5f15\u53d1NameError\u9519\u8bef\uff0c\u56e0\u4e3amy_list\u5df2\u7ecf\u88ab\u5220\u9664<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5220\u9664\u5143\u7d20<\/strong>\uff1adel\u8bed\u53e5\u53ef\u4ee5\u5220\u9664\u5217\u8868\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\uff0c\u4f46\u4e0d\u80fd\u5220\u9664\u6307\u5b9a\u503c\u7684\u5143\u7d20\u3002<\/li>\n<li><strong>\u5220\u9664\u5217\u8868<\/strong>\uff1adel\u8bed\u53e5\u53ef\u4ee5\u5220\u9664\u6574\u4e2a\u5217\u8868\uff0c\u4f46\u5728\u5220\u9664\u5217\u8868\u540e\uff0c\u4e0d\u80fd\u518d\u8bbf\u95ee\u8be5\u5217\u8868\u3002<\/li>\n<\/ol>\n<p><h2>\u56db\u3001\u5217\u8868\u89e3\u6790<\/h2>\n<\/p>\n<p><p><strong>\u5217\u8868\u89e3\u6790<\/strong>\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u65b0\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u4ece\u800c\u5b9e\u73b0\u5220\u9664\u6307\u5b9a\u5143\u7d20\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 3, 5]<\/p>\n<p>my_list = [x for x in my_list if x != 3]  # \u5220\u9664\u6240\u6709\u503c\u4e3a3\u7684\u5143\u7d20<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u65b0\u5217\u8868<\/strong>\uff1a\u5217\u8868\u89e3\u6790\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u800c\u4e0d\u662f\u5728\u539f\u5217\u8868\u4e0a\u8fdb\u884c\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u6761\u4ef6\u8fc7\u6ee4<\/strong>\uff1a\u53ef\u4ee5\u6839\u636e\u4efb\u610f\u6761\u4ef6\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u7075\u6d3b\u6027\u8f83\u9ad8\u3002<\/li>\n<\/ol>\n<p><h2>\u4e94\u3001\u7efc\u5408\u8fd0\u7528<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u751a\u81f3\u53ef\u4ee5\u7efc\u5408\u8fd0\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u7279\u5b9a\u503c\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u7ed3\u5408remove()\u548c\u5faa\u73af\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 3, 5]<\/p>\n<h2><strong>\u4f7f\u7528while\u5faa\u73af\u548cremove()\u65b9\u6cd5\u5220\u9664\u6240\u6709\u503c\u4e3a3\u7684\u5143\u7d20<\/strong><\/h2>\n<p>while 3 in my_list:<\/p>\n<p>    my_list.remove(3)<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u66f4\u7b80\u6d01\u5730\u5b9e\u73b0\u540c\u6837\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 3, 5]<\/p>\n<p>my_list = [x for x in my_list if x != 3]  # \u5220\u9664\u6240\u6709\u503c\u4e3a3\u7684\u5143\u7d20<\/p>\n<p>print(my_list)  # \u8f93\u51fa\uff1a[1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u5b9e\u73b0\u9ad8\u6548\u3001\u7b80\u6d01\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5217\u8868\u4e2d\u5220\u9664\u7279\u5b9a\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u5220\u9664Python\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002\u5982\u679c\u8be5\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3, 4]<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_list.remove(2)<\/code>\u5c06\u6570\u5b572\u4ece\u5217\u8868\u4e2d\u79fb\u9664\u3002<\/p>\n<p><strong>\u4f7f\u7528\u7d22\u5f15\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u77e5\u9053\u8981\u5220\u9664\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u6216<code>pop()<\/code>\u65b9\u6cd5\u3002<code>del<\/code>\u8bed\u53e5\u53ef\u4ee5\u5220\u9664\u6307\u5b9a\u7d22\u5f15\u7684\u5143\u7d20\uff0c\u800c<code>pop()<\/code>\u65b9\u6cd5\u4e0d\u4ec5\u4f1a\u5220\u9664\u6307\u5b9a\u7d22\u5f15\u7684\u5143\u7d20\uff0c\u8fd8\u4f1a\u8fd4\u56de\u8be5\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>my_list.pop(1)<\/code>\u4f1a\u5220\u9664\u7d22\u5f151\u7684\u5143\u7d20\u5e76\u8fd4\u56de\u5b83\u3002<\/p>\n<p><strong>\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u6709\u6548\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u82e5\u9700\u8981\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u6392\u9664\u6389\u8981\u5220\u9664\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 2, 3, 4]<\/code>\uff0c\u8981\u5220\u9664\u6240\u6709\u76842\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_list = [x for x in my_list if x != 2]<\/code>\u6765\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u7ed3\u679c\u5c06\u662f<code>[1, 3, 4]<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5220\u9664\u5217\u8868\u4e2d\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528remove()\u65b9\u6cd5\u3001\u4f7f\u7528pop()\u65b9\u6cd5\u3001\u4f7f\u7528d [&hellip;]","protected":false},"author":3,"featured_media":1187712,"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\/1187695"}],"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=1187695"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187695\/revisions"}],"predecessor-version":[{"id":1187715,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187695\/revisions\/1187715"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1187712"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1187695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1187695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1187695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}