{"id":1100301,"date":"2025-01-08T15:38:39","date_gmt":"2025-01-08T07:38:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100301.html"},"modified":"2025-01-08T15:38:41","modified_gmt":"2025-01-08T07:38:41","slug":"python-list%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e4%b8%80%e4%b8%aa%e5%85%83%e7%b4%a0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1100301.html","title":{"rendered":"python list\u5982\u4f55\u5220\u9664\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\/25063615\/1a242c03-dff8-4a50-9174-065f3d4e7f54.webp\" alt=\"python list\u5982\u4f55\u5220\u9664\u4e00\u4e2a\u5143\u7d20\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5305\u62ec\u4f7f\u7528remove()\u65b9\u6cd5\u3001pop()\u65b9\u6cd5\u3001del\u8bed\u53e5\u7b49\u3002<\/strong> \u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0cremove()\u65b9\u6cd5\u662f\u901a\u8fc7\u503c\u6765\u5220\u9664\u5143\u7d20\u7684\uff0c\u800cpop()\u65b9\u6cd5\u548cdel\u8bed\u53e5\u5219\u662f\u901a\u8fc7\u7d22\u5f15\u6765\u5220\u9664\u5143\u7d20\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u65b9\u5f0f\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528remove()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>remove()\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002\u5b83\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u5143\u7d20\u7684\u503c\u6765\u5220\u9664\uff0c\u800c\u4e0d\u9700\u8981\u77e5\u9053\u5143\u7d20\u7684\u7d22\u5f15\u3002\u7f3a\u70b9\u662f\u5982\u679c\u5217\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u7684\u5143\u7d20\uff0c\u4f1a\u629b\u51faValueError\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>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cremove()\u65b9\u6cd5\u53ea\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002\u5982\u679c\u5217\u8868\u4e2d\u6709\u591a\u4e2a\u76f8\u540c\u7684\u5143\u7d20\uff0c\u5176\u4ed6\u76f8\u540c\u7684\u5143\u7d20\u4e0d\u4f1a\u88ab\u5220\u9664\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528pop()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>pop()\u65b9\u6cd5\u901a\u8fc7\u7d22\u5f15\u6765\u5220\u9664\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\u3002\u5b83\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u5220\u9664\u4efb\u610f\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u53ef\u4ee5\u83b7\u53d6\u88ab\u5220\u9664\u7684\u5143\u7d20\u7684\u503c\u3002\u7f3a\u70b9\u662f\u9700\u8981\u77e5\u9053\u5143\u7d20\u7684\u7d22\u5f15\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>\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><p>\u4e09\u3001\u4f7f\u7528del\u8bed\u53e5<\/p>\n<\/p>\n<p><p>del\u8bed\u53e5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u6216\u5207\u7247\u3002\u5b83\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u5220\u9664\u591a\u4e2a\u5143\u7d20\uff0c\u7f3a\u70b9\u662f\u9700\u8981\u77e5\u9053\u5143\u7d20\u7684\u7d22\u5f15\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>\u5982\u679c\u9700\u8981\u5220\u9664\u591a\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>del my_list[1:3]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\u3002\u5b83\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u540c\u65f6\u5220\u9664\u591a\u4e2a\u5339\u914d\u7684\u5143\u7d20\uff0c\u7f3a\u70b9\u662f\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/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]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528filter()\u51fd\u6570<\/p>\n<\/p>\n<p><p>filter()\u51fd\u6570\u7528\u4e8e\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\u3002\u5b83\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u540c\u65f6\u5220\u9664\u591a\u4e2a\u5339\u914d\u7684\u5143\u7d20\uff0c\u7f3a\u70b9\u662f\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 3, 5]<\/p>\n<p>my_list = list(filter(lambda x: x != 3, my_list))<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u5faa\u73af\u5220\u9664<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5220\u9664\u5217\u8868\u4e2d\u7684\u6240\u6709\u5339\u914d\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u6216for\u5faa\u73af\u3002while\u5faa\u73af\u9002\u7528\u4e8e\u5220\u9664\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\uff0c\u800cfor\u5faa\u73af\u9002\u7528\u4e8e\u5220\u9664\u5355\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 3, 5]<\/p>\n<p>while 3 in my_list:<\/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><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for i in range(len(my_list)):<\/p>\n<p>    if my_list[i] == 3:<\/p>\n<p>        del my_list[i]<\/p>\n<p>        break<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u5220\u9664\u5217\u8868\u4e2d\u7684None\u503c<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5217\u8868\u4e2d\u53ef\u80fd\u5305\u542bNone\u503c\uff0c\u9700\u8981\u5220\u9664\u8fd9\u4e9bNone\u503c\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216filter()\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, None, 2, None, 3, 4, None, 5]<\/p>\n<p>my_list = [x for x in my_list if x is not None]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, None, 2, None, 3, 4, None, 5]<\/p>\n<p>my_list = list(filter(lambda x: x is not None, my_list))<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5220\u9664\u64cd\u4f5c\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u597d\u5730\u64cd\u4f5c\u5217\u8868\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 \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u67e5\u627e\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u5e76\u5c06\u5176\u5220\u9664\u3002\u5982\u679c\u5217\u8868\u4e2d\u6ca1\u6709\u8be5\u5143\u7d20\uff0c\u4f1a\u5f15\u53d1\u4e00\u4e2a<code>ValueError<\/code>\u3002\u4f8b\u5982\uff0c<code>my_list.remove(&#39;\u5143\u7d20&#39;)<\/code>\u5c06\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684&#39;\u5143\u7d20&#39;\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u5220\u9664\u5217\u8868\u4e2d\u67d0\u4e2a\u7279\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u6216\u8005<code>pop()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<code>del my_list[index]<\/code>\u53ef\u4ee5\u5220\u9664\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u800c<code>my_list.pop(index)<\/code>\u4e0d\u4ec5\u4f1a\u5220\u9664\u8be5\u5143\u7d20\uff0c\u8fd8\u4f1a\u8fd4\u56de\u5b83\u7684\u503c\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u7d22\u5f15\uff0c<code>pop()<\/code>\u5c06\u9ed8\u8ba4\u5220\u9664\u5e76\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<p><strong>\u5220\u9664\u5217\u8868\u4e2d\u7684\u6240\u6709\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8981\u5220\u9664\u5217\u8868\u4e2d\u7684\u6240\u6709\u91cd\u590d\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u6570\u636e\u7ed3\u6784\u3002\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u53ef\u4ee5\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u9879\uff0c\u4f8b\u5982\uff1a<code>my_list = list(set(my_list))<\/code>\u3002\u4e0d\u8fc7\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4fdd\u7559\u539f\u5217\u8868\u7684\u987a\u5e8f\u3002\u5982\u679c\u9700\u8981\u4fdd\u6301\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>not in<\/code>\u6765\u624b\u52a8\u8fc7\u6ee4\u91cd\u590d\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5305\u62ec\u4f7f\u7528remove()\u65b9\u6cd5\u3001pop()\u65b9\u6cd5\u3001del\u8bed\u53e5\u7b49\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1100306,"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\/1100301"}],"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=1100301"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100301\/revisions"}],"predecessor-version":[{"id":1100310,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100301\/revisions\/1100310"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100306"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1100301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1100301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1100301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}