{"id":1181467,"date":"2025-01-15T18:51:49","date_gmt":"2025-01-15T10:51:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1181467.html"},"modified":"2025-01-15T18:51:52","modified_gmt":"2025-01-15T10:51:52","slug":"python%e5%88%97%e8%a1%a8%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e5%a4%9a%e4%b8%aa%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1181467.html","title":{"rendered":"python\u5217\u8868\u5982\u4f55\u5220\u9664\u591a\u4e2a\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25125810\/66b4baad-e9d5-4a82-8b56-a41116543c88.webp\" alt=\"python\u5217\u8868\u5982\u4f55\u5220\u9664\u591a\u4e2a\u503c\" \/><\/p>\n<p><p> <strong>Python\u5217\u8868\u5220\u9664\u591a\u4e2a\u503c\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528filter()\u51fd\u6570\u3001\u4f7f\u7528remove()\u65b9\u6cd5\u7ed3\u5408\u5faa\u73af\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u662f\u4e00\u79cd\u8f83\u4e3a\u7b80\u4fbf\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u8be6\u7ec6\u6765\u8bf4\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5728\u5355\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u5bf9\u5217\u8868\u4e2d\u591a\u4e2a\u503c\u7684\u5220\u9664\u64cd\u4f5c\u3002\u901a\u8fc7\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5c06\u4e0d\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\u4fdd\u7559\u4e0b\u6765\uff0c\u4ece\u800c\u5b9e\u73b0\u5220\u9664\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868 <code>lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/code>\uff0c\u6211\u4eec\u5e0c\u671b\u5220\u9664\u5176\u4e2d\u7684 3\u30015 \u548c 7\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>values_to_remove = {3, 5, 7}<\/p>\n<p>lst = [x for x in lst if x not in values_to_remove]<\/p>\n<p>print(lst)  # \u8f93\u51fa [1, 2, 4, 6, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u96c6\u5408 <code>values_to_remove<\/code> \u6765\u5b58\u50a8\u8981\u5220\u9664\u7684\u503c\uff0c\u7136\u540e\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f <code>[x for x in lst if x not in values_to_remove]<\/code> \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u53ea\u5305\u542b\u4e0d\u5728 <code>values_to_remove<\/code> \u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u5220\u9664\u591a\u4e2a\u503c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u901a\u8fc7\u5c06\u4e0d\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\u4fdd\u7559\u4e0b\u6765\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5220\u9664\u7279\u5b9a\u503c\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868 <code>lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/code>\uff0c\u5e0c\u671b\u5220\u9664\u5176\u4e2d\u7684 3\u30015 \u548c 7\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>values_to_remove = {3, 5, 7}<\/p>\n<p>lst = [x for x in lst if x not in values_to_remove]<\/p>\n<p>print(lst)  # \u8f93\u51fa [1, 2, 4, 6, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u4f18\u70b9\u5728\u4e8e\u5176\u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u4e8e\u7406\u89e3\uff0c\u4e14\u6267\u884c\u6548\u7387\u8f83\u9ad8\u3002\u9002\u7528\u4e8e\u5220\u9664\u591a\u4e2a\u503c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001filter()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>filter()<\/code> \u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u901a\u8fc7\u5c06\u4e0d\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\u4fdd\u7559\u4e0b\u6765\uff0c\u5b9e\u73b0\u5220\u9664\u7279\u5b9a\u503c\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868 <code>lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/code>\uff0c\u5e0c\u671b\u5220\u9664\u5176\u4e2d\u7684 3\u30015 \u548c 7\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>filter()<\/code> \u51fd\u6570\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>values_to_remove = {3, 5, 7}<\/p>\n<p>lst = list(filter(lambda x: x not in values_to_remove, lst))<\/p>\n<p>print(lst)  # \u8f93\u51fa [1, 2, 4, 6, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> <code>filter()<\/code> \u51fd\u6570\u7684\u4f18\u70b9\u5728\u4e8e\u5176\u4ee3\u7801\u7b80\u6d01\uff0c\u4e14\u53ef\u4ee5\u4e0e <code>lambda<\/code> \u8868\u8fbe\u5f0f\u7ed3\u5408\u4f7f\u7528\uff0c\u5b9e\u73b0\u7075\u6d3b\u7684\u8fc7\u6ee4\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001remove()\u65b9\u6cd5\u7ed3\u5408\u5faa\u73af<\/h3>\n<\/p>\n<p><p><code>remove()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\u3002\u901a\u8fc7\u7ed3\u5408\u5faa\u73af\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5220\u9664\u591a\u4e2a\u503c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868 <code>lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/code>\uff0c\u5e0c\u671b\u5220\u9664\u5176\u4e2d\u7684 3\u30015 \u548c 7\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>remove()<\/code> \u65b9\u6cd5\u7ed3\u5408\u5faa\u73af\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>values_to_remove = [3, 5, 7]<\/p>\n<p>for value in values_to_remove:<\/p>\n<p>    while value in lst:<\/p>\n<p>        lst.remove(value)<\/p>\n<p>print(lst)  # \u8f93\u51fa [1, 2, 4, 6, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> <code>remove()<\/code> \u65b9\u6cd5\u7ed3\u5408\u5faa\u73af\u7684\u4f18\u70b9\u5728\u4e8e\u5176\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u51fa\u73b0\u7684\u6307\u5b9a\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001set\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u8fdb\u884c\u64cd\u4f5c\uff0c\u5229\u7528\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c\u5b9e\u73b0\u5220\u9664\u591a\u4e2a\u503c\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868 <code>lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/code>\uff0c\u5e0c\u671b\u5220\u9664\u5176\u4e2d\u7684 3\u30015 \u548c 7\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>values_to_remove = {3, 5, 7}<\/p>\n<p>lst = list(set(lst) - values_to_remove)<\/p>\n<p>print(lst)  # \u8f93\u51fa [1, 2, 4, 6, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u96c6\u5408\u64cd\u4f5c\u7684\u4f18\u70b9\u5728\u4e8e\u5176\u4ee3\u7801\u7b80\u6d01\uff0c\u6267\u884c\u6548\u7387\u8f83\u9ad8\uff0c\u9002\u7528\u4e8e\u5143\u7d20\u552f\u4e00\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e0d\u540c\u7684\u65b9\u6cd5\u6709\u5404\u81ea\u7684\u9002\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u4ee5\u4e0b\u662f\u5bf9\u4e0a\u8ff0\u65b9\u6cd5\u7684\u7efc\u5408\u6bd4\u8f83\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u4ee3\u7801\u7b80\u6d01\uff0c\u6267\u884c\u6548\u7387\u8f83\u9ad8\u3002\u63a8\u8350\u4f7f\u7528\u3002<\/li>\n<li><strong>filter()\u51fd\u6570<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u7075\u6d3b\u8fc7\u6ee4\u64cd\u4f5c\u7684\u60c5\u51b5\uff0c\u4ee3\u7801\u7b80\u6d01\uff0c\u4e0e <code>lambda<\/code> \u8868\u8fbe\u5f0f\u7ed3\u5408\u4f7f\u7528\u6548\u679c\u66f4\u4f73\u3002<\/li>\n<li><strong>remove()\u65b9\u6cd5\u7ed3\u5408\u5faa\u73af<\/strong>\uff1a\u9002\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u51fa\u73b0\u7684\u6307\u5b9a\u503c\uff0c\u4ee3\u7801\u76f4\u89c2\uff0c\u4f46\u6267\u884c\u6548\u7387\u8f83\u4f4e\u3002<\/li>\n<li><strong>set\u64cd\u4f5c<\/strong>\uff1a\u9002\u7528\u4e8e\u5143\u7d20\u552f\u4e00\u7684\u5217\u8868\uff0c\u4ee3\u7801\u7b80\u6d01\uff0c\u6267\u884c\u6548\u7387\u8f83\u9ad8\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u6027\u80fd\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u8f83\u5927\u5217\u8868\u65f6\uff0c\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u8868\u73b0\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u4ee5\u4e0b\u662f\u5bf9\u4e0a\u8ff0\u65b9\u6cd5\u7684\u6027\u80fd\u5206\u6790\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u5217\u8868\u63a8\u5bfc\u5f0f\u5728\u5904\u7406\u8f83\u5927\u5217\u8868\u65f6\u6027\u80fd\u8868\u73b0\u8f83\u597d\uff0c\u56e0\u4e3a\u5b83\u53ea\u9700\u8981\u904d\u5386\u5217\u8868\u4e00\u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\u3002<\/li>\n<li><strong>filter()\u51fd\u6570<\/strong>\uff1a<code>filter()<\/code> \u51fd\u6570\u7684\u6027\u80fd\u8868\u73b0\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u56e0\u4e3a\u5b83\u4e5f\u53ea\u9700\u8981\u904d\u5386\u5217\u8868\u4e00\u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\u3002<\/li>\n<li><strong>remove()\u65b9\u6cd5\u7ed3\u5408\u5faa\u73af<\/strong>\uff1a<code>remove()<\/code> \u65b9\u6cd5\u7ed3\u5408\u5faa\u73af\u7684\u6027\u80fd\u8868\u73b0\u8f83\u5dee\uff0c\u56e0\u4e3a\u6bcf\u6b21\u5220\u9664\u64cd\u4f5c\u90fd\u9700\u8981\u904d\u5386\u5217\u8868\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\u3002<\/li>\n<li><strong>set\u64cd\u4f5c<\/strong>\uff1a\u96c6\u5408\u64cd\u4f5c\u5728\u5904\u7406\u8f83\u5927\u5217\u8868\u65f6\u6027\u80fd\u8868\u73b0\u8f83\u597d\uff0c\u56e0\u4e3a\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u4f4e\uff0c\u9002\u7528\u4e8e\u5143\u7d20\u552f\u4e00\u7684\u5217\u8868\u3002<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u4f7f\u7528\u5b9e\u4f8b\uff0c\u5e2e\u52a9\u66f4\u597d\u5730\u7406\u89e3\u4e0a\u8ff0\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>\u5b9e\u4f8b1\uff1a\u5220\u9664\u5217\u8868\u4e2d\u7684\u5076\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>lst = [x for x in lst if x % 2 != 0]<\/p>\n<p>print(lst)  # \u8f93\u51fa [1, 3, 5, 7, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b9e\u4f8b2\uff1a\u5220\u9664\u5217\u8868\u4e2d\u7684\u8d1f\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">lst = [1, -2, 3, -4, 5, -6, 7, -8, 9]<\/p>\n<p>lst = list(filter(lambda x: x &gt;= 0, lst))<\/p>\n<p>print(lst)  # \u8f93\u51fa [1, 3, 5, 7, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b9e\u4f8b3\uff1a\u5220\u9664\u5217\u8868\u4e2d\u6307\u5b9a\u7684\u5b57\u7b26<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">lst = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;]<\/p>\n<p>values_to_remove = [&#39;b&#39;, &#39;d&#39;, &#39;f&#39;]<\/p>\n<p>for value in values_to_remove:<\/p>\n<p>    while value in lst:<\/p>\n<p>        lst.remove(value)<\/p>\n<p>print(lst)  # \u8f93\u51fa [&#39;a&#39;, &#39;c&#39;, &#39;e&#39;, &#39;g&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b9e\u4f8b4\uff1a\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">lst = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]<\/p>\n<p>lst = list(set(lst))<\/p>\n<p>print(lst)  # \u8f93\u51fa [1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u7c7b\u578b<\/strong>\uff1a\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u5143\u7d20\u7c7b\u578b\u4e00\u81f4\uff0c\u4ee5\u514d\u51fa\u73b0\u7c7b\u578b\u9519\u8bef\u3002<\/li>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u5728\u5904\u7406\u8f83\u5927\u5217\u8868\u65f6\uff0c\u9009\u62e9\u6027\u80fd\u8f83\u597d\u7684\u65b9\u6cd5\uff0c\u5982\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u96c6\u5408\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u5143\u7d20\u552f\u4e00\u6027<\/strong>\uff1a\u5728\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u65f6\uff0c\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\uff0c\u5426\u5219\u4f1a\u4e22\u5931\u91cd\u590d\u5143\u7d20\u3002<\/li>\n<\/ol>\n<p><h3>\u4e5d\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython \u5217\u8868\u5220\u9664\u591a\u4e2a\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u9009\u62e9\uff0c\u5305\u62ec\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter()\u51fd\u6570\u3001remove()\u65b9\u6cd5\u7ed3\u5408\u5faa\u73af\u4ee5\u53ca\u96c6\u5408\u64cd\u4f5c\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u548cfilter()\u51fd\u6570\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u4ee3\u7801\u7b80\u6d01\u4e14\u6267\u884c\u6548\u7387\u8f83\u9ad8\uff1bremove()\u65b9\u6cd5\u7ed3\u5408\u5faa\u73af\u9002\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u51fa\u73b0\u7684\u6307\u5b9a\u503c\uff1b\u96c6\u5408\u64cd\u4f5c\u9002\u7528\u4e8e\u5143\u7d20\u552f\u4e00\u7684\u5217\u8868\u3002\u901a\u8fc7\u7efc\u5408\u6bd4\u8f83\u548c\u6027\u80fd\u5206\u6790\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4ecePython\u5217\u8868\u4e2d\u5220\u9664\u7279\u5b9a\u7684\u591a\u4e2a\u503c\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5220\u9664\u591a\u4e2a\u7279\u5b9a\u503c\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u8981\u5220\u9664\u7684\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u8981\u4ece\u5217\u8868\u4e2d\u5220\u9664\u503c2\u548c3\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 2, 5, 3]\nvalues_to_remove = {2, 3}\nfiltered_list = [x for x in original_list if x not in values_to_remove]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>filtered_list<\/code>\u5c06\u53ea\u5305\u542b\u503c1\u30014\u548c5\u3002<\/p>\n<p><strong>\u5728\u5220\u9664\u591a\u4e2a\u503c\u65f6\uff0c\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u662f\u5426\u6709\u6548\uff1f<\/strong><br \/><code>remove()<\/code>\u65b9\u6cd5\u53ea\u80fd\u5220\u9664\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u3002\u5982\u679c\u60a8\u5e0c\u671b\u5220\u9664\u591a\u4e2a\u76f8\u540c\u7684\u503c\uff0c\u60a8\u9700\u8981\u5faa\u73af\u8c03\u7528<code>remove()<\/code>\uff0c\u6216\u8005\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\uff08\u5982\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>filter()<\/code>\u51fd\u6570\uff09\u3002\u4f8b\u5982\uff0c\u60f3\u8981\u4ece\u5217\u8868\u4e2d\u5220\u9664\u6240\u6709\u76842\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\uff1a<\/p>\n<pre><code class=\"language-python\">while 2 in original_list:\n    original_list.remove(2)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u6301\u7eed\u5220\u9664\u5217\u8868\u4e2d\u7684\u6240\u67092\uff0c\u76f4\u5230\u6ca1\u6709\u4e3a\u6b62\u3002<\/p>\n<p><strong>\u5982\u4f55\u5229\u7528NumPy\u5e93\u5220\u9664\u5217\u8868\u4e2d\u7684\u591a\u4e2a\u503c\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u64cd\u4f5c\u65b9\u5f0f\u3002\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6765\u5220\u9664\u4e0d\u9700\u8981\u7684\u503c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\narray = np.array([1, 2, 3, 4, 2, 5, 3])\nvalues_to_remove = [2, 3]\nfiltered_array = array[~np.isin(array, values_to_remove)]\n<\/code><\/pre>\n<p>\u8fd9\u91cc\uff0c<code>filtered_array<\/code>\u5c06\u53ea\u5305\u542b\u503c1\u548c4\u4ee5\u53ca5\uff0c\u5229\u7528NumPy\u7684\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u8fd8\u80fd\u5904\u7406\u66f4\u5927\u7684\u6570\u636e\u96c6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5217\u8868\u5220\u9664\u591a\u4e2a\u503c\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528filter()\u51fd\u6570\u3001\u4f7f\u7528remove()\u65b9\u6cd5\u7ed3\u5408\u5faa\u73af [&hellip;]","protected":false},"author":3,"featured_media":1181473,"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\/1181467"}],"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=1181467"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181467\/revisions"}],"predecessor-version":[{"id":1181474,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181467\/revisions\/1181474"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1181473"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1181467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1181467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1181467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}