{"id":1167604,"date":"2025-01-15T15:47:16","date_gmt":"2025-01-15T07:47:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1167604.html"},"modified":"2025-01-15T15:47:18","modified_gmt":"2025-01-15T07:47:18","slug":"python%e5%a6%82%e4%bd%95%e4%b8%8d%e5%8c%85%e6%9f%90%e4%ba%9b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1167604.html","title":{"rendered":"Python\u5982\u4f55\u4e0d\u5305\u67d0\u4e9b\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25211454\/5a3c9670-9413-4969-acd5-35fd5e6a806c.webp\" alt=\"Python\u5982\u4f55\u4e0d\u5305\u67d0\u4e9b\u503c\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u4e0d\u5305\u542b\u67d0\u4e9b\u503c\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u5217\u8868\u89e3\u6790\u3001\u8fc7\u6ee4\u5668\u3001\u96c6\u5408\u64cd\u4f5c\u7b49\u3002\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u6570\u636e\u96c6\u4e2d\u6392\u9664\u4e0d\u9700\u8981\u7684\u503c\uff0c\u5982\u5217\u8868\u89e3\u6790\u3001\u96c6\u5408\u64cd\u4f5c\u3001\u8fc7\u6ee4\u5668\u7b49\u3002\u5176\u4e2d\uff0c\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u5e38\u7528\u4e14\u7b80\u6d01\u7684\u65b9\u5f0f\u3002<\/strong> \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6570\u503c\u7684\u5217\u8868\uff0c\u5e76\u4e14\u6211\u4eec\u60f3\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u8be5\u5217\u8868\u4e0d\u5305\u542b\u67d0\u4e9b\u7279\u5b9a\u7684\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u5217\u8868\u89e3\u6790\u901a\u8fc7\u5728\u65b9\u62ec\u53f7\u5185\u7f16\u5199\u4e00\u4e2a\u8868\u8fbe\u5f0f\u548c\u4e00\u4e2afor\u5faa\u73af\u6765\u521b\u5efa\u65b0\u7684\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>values_to_exclude = {3, 7}<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6392\u9664\u67d0\u4e9b\u503c<\/strong><\/h2>\n<p>filtered_list = [x for x in original_list if x not in values_to_exclude]<\/p>\n<p>print(filtered_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5217\u8868 <code>filtered_list<\/code>\uff0c\u5176\u4e2d\u4e0d\u5305\u542b <code>values_to_exclude<\/code> \u4e2d\u7684\u503c\u3002\u901a\u8fc7 <code>if x not in values_to_exclude<\/code> \u8fd9\u4e00\u6761\u4ef6\uff0c\u6211\u4eec\u786e\u4fdd\u4e86\u53ea\u6709\u4e0d\u5728 <code>values_to_exclude<\/code> \u4e2d\u7684\u503c\u624d\u4f1a\u88ab\u6dfb\u52a0\u5230 <code>filtered_list<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5217\u8868\u89e3\u6790<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u9ad8\u6548\u3001\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u8fdb\u884c\u8fc7\u6ee4\u3001\u53d8\u6362\u7b49\u64cd\u4f5c\u3002\u5b83\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u7279\u6027\u4e4b\u4e00\uff0c\u80fd\u591f\u5728\u4e00\u884c\u4ee3\u7801\u5185\u5b8c\u6210\u590d\u6742\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_list = [expression for item in iterable if condition]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>expression<\/code> \u662f\u65b0\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u8868\u8fbe\u5f0f\uff1b<\/li>\n<li><code>item<\/code> \u662f\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff1b<\/li>\n<li><code>iterable<\/code> \u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u6bd4\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u7b49\uff1b<\/li>\n<li><code>condition<\/code> \u662f\u4e00\u4e2a\u53ef\u9009\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u8fc7\u6ee4\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><h3>2\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6570\u503c\u7684\u5217\u8868\uff0c\u5e76\u4e14\u6211\u4eec\u60f3\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u8be5\u5217\u8868\u4e0d\u5305\u542b\u67d0\u4e9b\u7279\u5b9a\u7684\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>values_to_exclude = {3, 7}<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6392\u9664\u67d0\u4e9b\u503c<\/strong><\/h2>\n<p>filtered_list = [x for x in original_list if x not in values_to_exclude]<\/p>\n<p>print(filtered_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5217\u8868 <code>filtered_list<\/code>\uff0c\u5176\u4e2d\u4e0d\u5305\u542b <code>values_to_exclude<\/code> \u4e2d\u7684\u503c\u3002\u901a\u8fc7 <code>if x not in values_to_exclude<\/code> \u8fd9\u4e00\u6761\u4ef6\uff0c\u6211\u4eec\u786e\u4fdd\u4e86\u53ea\u6709\u4e0d\u5728 <code>values_to_exclude<\/code> \u4e2d\u7684\u503c\u624d\u4f1a\u88ab\u6dfb\u52a0\u5230 <code>filtered_list<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u5d4c\u5957\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u4e00\u7ef4\u5217\u8868\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5d4c\u5957\u5217\u8868\uff08\u4e8c\u7ef4\u5217\u8868\u6216\u66f4\u9ad8\u7ef4\u5ea6\u7684\u5217\u8868\uff09\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u89e3\u6790\u6765\u6392\u9664\u67d0\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p>values_to_exclude = {3, 6, 9}<\/p>\n<h2><strong>\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u89e3\u6790\u6392\u9664\u67d0\u4e9b\u503c<\/strong><\/h2>\n<p>filtered_matrix = [[x for x in row if x not in values_to_exclude] for row in matrix]<\/p>\n<p>print(filtered_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u89e3\u6790\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u4e8c\u7ef4\u5217\u8868 <code>filtered_matrix<\/code>\uff0c\u5176\u4e2d\u4e0d\u5305\u542b <code>values_to_exclude<\/code> \u4e2d\u7684\u503c\u3002\u901a\u8fc7\u5728\u5185\u5c42\u5217\u8868\u89e3\u6790\u4e2d\u6dfb\u52a0\u6761\u4ef6 <code>if x not in values_to_exclude<\/code>\uff0c\u6211\u4eec\u786e\u4fdd\u4e86\u53ea\u6709\u4e0d\u5728 <code>values_to_exclude<\/code> \u4e2d\u7684\u503c\u624d\u4f1a\u88ab\u6dfb\u52a0\u5230 <code>filtered_matrix<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u96c6\u5408\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u96c6\u5408\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u548c\u53bb\u91cd\u7684\u573a\u666f\u3002\u5728Python\u4e2d\uff0c\u96c6\u5408\uff08set\uff09\u63d0\u4f9b\u4e86\u591a\u79cd\u64cd\u4f5c\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u4e0d\u5305\u542b\u67d0\u4e9b\u503c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u96c6\u5408<\/p>\n<p>my_set = {1, 2, 3, 4, 5}<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>my_set.add(6)<\/p>\n<h2><strong>\u79fb\u9664\u5143\u7d20<\/strong><\/h2>\n<p>my_set.remove(3)<\/p>\n<h2><strong>\u5224\u65ad\u5143\u7d20\u662f\u5426\u5728\u96c6\u5408\u4e2d<\/strong><\/h2>\n<p>if 4 in my_set:<\/p>\n<p>    print(&quot;4 is in the set&quot;)<\/p>\n<h2><strong>\u96c6\u5408\u8fd0\u7b97<\/strong><\/h2>\n<p>another_set = {4, 5, 6, 7}<\/p>\n<p>union_set = my_set | another_set  # \u5e76\u96c6<\/p>\n<p>intersection_set = my_set &amp; another_set  # \u4ea4\u96c6<\/p>\n<p>difference_set = my_set - another_set  # \u5dee\u96c6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6570\u503c\u7684\u5217\u8868\uff0c\u5e76\u4e14\u6211\u4eec\u60f3\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u8be5\u5217\u8868\u4e0d\u5305\u542b\u67d0\u4e9b\u7279\u5b9a\u7684\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>values_to_exclude = {3, 7}<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u6392\u9664\u67d0\u4e9b\u503c<\/strong><\/h2>\n<p>filtered_set = set(original_list) - values_to_exclude<\/p>\n<p>filtered_list = list(filtered_set)<\/p>\n<p>print(filtered_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06 <code>original_list<\/code> \u8f6c\u6362\u4e3a\u96c6\u5408 <code>filtered_set<\/code>\uff0c\u7136\u540e\u4f7f\u7528\u5dee\u96c6\u64cd\u4f5c <code>-<\/code> \u6392\u9664 <code>values_to_exclude<\/code> \u4e2d\u7684\u503c\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06 <code>filtered_set<\/code> \u8f6c\u6362\u56de\u5217\u8868 <code>filtered_list<\/code>\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u96c6\u5408\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\u4e8e\u5217\u8868\u89e3\u6790\uff0c\u4f46\u751f\u6210\u7684\u662f\u96c6\u5408\u800c\u4e0d\u662f\u5217\u8868\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u6765\u6392\u9664\u67d0\u4e9b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>values_to_exclude = {3, 7}<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u6392\u9664\u67d0\u4e9b\u503c<\/strong><\/h2>\n<p>filtered_set = {x for x in original_list if x not in values_to_exclude}<\/p>\n<p>print(filtered_set)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u96c6\u5408 <code>filtered_set<\/code>\uff0c\u5176\u4e2d\u4e0d\u5305\u542b <code>values_to_exclude<\/code> \u4e2d\u7684\u503c\u3002\u901a\u8fc7 <code>if x not in values_to_exclude<\/code> \u8fd9\u4e00\u6761\u4ef6\uff0c\u6211\u4eec\u786e\u4fdd\u4e86\u53ea\u6709\u4e0d\u5728 <code>values_to_exclude<\/code> \u4e2d\u7684\u503c\u624d\u4f1a\u88ab\u6dfb\u52a0\u5230 <code>filtered_set<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u8fc7\u6ee4\u5668\uff08filter\u51fd\u6570\uff09<\/h2>\n<\/p>\n<p><p><code>filter<\/code> \u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\uff0c\u7528\u4e8e\u8fc7\u6ee4\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u548c\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u6240\u6709\u4f7f\u51fd\u6570\u8fd4\u56de\u503c\u4e3a <code>True<\/code> \u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>filter<\/code> \u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_even(n):<\/p>\n<p>    return n % 2 == 0<\/p>\n<p>numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<h2><strong>\u4f7f\u7528 filter \u51fd\u6570\u8fc7\u6ee4\u5076\u6570<\/strong><\/h2>\n<p>even_numbers = filter(is_even, numbers)<\/p>\n<p>print(list(even_numbers))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570 <code>is_even<\/code> \u6765\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u5076\u6570\uff0c\u7136\u540e\u4f7f\u7528 <code>filter<\/code> \u51fd\u6570\u8fc7\u6ee4\u51fa\u5217\u8868 <code>numbers<\/code> \u4e2d\u7684\u6240\u6709\u5076\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6570\u503c\u7684\u5217\u8868\uff0c\u5e76\u4e14\u6211\u4eec\u60f3\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u8be5\u5217\u8868\u4e0d\u5305\u542b\u67d0\u4e9b\u7279\u5b9a\u7684\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>filter<\/code> \u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def not_in_exclude_list(x):<\/p>\n<p>    return x not in values_to_exclude<\/p>\n<p>original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>values_to_exclude = {3, 7}<\/p>\n<h2><strong>\u4f7f\u7528 filter \u51fd\u6570\u6392\u9664\u67d0\u4e9b\u503c<\/strong><\/h2>\n<p>filtered_list = filter(not_in_exclude_list, original_list)<\/p>\n<p>print(list(filtered_list))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570 <code>not_in_exclude_list<\/code> \u6765\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u4e0d\u5728 <code>values_to_exclude<\/code> \u4e2d\uff0c\u7136\u540e\u4f7f\u7528 <code>filter<\/code> \u51fd\u6570\u8fc7\u6ee4\u51fa\u5217\u8868 <code>original_list<\/code> \u4e2d\u6240\u6709\u4e0d\u5728 <code>values_to_exclude<\/code> \u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u7ed3\u5408 lambda \u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528 <code>lambda<\/code> \u8868\u8fbe\u5f0f\u548c <code>filter<\/code> \u51fd\u6570\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>values_to_exclude = {3, 7}<\/p>\n<h2><strong>\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u548c filter \u51fd\u6570\u6392\u9664\u67d0\u4e9b\u503c<\/strong><\/h2>\n<p>filtered_list = filter(lambda x: x not in values_to_exclude, original_list)<\/p>\n<p>print(list(filtered_list))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>lambda<\/code> \u8868\u8fbe\u5f0f <code>lambda x: x not in values_to_exclude<\/code> \u4f5c\u4e3a <code>filter<\/code> \u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u6765\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u4e0d\u5728 <code>values_to_exclude<\/code> \u4e2d\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u4e0d\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u5355\u72ec\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5b57\u5178\u89e3\u6790<\/h2>\n<\/p>\n<p><p>\u5b57\u5178\u89e3\u6790\u662fPython\u4e2d\u7684\u4e00\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u5904\u7406\u5b57\u5178\u3002\u5b83\u7684\u8bed\u6cd5\u7c7b\u4f3c\u4e8e\u5217\u8868\u89e3\u6790\uff0c\u4f46\u751f\u6210\u7684\u662f\u5b57\u5178\u800c\u4e0d\u662f\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u89e3\u6790\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_dict = {key_expression: value_expression for item in iterable if condition}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>key_expression<\/code> \u662f\u65b0\u5b57\u5178\u4e2d\u6bcf\u4e2a\u952e\u7684\u8868\u8fbe\u5f0f\uff1b<\/li>\n<li><code>value_expression<\/code> \u662f\u65b0\u5b57\u5178\u4e2d\u6bcf\u4e2a\u503c\u7684\u8868\u8fbe\u5f0f\uff1b<\/li>\n<li><code>item<\/code> \u662f\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff1b<\/li>\n<li><code>iterable<\/code> \u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u6bd4\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u7b49\uff1b<\/li>\n<li><code>condition<\/code> \u662f\u4e00\u4e2a\u53ef\u9009\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u8fc7\u6ee4\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><h3>2\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u952e\u503c\u5bf9\u7684\u5b57\u5178\uff0c\u5e76\u4e14\u6211\u4eec\u60f3\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u8be5\u5b57\u5178\u4e0d\u5305\u542b\u67d0\u4e9b\u7279\u5b9a\u7684\u952e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u89e3\u6790\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p>keys_to_exclude = {&#39;b&#39;, &#39;d&#39;}<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u89e3\u6790\u6392\u9664\u67d0\u4e9b\u952e<\/strong><\/h2>\n<p>filtered_dict = {k: v for k, v in original_dict.items() if k not in keys_to_exclude}<\/p>\n<p>print(filtered_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u89e3\u6790\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5b57\u5178 <code>filtered_dict<\/code>\uff0c\u5176\u4e2d\u4e0d\u5305\u542b <code>keys_to_exclude<\/code> \u4e2d\u7684\u952e\u3002\u901a\u8fc7 <code>if k not in keys_to_exclude<\/code> \u8fd9\u4e00\u6761\u4ef6\uff0c\u6211\u4eec\u786e\u4fdd\u4e86\u53ea\u6709\u4e0d\u5728 <code>keys_to_exclude<\/code> \u4e2d\u7684\u952e\u503c\u5bf9\u624d\u4f1a\u88ab\u6dfb\u52a0\u5230 <code>filtered_dict<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u5d4c\u5957\u5b57\u5178\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u89e3\u6790\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u4e00\u7ef4\u5b57\u5178\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5d4c\u5957\u5b57\u5178\uff08\u5b57\u5178\u7684\u503c\u4e5f\u662f\u5b57\u5178\uff09\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5d4c\u5957\u5b57\u5178\u89e3\u6790\u6765\u6392\u9664\u67d0\u4e9b\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_dict = {<\/p>\n<p>    &#39;a&#39;: {&#39;x&#39;: 1, &#39;y&#39;: 2, &#39;z&#39;: 3},<\/p>\n<p>    &#39;b&#39;: {&#39;x&#39;: 4, &#39;y&#39;: 5, &#39;z&#39;: 6},<\/p>\n<p>    &#39;c&#39;: {&#39;x&#39;: 7, &#39;y&#39;: 8, &#39;z&#39;: 9}<\/p>\n<p>}<\/p>\n<p>keys_to_exclude = {&#39;y&#39;, &#39;z&#39;}<\/p>\n<h2><strong>\u4f7f\u7528\u5d4c\u5957\u5b57\u5178\u89e3\u6790\u6392\u9664\u67d0\u4e9b\u952e<\/strong><\/h2>\n<p>filtered_nested_dict = {<\/p>\n<p>    k: {inner_k: inner_v for inner_k, inner_v in v.items() if inner_k not in keys_to_exclude}<\/p>\n<p>    for k, v in nested_dict.items()<\/p>\n<p>}<\/p>\n<p>print(filtered_nested_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5d4c\u5957\u5b57\u5178\u89e3\u6790\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5d4c\u5957\u5b57\u5178 <code>filtered_nested_dict<\/code>\uff0c\u5176\u4e2d\u4e0d\u5305\u542b <code>keys_to_exclude<\/code> \u4e2d\u7684\u952e\u3002\u901a\u8fc7\u5728\u5185\u5c42\u5b57\u5178\u89e3\u6790\u4e2d\u6dfb\u52a0\u6761\u4ef6 <code>if inner_k not in keys_to_exclude<\/code>\uff0c\u6211\u4eec\u786e\u4fdd\u4e86\u53ea\u6709\u4e0d\u5728 <code>keys_to_exclude<\/code> \u4e2d\u7684\u952e\u503c\u5bf9\u624d\u4f1a\u88ab\u6dfb\u52a0\u5230 <code>filtered_nested_dict<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7c7b\u4f3c\u4e8e\u5217\u8868\u89e3\u6790\uff0c\u4f46\u751f\u6210\u7684\u662f\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\u800c\u4e0d\u662f\u5217\u8868\u3002\u751f\u6210\u5668\u5bf9\u8c61\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u9010\u4e2a\u751f\u6210\u5143\u7d20\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u6570\u636e\u96c6\u6216\u9700\u8981\u60f0\u6027\u6c42\u503c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">generator = (expression for item in iterable if condition)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>expression<\/code> \u662f\u751f\u6210\u5668\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u8868\u8fbe\u5f0f\uff1b<\/li>\n<li><code>item<\/code> \u662f\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff1b<\/li>\n<li><code>iterable<\/code> \u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u6bd4\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u7b49\uff1b<\/li>\n<li><code>condition<\/code> \u662f\u4e00\u4e2a\u53ef\u9009\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u8fc7\u6ee4\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><h3>2\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6570\u503c\u7684\u5217\u8868\uff0c\u5e76\u4e14\u6211\u4eec\u60f3\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u751f\u6210\u5668\uff0c\u8be5\u751f\u6210\u5668\u4e0d\u5305\u542b\u67d0\u4e9b\u7279\u5b9a\u7684\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>values_to_exclude = {3, 7}<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6392\u9664\u67d0\u4e9b\u503c<\/strong><\/h2>\n<p>filtered_generator = (x for x in original_list if x not in values_to_exclude)<\/p>\n<p>for value in filtered_generator:<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u751f\u6210\u5668 <code>filtered_generator<\/code>\uff0c\u5176\u4e2d\u4e0d\u5305\u542b <code>values_to_exclude<\/code> \u4e2d\u7684\u503c\u3002\u901a\u8fc7 <code>if x not in values_to_exclude<\/code> \u8fd9\u4e00\u6761\u4ef6\uff0c\u6211\u4eec\u786e\u4fdd\u4e86\u53ea\u6709\u4e0d\u5728 <code>values_to_exclude<\/code> \u4e2d\u7684\u503c\u624d\u4f1a\u88ab\u751f\u6210\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u7ed3\u5408\u51fd\u6570\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u53ef\u4ee5\u4e0e\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u903b\u8f91\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e0e\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def process_values(values):<\/p>\n<p>    for value in values:<\/p>\n<p>        yield value * 2<\/p>\n<p>original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>values_to_exclude = {3, 7}<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6392\u9664\u67d0\u4e9b\u503c\u5e76\u4f20\u9012\u7ed9\u51fd\u6570<\/strong><\/h2>\n<p>filtered_generator = process_values(x for x in original_list if x not in values_to_exclude)<\/p>\n<p>for value in filtered_generator:<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570 <code>process_values<\/code> \u6765\u5904\u7406\u503c\uff0c\u5e76\u5c06\u751f\u6210\u5668\u8868\u8fbe\u5f0f <code>x for x in original_list if x not in values_to_exclude<\/code> \u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u8be5\u51fd\u6570\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6392\u9664\u67d0\u4e9b\u503c\u7684\u540c\u65f6\uff0c\u5bf9\u5269\u4f59\u7684\u503c\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u8fed\u4ee3\u5668\u548c\u751f\u6210\u5668<\/h2>\n<\/p>\n<p><p>\u8fed\u4ee3\u5668\u548c\u751f\u6210\u5668\u662fPython\u4e2d\u5904\u7406\u6570\u636e\u6d41\u7684\u4e24\u79cd\u91cd\u8981\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u9700\u8981\u60f0\u6027\u6c42\u503c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u8fed\u4ee3\u5668<\/h3>\n<\/p>\n<p><p>\u8fed\u4ee3\u5668\u662f\u4e00\u4e2a\u5b9e\u73b0\u4e86\u8fed\u4ee3\u534f\u8bae\u7684\u5bf9\u8c61\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u6309\u9700\u751f\u6210\u6570\u636e\u5143\u7d20\u7684\u65b9\u5f0f\u3002\u8fed\u4ee3\u5668\u901a\u8fc7 <code>__iter__()<\/code> \u65b9\u6cd5\u8fd4\u56de\u81ea\u8eab\uff0c\u5e76\u901a\u8fc7 <code>__next__()<\/code> \u65b9\u6cd5\u9010\u4e2a\u751f\u6210\u5143\u7d20\u3002\u5f53\u6ca1\u6709\u66f4\u591a\u5143\u7d20\u53ef\u751f\u6210\u65f6\uff0c<code>__next__()<\/code> \u65b9\u6cd5\u4f1a\u5f15\u53d1 <code>StopIteration<\/code> \u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyIterator:<\/p>\n<p>    def __init__(self, data):<\/p>\n<p>        self.data = data<\/p>\n<p>        self.index = 0<\/p>\n<p>    def __iter__(self):<\/p>\n<p>        return self<\/p>\n<p>    def __next__(self):<\/p>\n<p>        if self.index &lt; len(self.data):<\/p>\n<p>            result = self.data[self.index]<\/p>\n<p>            self.index += 1<\/p>\n<p>            return result<\/p>\n<p>        else:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se StopIteration<\/p>\n<p>my_iterator = MyIterator([1, 2, 3, 4, 5])<\/p>\n<p>for value in my_iterator:<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8fed\u4ee3\u5668 <code>MyIterator<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5217\u8868 <code>data<\/code> \u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u901a\u8fc7 <code>__next__()<\/code> \u65b9\u6cd5\u9010\u4e2a\u751f\u6210\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4f7f\u7528 <code>yield<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u751f\u6210\u5668\u51fd\u6570\u5728\u6bcf\u6b21\u8c03\u7528 <code>yield<\/code> \u65f6\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u5e76\u5728\u4e0b\u4e00\u6b21\u8fed\u4ee3\u65f6\u4ece\u4e0a\u6b21\u4e2d\u65ad\u7684\u5730\u65b9\u7ee7\u7eed\u6267\u884c\u3002\u751f\u6210\u5668\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_generator(data):<\/p>\n<p>    for item in data:<\/p>\n<p>        yield item<\/p>\n<p>my_gen = my_generator([1, 2, 3, 4, 5])<\/p>\n<p>for value in my_gen:<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6392\u9664\u7279\u5b9a\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u548c\u5217\u8868\u89e3\u6790\u7b49\u6280\u672f\u6765\u6392\u9664\u7279\u5b9a\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868\u5e76\u5e0c\u671b\u6392\u9664\u67d0\u4e9b\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5305\u542b\u6240\u6709\u4e0d\u60f3\u6392\u9664\u7684\u503c\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5]\nexcluded_values = [2, 4]\nfiltered_list = [value for value in original_list if value not in excluded_values]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>filtered_list<\/code>\u5c06\u53ea\u5305\u542b<code>[1, 3, 5]<\/code>\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u5982\u4f55\u6709\u6548\u5730\u6392\u9664\u7279\u5b9a\u503c\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u4f7f\u7528Pandas\u5e93\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u6392\u9664\u7279\u5b9a\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>DataFrame<\/code>\u7684<code>drop<\/code>\u65b9\u6cd5\u6216\u5e03\u5c14\u7d22\u5f15\u6765\u8fc7\u6ee4\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = pd.DataFrame({&#39;A&#39;: [1, 2, 3, 4, 5]})\nfiltered_data = data[~data[&#39;A&#39;].isin([2, 4])]\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684<code>DataFrame<\/code>\uff0c\u4e0d\u5305\u542b\u503c<code>2<\/code>\u548c<code>4<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4e0d\u5305\u67d0\u4e9b\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5b57\u7b26\u4e32\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>replace<\/code>\u65b9\u6cd5\u6216\u8005\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u6392\u9664\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-python\">text = &quot;Hello, this is a sample text.&quot;\nexcluded_string = &quot;sample&quot;\nnew_text = text.replace(excluded_string, &quot;&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>new_text<\/code>\u5c06\u53d8\u4e3a<code>&quot;Hello, this is a  text.&quot;<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u6587\u672c\u4e2d\u79fb\u9664\u4e0d\u9700\u8981\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u4e0d\u5305\u542b\u67d0\u4e9b\u503c\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u5217\u8868\u89e3\u6790\u3001\u8fc7\u6ee4\u5668\u3001\u96c6\u5408\u64cd\u4f5c\u7b49\u3002\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef [&hellip;]","protected":false},"author":3,"featured_media":1167612,"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\/1167604"}],"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=1167604"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167604\/revisions"}],"predecessor-version":[{"id":1167615,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167604\/revisions\/1167615"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1167612"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1167604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1167604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1167604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}