{"id":1111051,"date":"2025-01-08T17:25:28","date_gmt":"2025-01-08T09:25:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1111051.html"},"modified":"2025-01-08T17:25:31","modified_gmt":"2025-01-08T09:25:31","slug":"python%e5%a6%82%e4%bd%95%e5%8e%bb%e9%99%a4%e5%88%97%e8%a1%a8%e7%9a%84%e4%b8%80%e4%b8%aa%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1111051.html","title":{"rendered":"python\u5982\u4f55\u53bb\u9664\u5217\u8868\u7684\u4e00\u4e2a\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073640\/45dc1c26-b1e2-40f2-bef8-abbe36250f25.webp\" alt=\"python\u5982\u4f55\u53bb\u9664\u5217\u8868\u7684\u4e00\u4e2a\u5143\u7d20\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u53bb\u9664\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\uff0c\u5305\u62ec\u4f7f\u7528remove()\u3001pop()\u3001del\u3001\u5217\u8868\u63a8\u5bfc\u7b49\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528remove()\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6839\u636e\u5143\u7d20\u7684\u503c\u8fdb\u884c\u5220\u9664\u3002remove()\u65b9\u6cd5\u4f1a\u5220\u9664\u5217\u8868\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u6307\u5b9a\u503c\u3002\u5982\u679c\u9700\u8981\u6839\u636e\u7d22\u5f15\u8fdb\u884c\u5220\u9664\uff0c\u53ef\u4ee5\u4f7f\u7528pop()\u6216del\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528 remove() \u65b9\u6cd5\u5220\u9664\u5143\u7d20<\/p>\n<\/p>\n<p><p>remove() \u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5143\u7d20\u7684\u503c\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<strong>\u5b83\u53ea\u4f1a\u5220\u9664\u5217\u8868\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u6307\u5b9a\u503c<\/strong>\uff0c\u5982\u679c\u503c\u4e0d\u5728\u5217\u8868\u4e2d\uff0c\u4f1a\u5f15\u53d1 ValueError \u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list.remove(3)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528 pop() \u65b9\u6cd5\u5220\u9664\u5143\u7d20<\/p>\n<\/p>\n<p><p>pop() \u65b9\u6cd5\u6839\u636e\u7d22\u5f15\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u3002<strong>\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u7d22\u5f15\uff0cpop() \u65b9\u6cd5\u4f1a\u5220\u9664\u5e76\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/strong>\u3002<\/p>\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)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p>print(removed_element)  # \u8f93\u51fa: 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528 del \u8bed\u53e5\u5220\u9664\u5143\u7d20<\/p>\n<\/p>\n<p><p>del \u8bed\u53e5\u53ef\u4ee5\u6839\u636e\u7d22\u5f15\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c<strong>\u5b83\u4e0d\u4f1a\u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20<\/strong>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>del my_list[2]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5220\u9664\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u4e0d\u5305\u62ec\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4f1a\u4fee\u6539\u539f\u59cb\u5217\u8868\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868<\/strong>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>new_list = [x for x in my_list if x != 3]<\/p>\n<p>print(new_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528 filter() \u51fd\u6570\u5220\u9664\u5143\u7d20<\/p>\n<\/p>\n<p><p>filter() \u51fd\u6570\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c<strong>\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61<\/strong>\uff0c\u9700\u8981\u4f7f\u7528 list() \u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>new_list = list(filter(lambda x: x != 3, my_list))<\/p>\n<p>print(new_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u6839\u636e\u6761\u4ef6\u5220\u9664\u591a\u4e2a\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u6839\u636e\u67d0\u4e2a\u6761\u4ef6\u5220\u9664\u591a\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u6216 filter() \u51fd\u6570\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5, 3, 6]<\/p>\n<p>new_list = [x for x in my_list if x != 3]<\/p>\n<p>print(new_list)  # \u8f93\u51fa: [1, 2, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u5220\u9664\u6240\u6709\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u5982\u679c\u60f3\u5220\u9664\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 clear() \u65b9\u6cd5\u6216\u5c06\u5217\u8868\u91cd\u65b0\u8d4b\u503c\u4e3a\u7a7a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list.clear()<\/p>\n<p>print(my_list)  # \u8f93\u51fa: []<\/p>\n<h2><strong>\u6216\u8005<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list = []<\/p>\n<p>print(my_list)  # \u8f93\u51fa: []<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u5220\u9664\u91cd\u590d\u7684\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u4e2d\u6709\u91cd\u590d\u7684\u5143\u7d20\uff0c\u9700\u8981\u5220\u9664\u6240\u6709\u91cd\u590d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u53bb\u91cd\uff0c\u7136\u540e\u518d\u8f6c\u6362\u56de\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5, 3, 6]<\/p>\n<p>my_list = list(set(my_list))<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001\u5220\u9664\u5143\u7d20\u5e76\u4fdd\u7559\u5217\u8868\u987a\u5e8f<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5220\u9664\u91cd\u590d\u7684\u5143\u7d20\u5e76\u4fdd\u7559\u539f\u59cb\u5217\u8868\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u6709\u5e8f\u5b57\u5178\uff08OrderedDict\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import OrderedDict<\/p>\n<p>my_list = [1, 2, 3, 4, 5, 3, 6]<\/p>\n<p>my_list = list(OrderedDict.fromkeys(my_list))<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u3001\u5220\u9664\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u6839\u636e\u590d\u6742\u6761\u4ef6\u5220\u9664\u5143\u7d20\uff0c\u53ef\u4ee5\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u6216 filter() \u51fd\u6570\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5220\u9664\u6240\u6709\u5947\u6570\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>new_list = [x for x in my_list if x % 2 == 0]<\/p>\n<p>print(new_list)  # \u8f93\u51fa: [2, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\u8d77\u6765\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u53ef\u4ee5\u6839\u636e\u5143\u7d20\u7684\u503c\u6216\u7d22\u5f15\u5220\u9664\u5355\u4e2a\u5143\u7d20\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u5220\u9664\u591a\u4e2a\u5143\u7d20\uff0c\u751a\u81f3\u53ef\u4ee5\u5220\u9664\u6240\u6709\u5143\u7d20\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u3002\u65e0\u8bba\u662f\u4f7f\u7528 remove()\u3001pop()\u3001del \u8fd8\u662f\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u901a\u8fc7\u5408\u7406\u5730\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4ece\u5217\u8868\u4e2d\u5220\u9664\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u53ea\u9700\u8c03\u7528\u8be5\u65b9\u6cd5\u5e76\u4f20\u5165\u8981\u5220\u9664\u7684\u5143\u7d20\u503c\uff0c\u5982\u679c\u8be5\u5143\u7d20\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\uff0c\u5b83\u5c06\u88ab\u79fb\u9664\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, 4]\nmy_list.remove(2)\nprint(my_list)  # \u8f93\u51fa: [1, 3, 4]\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c<code>remove()<\/code>\u53ea\u4f1a\u5220\u9664\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u8be5\u5143\u7d20\uff0c\u5982\u679c\u8981\u5220\u9664\u4e0d\u5b58\u5728\u7684\u5143\u7d20\uff0c\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u6839\u636e\u7d22\u5f15\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u8981\u6839\u636e\u7d22\u5f15\u5220\u9664\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pop()<\/code>\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u7d22\u5f15\u53c2\u6570\u5e76\u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u7d22\u5f15\uff0c\u9ed8\u8ba4\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, 4]\nremoved_element = my_list.pop(1)  # \u5220\u9664\u7d22\u5f151\u7684\u5143\u7d20\nprint(my_list)  # \u8f93\u51fa: [1, 3, 4]\nprint(removed_element)  # \u8f93\u51fa: 2\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>pop()<\/code>\u65b9\u6cd5\u65f6\uff0c\u8bf7\u786e\u4fdd\u7d22\u5f15\u5728\u6709\u6548\u8303\u56f4\u5185\uff0c\u8d85\u51fa\u8303\u56f4\u5c06\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u5982\u679c\u60f3\u8981\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u8fc7\u6ee4\u6389\u4e0d\u9700\u8981\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 2, 3, 4]\nmy_list = [x for x in my_list if x != 2]\nprint(my_list)  # \u8f93\u51fa: [1, 3, 4]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9ad8\u6548\u5730\u79fb\u9664\u6240\u6709\u6307\u5b9a\u7684\u5143\u7d20\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u539f\u59cb\u5217\u8868\u4e2d\u7684\u5176\u4ed6\u5143\u7d20\u5c06\u5f97\u4ee5\u4fdd\u7559\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u53bb\u9664\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\uff0c\u5305\u62ec\u4f7f\u7528remove()\u3001pop()\u3001del\u3001\u5217\u8868\u63a8\u5bfc\u7b49\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1111066,"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\/1111051"}],"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=1111051"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111051\/revisions"}],"predecessor-version":[{"id":1111071,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111051\/revisions\/1111071"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1111066"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1111051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1111051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1111051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}