{"id":174844,"date":"2024-05-08T18:38:57","date_gmt":"2024-05-08T10:38:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/174844.html"},"modified":"2024-05-08T18:39:02","modified_gmt":"2024-05-08T10:39:02","slug":"python%e5%a6%82%e4%bd%95%e4%bb%8e%e4%b8%80%e4%b8%aa%e5%88%97%e8%a1%a8%e5%88%a0%e9%99%a4%e5%8f%a6%e4%b8%80%e4%b8%aa%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/174844.html","title":{"rendered":"python\u5982\u4f55\u4ece\u4e00\u4e2a\u5217\u8868\u5220\u9664\u53e6\u4e00\u4e2a\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27052526\/fe0fbaf5-b9f5-44d3-bb8e-7e1d38c03ade.webp\" alt=\"python\u5982\u4f55\u4ece\u4e00\u4e2a\u5217\u8868\u5220\u9664\u53e6\u4e00\u4e2a\u5217\u8868\" \/><\/p>\n<p><p>\u8981\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u5220\u9664\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\u5b58\u5728\u7684\u6240\u6709\u5143\u7d20\uff0c\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u6709 <strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5229\u7528<code>filter()<\/code>\u51fd\u6570\u3001\u501f\u52a9\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c<\/strong>\u3002<strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong> \u662f\u6700\u4e3a\u76f4\u89c2\u548c\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u8be5\u5217\u8868\u4ec5\u5305\u542b\u4e0d\u5728\u7b2c\u4e8c\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e24\u4e2a\u5217\u8868\uff0c<code>list1<\/code>\u548c<code>list2<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>[x for x in list1 if x not in list2]<\/code>\u5feb\u901f\u5f97\u5230\u4e00\u4e2a\u4ec5\u5305\u542b<code>list1<\/code>\u4e2d\u72ec\u6709\u5143\u7d20\u7684\u65b0\u5217\u8868\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\u662fPython\u4e2d\u5904\u7406\u5217\u8868\u76f8\u5173\u95ee\u9898\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u8fc7\u6ee4\u51fa\u4e0d\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002\u5b83\u4e0d\u4ec5\u4ee3\u7801\u7b80\u6d01\uff0c\u800c\u4e14\u6267\u884c\u6548\u7387\u76f8\u5bf9\u8f83\u9ad8\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [4, 5, 6, 7, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8981\u79fb\u9664<code>list1<\/code>\u4e2d\u6240\u6709\u51fa\u73b0\u5728<code>list2<\/code>\u4e2d\u7684\u5143\u7d20\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result_list = [item for item in list1 if item not in list2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u80cc\u540e\u7684\u903b\u8f91\u662f\u904d\u5386<code>list1<\/code>\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u4ec5\u5f53\u8be5\u5143\u7d20\u4e0d\u5728<code>list2<\/code>\u4e2d\u65f6\u624d\u5c06\u5176\u6dfb\u52a0\u5230<code>result_list<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528<code>FILTER()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>filter()<\/code>\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u51fd\u6570\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u8fc7\u6ee4\uff0c\u8fd4\u56de\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u7ec4\u6210\u7684\u65b0\u8fed\u4ee3\u5668\u3002\u7ed3\u5408\u533f\u540d\u51fd\u6570<code>lambda<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u4ece\u5217\u8868\u4e2d\u5220\u9664\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u9700\u6c42\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [4, 5, 6, 7, 8]<\/p>\n<p>filtered_list = list(filter(lambda x: x not in list2, list1))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u76f8\u8f83\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u66f4\u52a0\u51fd\u6570\u5f0f\u7f16\u7a0b\u98ce\u683c\uff0c<code>filter()<\/code>\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u56e0\u6b64\u5982\u679c\u9700\u8981\u5217\u8868\u5f62\u5f0f\uff0c\u8bb0\u5f97\u7528<code>list()<\/code>\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u501f\u52a9\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\uff08Set\uff09\u662fPython\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u5185\u90e8\u5143\u7d20\u662f\u552f\u4e00\u7684\uff0c\u65e0\u5e8f\u7684\u3002\u5229\u7528\u8fd9\u4e2a\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5217\u8868\u8f6c\u5316\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u4f7f\u7528\u5dee\u96c6\u64cd\u4f5c\u79fb\u9664\u91cd\u590d\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [4, 5, 6, 7, 8]<\/p>\n<p>set1 = set(list1)<\/p>\n<p>set2 = set(list2)<\/p>\n<p>result_set = set1 - set2  # \u5dee\u96c6\u64cd\u4f5c<\/p>\n<p>result_list = list(result_set)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c\u662f\u5904\u7406\u6b64\u7c7b\u95ee\u9898\u7684\u4e00\u79cd\u9ad8\u6548\u65b9\u5f0f\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u6240\u4ee5\u8f6c\u6362\u56de\u5217\u8868\u540e\u5143\u7d20\u7684\u539f\u59cb\u987a\u5e8f\u53ef\u80fd\u4f1a\u4e22\u5931\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6027\u80fd\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u4ee5\u4e0a\u4e09\u79cd\u65b9\u6cd5\u90fd\u80fd\u591f\u6709\u6548\u5730\u89e3\u51b3\u95ee\u9898\uff0c\u4f46\u5728\u9762\u5bf9\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u65f6\uff0c\u6027\u80fd\u8003\u8651\u53d8\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u867d\u7136\u7b80\u6d01\u6613\u8bfb\uff0c\u4f46\u53ef\u80fd\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u90fd\u8fdb\u884c\u4e00\u6b21<code>in<\/code>\u64cd\u4f5c\uff0c\u5bfc\u81f4\u6548\u7387\u4f4e\u4e0b\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c\u56e0\u4e3a\u5229\u7528\u4e86\u6563\u5217\u6280\u672f\u67e5\u627e\u6548\u7387\u66f4\u9ad8\uff0c\u901a\u5e38\u662f\u6700\u5feb\u7684\u65b9\u6cd5\uff0c\u4f46\u9700\u8981\u727a\u7272\u6570\u636e\u7684\u987a\u5e8f\u6027\u3002<code>filter()<\/code>\u51fd\u6570\u7684\u6027\u80fd\u5728\u8fd9\u4e09\u8005\u4e4b\u95f4\uff0c\u4f46\u5982\u679c\u4e0e<code>lambda<\/code>\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u80fd\u7531\u4e8e\u6bcf\u6b21\u8c03\u7528\u90fd\u4ea7\u751f\u65b0\u7684\u51fd\u6570\u5bf9\u8c61\u800c\u7a0d\u663e\u4f4e\u6548\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6b63\u786e\u7684\u9009\u62e9\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u89c4\u6a21\u3001\u662f\u5426\u9700\u8981\u4fdd\u6301\u5143\u7d20\u987a\u5e8f\u4ee5\u53ca\u5bf9\u4ee3\u7801\u53ef\u8bfb\u6027\u7684\u8981\u6c42\u3002\u5f00\u53d1\u8005\u5e94\u8be5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u505a\u51fa\u7075\u6d3b\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p>\u5e38\u89c1\u7684\u4ece\u4e00\u4e2a\u5217\u8868\u5220\u9664\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u5b9e\u73b0\u65b9\u5f0f\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<ol>\n<li><strong>\u5982\u4f55\u5220\u9664\u4e00\u4e2a\u5217\u8868\u4e2d\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\uff1f<\/strong><br \/>\n\u8981\u5220\u9664\u4e00\u4e2a\u5217\u8868\u4e2d\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8005\u5faa\u73af\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u7684\u76ee\u6807\u662f\u5220\u9664\u5217\u8868A\u4e2d\u5b58\u5728\u4e8e\u5217\u8868B\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-python\">A = [1, 2, 3, 4, 5]\nB = [3, 4, 5]\n\nA = [x for x in A if x not in B]\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868A\uff0c\u5176\u4e2d\u5df2\u7ecf\u5220\u9664\u4e86\u4e0e\u5217\u8868B\u4e2d\u76f8\u540c\u7684\u5143\u7d20\u3002<\/p>\n<ol start=\"2\">\n<li><strong>\u5982\u4f55\u5220\u9664\u4e00\u4e2a\u5217\u8868\u4e2d\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u67d0\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\n\u8981\u5220\u9664\u4e00\u4e2a\u5217\u8868\u4e2d\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u7279\u5b9a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684remove()\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u4ece\u5217\u8868\u4e2d\u79fb\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u7684\u76ee\u6807\u662f\u4ece\u5217\u8868A\u4e2d\u5220\u9664\u5217\u8868B\u4e2d\u7684\u5143\u7d20\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-python\">A = [1, 2, 3, 4, 5]\nB = [3, 4, 5]\n\nfor element in B:\n    if element in A:\n        A.remove(element)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5217\u8868B\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u4f7f\u7528remove()\u65b9\u6cd5\u5c06\u5b83\u4eec\u4ece\u5217\u8868A\u4e2d\u5220\u9664\u3002<\/p>\n<ol start=\"3\">\n<li><strong>\u5982\u4f55\u5220\u9664\u4e00\u4e2a\u5217\u8868\u4e2d\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u91cd\u590d\u5143\u7d20\uff1f<\/strong><br \/>\n\u8981\u5220\u9664\u4e00\u4e2a\u5217\u8868\u4e2d\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u91cd\u590d\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u5b9e\u73b0\u3002\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c\u6765\u5220\u9664\u91cd\u590d\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u7684\u76ee\u6807\u662f\u4ece\u5217\u8868A\u4e2d\u5220\u9664\u4e0e\u5217\u8868B\u4e2d\u91cd\u590d\u7684\u5143\u7d20\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-python\">A = [1, 2, 3, 4, 5]\nB = [3, 4, 5]\n\nA = list(set(A) - set(B))\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5148\u5c06\u5217\u8868A\u548cB\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u4f7f\u7528\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c\u6765\u79fb\u9664\u91cd\u590d\u7684\u5143\u7d20\uff0c\u6700\u540e\u518d\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868A\uff0c\u5176\u4e2d\u5df2\u7ecf\u5220\u9664\u4e86\u91cd\u590d\u7684\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u5220\u9664\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\u5b58\u5728\u7684\u6240\u6709\u5143\u7d20\uff0c\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u6709 \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5229\u7528filter()\u51fd\u6570\u3001\u501f\u52a9\u96c6 [&hellip;]","protected":false},"author":3,"featured_media":174851,"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\/174844"}],"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=174844"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/174844\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/174851"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=174844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=174844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=174844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}