{"id":968928,"date":"2024-12-27T05:11:40","date_gmt":"2024-12-26T21:11:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/968928.html"},"modified":"2024-12-27T05:11:42","modified_gmt":"2024-12-26T21:11:42","slug":"python-%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e5%a4%9a%e4%b8%aa%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/968928.html","title":{"rendered":"python \u5982\u4f55\u5220\u9664\u591a\u4e2a\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183441\/3cbd3423-efe2-47a8-a286-155e4b41f4fb.webp\" alt=\"python \u5982\u4f55\u5220\u9664\u591a\u4e2a\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5220\u9664\u591a\u4e2a\u5185\u5bb9\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u901a\u8fc7filter()\u51fd\u6570\u3001\u5229\u7528\u96c6\u5408\u7684\u5dee\u96c6\u8fd0\u7b97<\/strong>\u3002\u5176\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u6700\u4e3a\u5e38\u7528\u4e14\u7075\u6d3b\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u904d\u5386\u5217\u8868\u7684\u540c\u65f6\uff0c\u6839\u636e\u6761\u4ef6\u7b5b\u9009\u51fa\u9700\u8981\u4fdd\u7559\u7684\u5143\u7d20\uff0c\u4ece\u800c\u5b9e\u73b0\u5220\u9664\u6307\u5b9a\u5185\u5bb9\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u4f60\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5bf9\u5217\u8868\u8fdb\u884c\u8fc7\u6ee4\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>data<\/code>\uff0c\u60f3\u8981\u5220\u9664\u5176\u4e2d\u7684\u591a\u4e2a\u7279\u5b9a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5, 6]<\/p>\n<p>to_remove = {2, 4, 6}<\/p>\n<p>filtered_data = [item for item in data if item not in to_remove]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u96c6\u5408<code>to_remove<\/code>\u6765\u5b58\u50a8\u6240\u6709\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\uff0c\u7136\u540e\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u6784\u9020\u4e00\u4e2a\u65b0\u7684\u5217\u8868<code>filtered_data<\/code>\uff0c\u5176\u4e2d\u53ea\u5305\u542b\u4e0d\u5728<code>to_remove<\/code>\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u591a\u4e2a\u5185\u5bb9\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u65b0\u7684\u5217\u8868\u3001\u8fdb\u884c\u5143\u7d20\u7684\u8f6c\u6362\u7b49\u3002\u76f8\u6bd4\u4e8e\u4f20\u7edf\u7684for\u5faa\u73af\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u5177\u6709\u66f4\u9ad8\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>[expression for item in iterable if condition]<\/code>\uff0c\u5176\u4e2d<code>expression<\/code>\u662f\u5bf9\u5143\u7d20\u8fdb\u884c\u7684\u64cd\u4f5c\uff0c<code>item<\/code>\u662f\u4ece<code>iterable<\/code>\u4e2d\u83b7\u53d6\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c<code>condition<\/code>\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u8fc7\u6ee4\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5, 6]<\/p>\n<p>to_remove = [2, 4, 6]<\/p>\n<p>filtered_data = [item for item in data if item not in to_remove]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>filtered_data<\/code>\u5c06\u4f1a\u662f\u4e00\u4e2a\u4e0d\u5305\u542b2, 4, 6\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u9ad8\u7ea7\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u8fd8\u53ef\u4ee5\u5d4c\u5957\u4f7f\u7528\uff0c\u4ee5\u5904\u7406\u591a\u7ef4\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff0c\u9700\u8981\u5220\u9664\u5176\u4e2d\u67d0\u4e9b\u884c\u6216\u5217\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>to_remove = {2, 5, 8}<\/p>\n<p>filtered_matrix = [[item for item in row if item not in to_remove] for row in matrix]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c06\u4ece\u6bcf\u4e00\u884c\u4e2d\u5220\u9664\u6240\u6709\u5728<code>to_remove<\/code>\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528filter()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>filter()<\/code>\u51fd\u6570\u662fPython\u5185\u5efa\u7684\u4e00\u4e2a\u7528\u4e8e\u8fc7\u6ee4\u6570\u636e\u7684\u9ad8\u9636\u51fd\u6570\u3002\u5b83\u901a\u8fc7\u4e00\u4e2a\u51fd\u6570\u5bf9\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5224\u65ad\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u8fed\u4ee3\u5668\uff0c\u53ea\u5305\u542b\u5224\u65ad\u7ed3\u679c\u4e3a\u771f\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>filter()<\/code>\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>filter(function, iterable)<\/code>\uff0c\u5176\u4e2d<code>function<\/code>\u662f\u4e00\u4e2a\u8fd4\u56de\u5e03\u5c14\u503c\u7684\u51fd\u6570\uff0c<code>iterable<\/code>\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5, 6]<\/p>\n<p>to_remove = {2, 4, 6}<\/p>\n<p>filtered_data = filter(lambda item: item not in to_remove, data)<\/p>\n<p>filtered_data = list(filtered_data)  # \u8f6c\u6362\u4e3a\u5217\u8868<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>filter()<\/code>\u51fd\u6570\u901a\u8fc7<code>lambda<\/code>\u51fd\u6570\u5bf9<code>data<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5224\u65ad\uff0c\u4ec5\u4fdd\u7559\u4e0d\u5728<code>to_remove<\/code>\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9664\u4e86<code>lambda<\/code>\u51fd\u6570\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u51fd\u6570\u6765\u8fdb\u884c\u8fc7\u6ee4\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def should_keep(item, items_to_remove):<\/p>\n<p>    return item not in items_to_remove<\/p>\n<p>data = [1, 2, 3, 4, 5, 6]<\/p>\n<p>to_remove = {2, 4, 6}<\/p>\n<p>filtered_data = filter(lambda item: should_keep(item, to_remove), data)<\/p>\n<p>filtered_data = list(filtered_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\uff0c\u5c24\u5176\u662f\u5f53\u8fc7\u6ee4\u6761\u4ef6\u6bd4\u8f83\u590d\u6742\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u96c6\u5408\u7684\u5dee\u96c6\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u96c6\u5408\uff0cPython\u4e2d\u7684\u96c6\u5408\u652f\u6301\u8bb8\u591a\u6570\u5b66\u8fd0\u7b97\uff0c\u6bd4\u5982\u4ea4\u96c6\u3001\u5e76\u96c6\u548c\u5dee\u96c6\u3002\u4f7f\u7528\u5dee\u96c6\u8fd0\u7b97\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5220\u9664\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5dee\u96c6\u8fd0\u7b97\u7b26<code>-<\/code>\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u4ece\u96c6\u5408\u4e2d\u5220\u9664\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {1, 2, 3, 4, 5, 6}<\/p>\n<p>to_remove = {2, 4, 6}<\/p>\n<p>filtered_data = data - to_remove<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>filtered_data<\/code>\u5c06\u662f\u4e00\u4e2a\u96c6\u5408\uff0c\u5305\u542b1, 3, 5\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u7ed3\u5408\u5217\u8868\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u6570\u636e\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u800c\u4e0d\u662f\u96c6\u5408\uff0c\u4f60\u53ef\u4ee5\u5148\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u8fdb\u884c\u5dee\u96c6\u8fd0\u7b97\u540e\u518d\u8f6c\u6362\u56de\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5, 6]<\/p>\n<p>to_remove = {2, 4, 6}<\/p>\n<p>filtered_data = list(set(data) - to_remove)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u5220\u9664\u5927\u91cf\u91cd\u590d\u5143\u7d20\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u591a\u4e2a\u5185\u5bb9\uff0c\u6bd4\u5982\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3001\u4f7f\u7528<code>numpy<\/code>\u5e93\u8fdb\u884c\u6570\u7ec4\u64cd\u4f5c\u7b49\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528map()\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>map()<\/code>\u51fd\u6570\u53ef\u7528\u4e8e\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u5230\u5e8f\u5217\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u867d\u7136<code>map()<\/code>\u672c\u8eab\u4e0d\u7528\u4e8e\u5220\u9664\u5143\u7d20\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5, 6]<\/p>\n<p>to_remove = {2, 4, 6}<\/p>\n<p>filtered_data = map(lambda x: x if x not in to_remove else None, data)<\/p>\n<p>filtered_data = list(filter(None, filtered_data))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>map()<\/code>\u5c06\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\u66ff\u6362\u4e3a<code>None<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>filter()<\/code>\u53bb\u9664<code>None<\/code>\u503c\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528numpy\u5e93<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\u6216\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6570\u503c\u8fd0\u7b97\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u3002<code>numpy<\/code>\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<p>to_remove = np.array([2, 4, 6])<\/p>\n<p>filtered_data = np.setdiff1d(data, to_remove)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>filtered_data<\/code>\u5c06\u662f\u4e00\u4e2a<code>numpy<\/code>\u6570\u7ec4\uff0c\u5305\u542b1, 3, 5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u5220\u9664\u591a\u4e2a\u5185\u5bb9\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u5e38\u89c1\u7684\u9677\u9631\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u6ce8\u610f\u539f\u59cb\u6570\u636e\u4e0d\u53d8\u6027<\/h4>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5927\u591a\u6570\u6570\u636e\u7ed3\u6784\uff08\u5982\u5217\u8868\u3001\u96c6\u5408\uff09\u662f\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u5728\u6267\u884c\u5220\u9664\u64cd\u4f5c\u65f6\uff0c\u539f\u59cb\u6570\u636e\u53ef\u80fd\u4f1a\u88ab\u4fee\u6539\u3002\u5982\u679c\u4f60\u9700\u8981\u4fdd\u7559\u539f\u59cb\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u521b\u5efa\u6570\u636e\u7684\u526f\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5, 6]<\/p>\n<p>to_remove = {2, 4, 6}<\/p>\n<h2><strong>\u521b\u5efa\u526f\u672c<\/strong><\/h2>\n<p>data_copy = data[:]<\/p>\n<p>filtered_data = [item for item in data_copy if item not in to_remove]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u6027\u80fd\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0c\u9009\u62e9\u5408\u9002\u7684\u5220\u9664\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u96c6\u5408\u7684\u5dee\u96c6\u8fd0\u7b97\u901a\u5e38\u6bd4\u5217\u8868\u63a8\u5bfc\u5f0f\u66f4\u5feb\uff0c\u7279\u522b\u662f\u5f53\u8981\u5220\u9664\u7684\u5143\u7d20\u5f88\u591a\u65f6\u3002<\/p>\n<\/p>\n<p><h4>5.3 \u907f\u514d\u5d4c\u5957\u8fc7\u6df1<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5d4c\u5957\u51fd\u6570\u65f6\uff0c\u6ce8\u610f\u4fdd\u6301\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u8fc7\u4e8e\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\u53ef\u80fd\u4f1a\u964d\u4f4e\u4ee3\u7801\u7684\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u5220\u9664\u591a\u4e2a\u5185\u5bb9\u3002\u65e0\u8bba\u662f\u5217\u8868\u63a8\u5bfc\u5f0f\u3001<code>filter()<\/code>\u51fd\u6570\uff0c\u8fd8\u662f\u96c6\u5408\u8fd0\u7b97\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u52bf\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7f16\u5199\u51fa\u7b80\u6d01\u3001\u9ad8\u6548\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664\u5217\u8868\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\u6765\u5220\u9664\u591a\u4e2a\u5143\u7d20\u3002\u6bd4\u5982\uff0c\u5982\u679c\u8981\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u5220\u9664\u6240\u6709\u503c\u4e3a\u7279\u5b9a\u6570\u5b57\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u53ea\u5305\u542b\u90a3\u4e9b\u4e0d\u7b49\u4e8e\u8be5\u6570\u5b57\u7684\u5143\u7d20\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 2, 5]\nvalue_to_remove = 2\nfiltered_list = [item for item in original_list if item != value_to_remove]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>filtered_list<\/code>\u5c06\u53ea\u5305\u542b\u4e0d\u7b49\u4e8e2\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u5728\u5b57\u5178\u4e2d\u5982\u4f55\u5220\u9664\u591a\u4e2a\u952e\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u7ed3\u5408\u5faa\u73af\u6765\u5220\u9664\u591a\u4e2a\u952e\u3002\u5982\u679c\u9700\u8981\u5220\u9664\u7684\u952e\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u53ef\u4ee5\u904d\u5386\u8be5\u5217\u8868\u5e76\u9010\u4e00\u5220\u9664\u5bf9\u5e94\u7684\u952e\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">original_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}\nkeys_to_remove = [&#39;a&#39;, &#39;c&#39;]\nfor key in keys_to_remove:\n    if key in original_dict:\n        del original_dict[key]\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c<code>original_dict<\/code>\u5c06\u53ea\u5269\u4e0b\u672a\u88ab\u5220\u9664\u7684\u952e\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Python\u7684\u8fc7\u6ee4\u51fd\u6570\u6765\u5220\u9664\u591a\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528<code>filter()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u5220\u9664\u591a\u4e2a\u5143\u7d20\u7684\u529f\u80fd\u3002\u901a\u8fc7\u4f20\u5165\u4e00\u4e2a\u8fc7\u6ee4\u51fd\u6570\u548c\u5f85\u5904\u7406\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u53ea\u4fdd\u7559\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5]\nvalues_to_remove = {2, 4}\nfiltered_list = list(filter(lambda x: x not in values_to_remove, original_list))\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>filtered_list<\/code>\u5c06\u4e0d\u5305\u542b2\u548c4\uff0c\u53ea\u67091\u30013\u548c5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5220\u9664\u591a\u4e2a\u5185\u5bb9\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u901a\u8fc7filter()\u51fd\u6570\u3001\u5229\u7528\u96c6\u5408\u7684\u5dee\u96c6\u8fd0\u7b97\u3002\u5176\u4e2d\uff0c\u5217\u8868 [&hellip;]","protected":false},"author":3,"featured_media":968934,"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\/968928"}],"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=968928"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968928\/revisions"}],"predecessor-version":[{"id":968936,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968928\/revisions\/968936"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/968934"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=968928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=968928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=968928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}