{"id":1109990,"date":"2025-01-08T17:15:27","date_gmt":"2025-01-08T09:15:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1109990.html"},"modified":"2025-01-08T17:15:30","modified_gmt":"2025-01-08T09:15:30","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e5%88%97%e8%a1%a8%e4%b8%ad%e7%9a%84%e6%9f%90%e4%b8%aa%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1109990.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5220\u9664\u5217\u8868\u4e2d\u7684\u67d0\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\/25073023\/765bf9b0-f055-41ff-80d6-492daa1f38a7.webp\" alt=\"python\u4e2d\u5982\u4f55\u5220\u9664\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5220\u9664\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\uff0c\u5305\u62ec\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u3001<code>del<\/code>\u8bed\u53e5\u3001<code>pop()<\/code>\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u3002<code>remove()<\/code>\u65b9\u6cd5\u4f1a\u4ece\u5217\u8868\u4e2d\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p><strong>remove()\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<p><code>remove()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002\u5982\u679c\u5217\u8868\u4e2d\u5b58\u5728\u591a\u4e2a\u76f8\u540c\u7684\u5143\u7d20\uff0c<code>remove()<\/code>\u65b9\u6cd5\u53ea\u4f1a\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u4f20\u9012\u8981\u5220\u9664\u7684\u5143\u7d20\u4f5c\u4e3a\u53c2\u6570\u3002\u5982\u679c\u8be5\u5143\u7d20\u5728\u5217\u8868\u4e2d\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>my_list.remove(2)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 3, 4, 2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001REMOVE()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>remove()<\/code>\u65b9\u6cd5\u662fPython\u5217\u8868\u64cd\u4f5c\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\u3002\u5b83\u4ec5\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\uff0c\u4ee5\u4e0b\u662f\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>remove()<\/code>\u65b9\u6cd5\u9700\u8981\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u8981\u5220\u9664\u7684\u5143\u7d20\u3002\u5982\u679c\u8be5\u5143\u7d20\u5728\u5217\u8868\u4e2d\u5b58\u5728\uff0c<code>remove()<\/code>\u65b9\u6cd5\u5c06\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\uff1b\u5982\u679c\u8be5\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>my_list.remove(2)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 3, 4, 2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>remove()<\/code>\u65b9\u6cd5\u5220\u9664\u4e86\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u5143\u7d20<code>2<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u8981\u5220\u9664\u7684\u5143\u7d20\u5728\u5217\u8868\u4e2d\u4e0d\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u5728\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u65f6\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>try:<\/p>\n<p>    my_list.remove(6)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u8981\u5220\u9664\u7684\u5143\u7d20\u4e0d\u5b58\u5728\u4e8e\u5217\u8868\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u8981\u5220\u9664\u7684\u5143\u7d20<code>6<\/code>\u5728\u5217\u8868\u4e2d\u4e0d\u5b58\u5728\uff0c\u7a0b\u5e8f\u4f1a\u6355\u83b7<code>ValueError<\/code>\u5f02\u5e38\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001DEL\u8bed\u53e5<\/h3>\n<\/p>\n<p><p><code>del<\/code>\u8bed\u53e5\u662fPython\u4e2d\u7528\u4e8e\u5220\u9664\u5bf9\u8c61\u7684\u8bed\u53e5\u3002\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u5220\u9664\u5217\u8868\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\uff0c\u4ee5\u4e0b\u662f\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u7684\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>del<\/code>\u8bed\u53e5\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u8981\u5220\u9664\u7684\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>del my_list[1]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 3, 4, 2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>del<\/code>\u8bed\u53e5\u5220\u9664\u4e86\u5217\u8868\u4e2d\u7d22\u5f15\u4e3a<code>1<\/code>\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5220\u9664\u591a\u4e2a\u5143\u7d20<\/h4>\n<\/p>\n<p><p><code>del<\/code>\u8bed\u53e5\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\u3002\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u8bed\u6cd5\u6307\u5b9a\u8981\u5220\u9664\u7684\u5143\u7d20\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>del my_list[1:3]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 4, 2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>del<\/code>\u8bed\u53e5\u5220\u9664\u4e86\u5217\u8868\u4e2d\u7d22\u5f15\u8303\u56f4\u4e3a<code>1<\/code>\u5230<code>3<\/code>\u4e4b\u95f4\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001POP()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>pop()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5e76\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\u3002<code>pop()<\/code>\u65b9\u6cd5\u9700\u8981\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u8981\u5220\u9664\u7684\u5143\u7d20\u7684\u7d22\u5f15\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u7d22\u5f15\uff0c<code>pop()<\/code>\u65b9\u6cd5\u5c06\u5220\u9664\u5e76\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>removed_element = my_list.pop(1)<\/p>\n<p>print(my_list)           # \u8f93\u51fa: [1, 3, 4, 2, 5]<\/p>\n<p>print(removed_element)   # \u8f93\u51fa: 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>pop()<\/code>\u65b9\u6cd5\u5220\u9664\u5e76\u8fd4\u56de\u4e86\u5217\u8868\u4e2d\u7d22\u5f15\u4e3a<code>1<\/code>\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4e0d\u6307\u5b9a\u7d22\u5f15\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>removed_element = my_list.pop()<\/p>\n<p>print(my_list)           # \u8f93\u51fa: [1, 2, 3, 4, 2]<\/p>\n<p>print(removed_element)   # \u8f93\u51fa: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>pop()<\/code>\u65b9\u6cd5\u5220\u9664\u5e76\u8fd4\u56de\u4e86\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u5217\u8868\u751f\u6210\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6761\u4ef6\u8bed\u53e5\u8fc7\u6ee4\u6389\u8981\u5220\u9664\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>my_list = [x for x in my_list if x != 2]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u5143\u7d20<code>2<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5220\u9664\u591a\u4e2a\u4e0d\u540c\u7684\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u6761\u4ef6\u8bed\u53e5\u8fc7\u6ee4\u6389\u591a\u4e2a\u4e0d\u540c\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>my_list = [x for x in my_list if x not in (2, 4)]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 3, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u5143\u7d20<code>2<\/code>\u548c<code>4<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001FILTER()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>filter()<\/code>\u51fd\u6570\u7528\u4e8e\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\u3002\u4f7f\u7528<code>filter()<\/code>\u51fd\u6570\u65f6\uff0c\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u8fc7\u6ee4\u51fd\u6570\u548c\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>my_list = list(filter(lambda x: x != 2, my_list))<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>filter()<\/code>\u51fd\u6570\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u5143\u7d20<code>2<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5220\u9664\u591a\u4e2a\u4e0d\u540c\u7684\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u8fc7\u6ee4\u51fd\u6570\uff0c\u7528\u4e8e\u8fc7\u6ee4\u6389\u591a\u4e2a\u4e0d\u540c\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>my_list = list(filter(lambda x: x not in (2, 4), my_list))<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 3, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>filter()<\/code>\u51fd\u6570\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u5143\u7d20<code>2<\/code>\u548c<code>4<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001Numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5904\u7406\u7684\u662f\u6570\u503c\u578b\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528Numpy\u5e93\u7684\u6570\u7ec4\u64cd\u4f5c\u6765\u5220\u9664\u5217\u8868\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\u3002Numpy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u7684\u6570\u503c\u578b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import numpy as np<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>my_array = np.array(my_list)<\/p>\n<p>my_array = my_array[my_array != 2]<\/p>\n<p>print(my_array)  # \u8f93\u51fa: [1 3 4 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0cNumpy\u6570\u7ec4\u64cd\u4f5c\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u5143\u7d20<code>2<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5220\u9664\u591a\u4e2a\u4e0d\u540c\u7684\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Numpy\u6570\u7ec4\u64cd\u4f5c\u8fc7\u6ee4\u6389\u591a\u4e2a\u4e0d\u540c\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import numpy as np<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>my_array = np.array(my_list)<\/p>\n<p>my_array = my_array[~np.isin(my_array, [2, 4])]<\/p>\n<p>print(my_array)  # \u8f93\u51fa: [1 3 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0cNumpy\u6570\u7ec4\u64cd\u4f5c\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u5143\u7d20<code>2<\/code>\u548c<code>4<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u96c6\u5408\uff08Set\uff09<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\uff0c\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u5220\u9664\u6307\u5b9a\u7684\u5143\u7d20\u540e\uff0c\u518d\u5c06\u96c6\u5408\u8f6c\u6362\u56de\u5217\u8868\u3002\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\uff0c\u56e0\u6b64\u5728\u96c6\u5408\u4e2d\u5220\u9664\u5143\u7d20\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>my_set = set(my_list)<\/p>\n<p>my_set.discard(2)<\/p>\n<p>my_list = list(my_set)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u540e\uff0c\u4f7f\u7528<code>discard()<\/code>\u65b9\u6cd5\u5220\u9664\u4e86\u5143\u7d20<code>2<\/code>\uff0c\u518d\u5c06\u96c6\u5408\u8f6c\u6362\u56de\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5220\u9664\u591a\u4e2a\u4e0d\u540c\u7684\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u5220\u9664\u591a\u4e2a\u4e0d\u540c\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5]<\/p>\n<p>my_set = set(my_list)<\/p>\n<p>my_set.difference_update([2, 4])<\/p>\n<p>my_list = list(my_set)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 3, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\u7684<code>difference_update()<\/code>\u65b9\u6cd5\u5220\u9664\u4e86\u591a\u4e2a\u4e0d\u540c\u7684\u5143\u7d20<code>2<\/code>\u548c<code>4<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u9012\u5f52\u5220\u9664<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u5d4c\u5957\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u5220\u9664\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\u3002\u9012\u5f52\u65b9\u6cd5\u53ef\u4ee5\u904d\u5386\u5d4c\u5957\u7684\u5217\u8868\u7ed3\u6784\uff0c\u5e76\u5220\u9664\u6307\u5b9a\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>def recursive_remove(lst, val):<\/p>\n<p>    for i in range(len(lst)):<\/p>\n<p>        if isinstance(lst[i], list):<\/p>\n<p>            recursive_remove(lst[i], val)<\/p>\n<p>        elif lst[i] == val:<\/p>\n<p>            lst.pop(i)<\/p>\n<p>            return<\/p>\n<p>my_list = [1, 2, [3, 4, [2, 5]], 2, 5]<\/p>\n<p>recursive_remove(my_list, 2)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, [3, 4, [2, 5]], 2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u9012\u5f52\u65b9\u6cd5\u5220\u9664\u4e86\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20<code>2<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5220\u9664\u6240\u6709\u5339\u914d\u7684\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4fee\u6539\u9012\u5f52\u65b9\u6cd5\uff0c\u5220\u9664\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>def recursive_remove_all(lst, val):<\/p>\n<p>    i = 0<\/p>\n<p>    while i &lt; len(lst):<\/p>\n<p>        if isinstance(lst[i], list):<\/p>\n<p>            recursive_remove_all(lst[i], val)<\/p>\n<p>        elif lst[i] == val:<\/p>\n<p>            lst.pop(i)<\/p>\n<p>            continue<\/p>\n<p>        i += 1<\/p>\n<p>my_list = [1, 2, [3, 4, [2, 5]], 2, 5]<\/p>\n<p>recursive_remove_all(my_list, 2)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, [3, 4, [5]], 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u9012\u5f52\u65b9\u6cd5\u5220\u9664\u4e86\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u6240\u6709\u5339\u914d\u7684\u5143\u7d20<code>2<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5220\u9664\u5217\u8868\u4e2d\u91cd\u590d\u7684\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u9700\u8981\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u7684\u91cd\u590d\u5143\u7d20\uff0c\u53ea\u4fdd\u7559\u552f\u4e00\u7684\u5143\u7d20\u3002\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u6216\u8005\u5b57\u5178\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u96c6\u5408\u5220\u9664\u91cd\u590d\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u6765\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5, 3]<\/p>\n<p>my_list = list(set(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>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\u5220\u9664\u4e86\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4fdd\u6301\u5143\u7d20\u987a\u5e8f\u7684\u5220\u9664\u91cd\u590d\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u5220\u9664\u91cd\u590d\u5143\u7d20\uff0c\u5e76\u4fdd\u6301\u5143\u7d20\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 2, 5, 3]<\/p>\n<p>my_list = list(dict.fromkeys(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>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u5b57\u5178\u5220\u9664\u4e86\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff0c\u5e76\u4fdd\u6301\u4e86\u5143\u7d20\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5220\u9664\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\uff0c\u5305\u62ec<code>remove()<\/code>\u65b9\u6cd5\u3001<code>del<\/code>\u8bed\u53e5\u3001<code>pop()<\/code>\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001<code>filter()<\/code>\u51fd\u6570\u3001Numpy\u5e93\u3001\u96c6\u5408\u3001\u9012\u5f52\u65b9\u6cd5\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u5176\u4e2d\uff0c<code>remove()<\/code>\u65b9\u6cd5\u6700\u4e3a\u5e38\u7528\uff0c\u7528\u4e8e\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\uff1b<code>del<\/code>\u8bed\u53e5\u548c<code>pop()<\/code>\u65b9\u6cd5\u7528\u4e8e\u901a\u8fc7\u7d22\u5f15\u5220\u9664\u5143\u7d20\uff1b\u5217\u8868\u63a8\u5bfc\u5f0f\u548c<code>filter()<\/code>\u51fd\u6570\u7528\u4e8e\u751f\u6210\u65b0\u7684\u5217\u8868\uff0c\u5220\u9664\u6307\u5b9a\u5143\u7d20\uff1bNumpy\u5e93\u7528\u4e8e\u5904\u7406\u6570\u503c\u578b\u5217\u8868\uff1b\u96c6\u5408\u7528\u4e8e\u5220\u9664\u552f\u4e00\u5143\u7d20\uff1b\u9012\u5f52\u65b9\u6cd5\u7528\u4e8e\u5904\u7406\u5d4c\u5957\u5217\u8868\u3002<\/strong> \u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5220\u9664\u5217\u8868\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u5168\u5730\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b89\u5168\u5730\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u6216<code>del<\/code>\u8bed\u53e5\u6765\u5b9e\u73b0\u3002<code>remove()<\/code>\u65b9\u6cd5\u4f1a\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\uff0c\u800c<code>del<\/code>\u8bed\u53e5\u53ef\u4ee5\u6839\u636e\u7d22\u5f15\u5220\u9664\u6307\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\uff0c\u4ee5\u907f\u514d\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<p><strong>\u4f7f\u7528\u7d22\u5f15\u5220\u9664\u5143\u7d20\u65f6\uff0c\u6709\u54ea\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u7d22\u5f15\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u65f6\uff0c\u786e\u4fdd\u7d22\u5f15\u5728\u5217\u8868\u7684\u6709\u6548\u8303\u56f4\u5185\u3002\u5982\u679c\u5c1d\u8bd5\u5220\u9664\u4e00\u4e2a\u8d85\u51fa\u8303\u56f4\u7684\u7d22\u5f15\uff0c\u4f1a\u5f15\u53d1<code>IndexError<\/code>\u3002\u5728\u5220\u9664\u4e4b\u524d\uff0c\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5<code>len()<\/code>\u51fd\u6570\u6765\u786e\u8ba4\u5217\u8868\u7684\u957f\u5ea6\uff0c\u786e\u4fdd\u7d22\u5f15\u6709\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5220\u9664\u5143\u7d20\u540e\u4fdd\u6301\u5217\u8868\u7684\u987a\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5220\u9664\u5143\u7d20\u540e\uff0c\u5217\u8868\u7684\u987a\u5e8f\u4f1a\u81ea\u52a8\u4fdd\u6301\u4e0d\u53d8\u3002\u5982\u679c\u4f60\u4f7f\u7528<code>remove()<\/code>\u6216\u8005<code>del<\/code>\u8bed\u53e5\u6765\u5220\u9664\u5143\u7d20\uff0c\u5269\u4f59\u7684\u5143\u7d20\u4f1a\u81ea\u52a8\u5411\u5de6\u79fb\u52a8\u4ee5\u586b\u8865\u7a7a\u7f3a\u3002\u56e0\u6b64\uff0c\u5220\u9664\u5143\u7d20\u4e0d\u4f1a\u5f71\u54cd\u5217\u8868\u4e2d\u5176\u4ed6\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5220\u9664\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\uff0c\u5305\u62ec\u4f7f\u7528remove()\u65b9\u6cd5\u3001del\u8bed\u53e5\u3001pop()\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1109997,"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\/1109990"}],"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=1109990"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109990\/revisions"}],"predecessor-version":[{"id":1109998,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109990\/revisions\/1109998"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1109997"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1109990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1109990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1109990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}