{"id":1061667,"date":"2024-12-31T15:44:47","date_gmt":"2024-12-31T07:44:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1061667.html"},"modified":"2024-12-31T15:44:52","modified_gmt":"2024-12-31T07:44:52","slug":"python%e4%b8%ad%e4%b8%a4%e4%b8%aa%e5%88%97%e8%a1%a8%e5%a6%82%e4%bd%95%e7%9b%b8%e5%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1061667.html","title":{"rendered":"Python\u4e2d\u4e24\u4e2a\u5217\u8868\u5982\u4f55\u76f8\u51cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/59b4e6df-7599-4760-974b-4b1538159f5c.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u4e2d\u4e24\u4e2a\u5217\u8868\u5982\u4f55\u76f8\u51cf\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u4e24\u4e2a\u5217\u8868\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u8fdb\u884c\u76f8\u51cf\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528\u96c6\u5408\u8fd0\u7b97\u3001\u4f7f\u7528\u5faa\u73af\u7b49\u3002<strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u96c6\u5408\u8fd0\u7b97\u3001\u5faa\u73af<\/strong>\u3002\u5176\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u6700\u5e38\u7528\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u64cd\u4f5c\uff0c\u540c\u65f6\u5177\u6709\u5f88\u597d\u7684\u53ef\u8bfb\u6027\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u3002<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u5c06\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\u7ed3\u5408\u5728\u4e00\u8d77\u7684\u7b80\u6d01\u65b9\u5f0f\u3002\u5b83\u7684\u8bed\u6cd5\u7ed3\u6784\u7b80\u5355\u660e\u4e86\uff0c\u975e\u5e38\u9002\u5408\u7528\u6765\u8fdb\u884c\u5217\u8868\u7684\u521b\u5efa\u548c\u4fee\u6539\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u904d\u5386\u7b2c\u4e00\u4e2a\u5217\u8868\uff0c\u5e76\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u5728\u7b2c\u4e8c\u4e2a\u5217\u8868\u4e2d\uff0c\u5982\u679c\u4e0d\u5728\uff0c\u5219\u5c06\u8be5\u5143\u7d20\u6dfb\u52a0\u5230\u65b0\u7684\u5217\u8868\u4e2d\u3002\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5b9e\u73b0\u5217\u8868\u76f8\u51cf<\/strong><\/h2>\n<p>result = [item for item in list1 if item not in list2]<\/p>\n<p>print(result)  # \u8f93\u51fa: [1, 2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>item for item in list1 if item not in list2<\/code> \u662f\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u6838\u5fc3\u90e8\u5206\u3002\u5b83\u904d\u5386 <code>list1<\/code> \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20 <code>item<\/code>\uff0c\u5e76\u68c0\u67e5\u8be5\u5143\u7d20\u662f\u5426\u4e0d\u5728 <code>list2<\/code> \u4e2d\u3002\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u5c06\u8be5\u5143\u7d20\u6dfb\u52a0\u5230\u7ed3\u679c\u5217\u8868 <code>result<\/code> \u4e2d\u3002\u6700\u7ec8\uff0c<code>result<\/code> \u5217\u8868\u5c31\u5305\u542b\u4e86\u6240\u6709\u5728 <code>list1<\/code> \u4e2d\u4f46\u4e0d\u5728 <code>list2<\/code> \u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u4ecb\u7ecd\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879\u548c\u4f18\u5316\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\u8fd0\u7b97<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\u8fd0\u7b97\u6765\u8fdb\u884c\u5217\u8868\u76f8\u51cf\u662f\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u96c6\u5408\uff08Set\uff09\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u6570\u636e\u7ed3\u6784\uff0c\u652f\u6301\u96c6\u5408\u7684\u5dee\u96c6\u8fd0\u7b97\u3002\u6211\u4eec\u53ef\u4ee5\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\">list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4]<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u5dee\u96c6\u8fd0\u7b97\u5b9e\u73b0\u5217\u8868\u76f8\u51cf<\/strong><\/h2>\n<p>result = list(set(list1) - set(list2))<\/p>\n<p>print(result)  # \u8f93\u51fa: [1, 2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u4e8e\u7406\u89e3\uff0c\u5e76\u4e14\u96c6\u5408\u8fd0\u7b97\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u4f4e\uff0c\u9002\u5408\u5904\u7406\u8f83\u5927\u7684\u6570\u636e\u96c6\u3002\u7136\u800c\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4f1a\u4e22\u5931\u5217\u8868\u7684\u987a\u5e8f\u3002\u5982\u679c\u4fdd\u6301\u987a\u5e8f\u975e\u5e38\u91cd\u8981\uff0c\u90a3\u4e48\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u4e0d\u9002\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u6765\u5b9e\u73b0\u5217\u8868\u76f8\u51cf\u662f\u4e00\u79cd\u76f4\u89c2\u4f46\u76f8\u5bf9\u7e41\u7410\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u6765\u904d\u5386\u4e24\u4e2a\u5217\u8868\uff0c\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u5728\u7b2c\u4e8c\u4e2a\u5217\u8868\u4e2d\uff0c\u5982\u679c\u4e0d\u5728\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u7ed3\u679c\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4]<\/p>\n<h2><strong>\u4f7f\u7528\u5faa\u73af\u5b9e\u73b0\u5217\u8868\u76f8\u51cf<\/strong><\/h2>\n<p>result = []<\/p>\n<p>for item in list1:<\/p>\n<p>    if item not in list2:<\/p>\n<p>        result.append(item)<\/p>\n<p>print(result)  # \u8f93\u51fa: [1, 2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u903b\u8f91\u6e05\u6670\uff0c\u5bb9\u6613\u7406\u89e3\uff0c\u4f46\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u6548\u7387\u8f83\u4f4e\u3002\u56e0\u4e3a\u6bcf\u6b21\u68c0\u67e5\u90fd\u9700\u8981\u904d\u5386\u6574\u4e2a\u7b2c\u4e8c\u4e2a\u5217\u8868\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528Counter\u8ba1\u6570\u5668<\/h2>\n<\/p>\n<p><p><code>Counter<\/code> \u662f Python \u7684 <code>collections<\/code> \u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u7528\u4e8e\u8ba1\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>Counter<\/code> \u6765\u5b9e\u73b0\u5217\u8868\u76f8\u51cf\u3002\u901a\u8fc7\u5bf9\u4e24\u4e2a\u5217\u8868\u8fdb\u884c\u8ba1\u6570\uff0c\u7136\u540e\u6839\u636e\u8ba1\u6570\u7ed3\u679c\u6765\u751f\u6210\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4]<\/p>\n<h2><strong>\u4f7f\u7528Counter\u5b9e\u73b0\u5217\u8868\u76f8\u51cf<\/strong><\/h2>\n<p>counter1 = Counter(list1)<\/p>\n<p>counter2 = Counter(list2)<\/p>\n<p>result = list((counter1 - counter2).elements())<\/p>\n<p>print(result)  # \u8f93\u51fa: [1, 2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\uff0c\u4e14 <code>Counter<\/code> \u63d0\u4f9b\u4e86\u5f88\u591a\u6709\u7528\u7684\u65b9\u6cd5\u6765\u5904\u7406\u8ba1\u6570\u95ee\u9898\u3002\u9002\u5408\u5904\u7406\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u5217\u8868\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>Counter<\/code> \u751f\u6210\u7684\u7ed3\u679c\u662f\u65e0\u5e8f\u7684\uff0c\u5982\u679c\u987a\u5e8f\u5f88\u91cd\u8981\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528NumPy\u5e93<\/h2>\n<\/p>\n<p><p><code>NumPy<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>NumPy<\/code> \u6765\u5b9e\u73b0\u5217\u8868\u76f8\u51cf\u3002<code>NumPy<\/code> \u7684\u6570\u7ec4\u8fd0\u7b97\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>list1 = np.array([1, 2, 3, 4, 5])<\/p>\n<p>list2 = np.array([3, 4])<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u5b9e\u73b0\u5217\u8868\u76f8\u51cf<\/strong><\/h2>\n<p>result = np.setdiff1d(list1, list2)<\/p>\n<p>print(result)  # \u8f93\u51fa: [1 2 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u9ad8\u6548\uff0c\u9002\u5408\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002\u4f46\u9700\u8981\u5b89\u88c5 <code>NumPy<\/code> \u5e93\uff0c\u5e76\u4e14\u5728\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u65f6\u53ef\u80fd\u663e\u5f97\u6709\u4e9b\u5927\u6750\u5c0f\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4fdd\u6301\u987a\u5e8f\u7684\u5217\u8868\u76f8\u51cf<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u4fdd\u6301\u5217\u8868\u7684\u539f\u59cb\u987a\u5e8f\u975e\u5e38\u91cd\u8981\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u57fa\u7840\u4e0a\uff0c\u786e\u4fdd\u7ed3\u679c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u987a\u5e8f\u4e0e\u539f\u59cb\u5217\u8868\u4e2d\u7684\u987a\u5e8f\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4]<\/p>\n<h2><strong>\u4fdd\u6301\u987a\u5e8f\u7684\u5217\u8868\u76f8\u51cf<\/strong><\/h2>\n<p>result = [item for item in list1 if item not in list2]<\/p>\n<p>print(result)  # \u8f93\u51fa: [1, 2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u9ad8\u6548\uff0c\u5e76\u4e14\u80fd\u591f\u4fdd\u6301\u539f\u59cb\u5217\u8868\u7684\u987a\u5e8f\u3002\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u5904\u7406\u91cd\u590d\u5143\u7d20<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5217\u8868\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\u3002\u6211\u4eec\u9700\u8981\u786e\u4fdd\u76f8\u51cf\u64cd\u4f5c\u80fd\u591f\u6b63\u786e\u5904\u7406\u8fd9\u4e9b\u91cd\u590d\u5143\u7d20\u3002\u53ef\u4ee5\u7ed3\u5408 <code>Counter<\/code> \u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>list1 = [1, 2, 2, 3, 4, 5]<\/p>\n<p>list2 = [2, 3]<\/p>\n<h2><strong>\u5904\u7406\u91cd\u590d\u5143\u7d20\u7684\u5217\u8868\u76f8\u51cf<\/strong><\/h2>\n<p>counter1 = Counter(list1)<\/p>\n<p>counter2 = Counter(list2)<\/p>\n<p>result = []<\/p>\n<p>for item in list1:<\/p>\n<p>    if counter2[item] &gt; 0:<\/p>\n<p>        counter2[item] -= 1<\/p>\n<p>    else:<\/p>\n<p>        result.append(item)<\/p>\n<p>print(result)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e86\u91cd\u590d\u5143\u7d20\u6309\u6b63\u786e\u7684\u6570\u91cf\u8fdb\u884c\u5904\u7406\uff0c\u5e76\u4e14\u80fd\u591f\u4fdd\u6301\u539f\u59cb\u5217\u8868\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u6027\u80fd\u6bd4\u8f83<\/h2>\n<\/p>\n<p><p>\u4e0d\u540c\u65b9\u6cd5\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u7684\u6027\u80fd\u8868\u73b0\u5404\u5f02\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757\u6765\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6027\u80fd\u6bd4\u8f83\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>list1 = list(range(1000))<\/p>\n<p>list2 = list(range(500, 1000))<\/p>\n<h2><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p>def list_comprehension():<\/p>\n<p>    return [item for item in list1 if item not in list2]<\/p>\n<h2><strong>\u96c6\u5408\u8fd0\u7b97<\/strong><\/h2>\n<p>def set_difference():<\/p>\n<p>    return list(set(list1) - set(list2))<\/p>\n<h2><strong>\u5faa\u73af<\/strong><\/h2>\n<p>def loop_method():<\/p>\n<p>    result = []<\/p>\n<p>    for item in list1:<\/p>\n<p>        if item not in list2:<\/p>\n<p>            result.append(item)<\/p>\n<p>    return result<\/p>\n<h2><strong>Counter\u8ba1\u6570\u5668<\/strong><\/h2>\n<p>def counter_method():<\/p>\n<p>    counter1 = Counter(list1)<\/p>\n<p>    counter2 = Counter(list2)<\/p>\n<p>    return list((counter1 - counter2).elements())<\/p>\n<h2><strong>NumPy\u5e93<\/strong><\/h2>\n<p>def numpy_method():<\/p>\n<p>    return np.setdiff1d(np.array(list1), np.array(list2))<\/p>\n<h2><strong>\u6d4b\u8bd5\u6027\u80fd<\/strong><\/h2>\n<p>print(timeit.timeit(list_comprehension, number=1000))<\/p>\n<p>print(timeit.timeit(set_difference, number=1000))<\/p>\n<p>print(timeit.timeit(loop_method, number=1000))<\/p>\n<p>print(timeit.timeit(counter_method, number=1000))<\/p>\n<p>print(timeit.timeit(numpy_method, number=1000))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6027\u80fd\u6bd4\u8f83\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u7279\u5b9a\u573a\u666f\u7684\u65b9\u6cd5\u3002\u5728\u5904\u7406\u8f83\u5927\u6570\u636e\u96c6\u65f6\uff0c\u96c6\u5408\u8fd0\u7b97\u548c <code>NumPy<\/code> \u901a\u5e38\u8868\u73b0\u8f83\u597d\uff1b\u800c\u5728\u9700\u8981\u4fdd\u6301\u987a\u5e8f\u6216\u5904\u7406\u91cd\u590d\u5143\u7d20\u65f6\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u548c <code>Counter<\/code> \u66f4\u52a0\u9002\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u76f8\u51cf\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec<strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u96c6\u5408\u8fd0\u7b97\u3001\u5faa\u73af\u3001Counter\u8ba1\u6570\u5668\u3001NumPy\u5e93<\/strong>\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002\u5efa\u8bae\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u6570\u636e\u89c4\u6a21\u548c\u5177\u4f53\u9700\u6c42\uff0c\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u786e\u4fdd\u4ee3\u7801\u9ad8\u6548\u3001\u7b80\u6d01\u3001\u51c6\u786e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u60f3\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u51cf\u53bb\u53e6\u4e00\u4e2a\u5217\u8868\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8fc7\u6ee4\u51fa\u5728\u7b2c\u4e00\u4e2a\u5217\u8868\u4e2d\u4f46\u4e0d\u5728\u7b2c\u4e8c\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e24\u4e2a\u5217\u8868 <code>list1 = [1, 2, 3, 4]<\/code> \u548c <code>list2 = [2, 4]<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u76f8\u51cf\uff1a  <\/p>\n<pre><code class=\"language-python\">result = [item for item in list1 if item not in list2]\n<\/code><\/pre>\n<p>\u8fd0\u884c\u540e\uff0c<code>result<\/code> \u5c06\u4f1a\u662f <code>[1, 3]<\/code>\u3002<\/p>\n<p><strong>\u4f7f\u7528\u96c6\u5408\u8fd0\u7b97\u5982\u4f55\u7b80\u5316\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\uff1f<\/strong><br \/>\u5229\u7528Python\u7684\u96c6\u5408\uff08set\uff09\u53ef\u4ee5\u66f4\u52a0\u7b80\u6d01\u5730\u5b9e\u73b0\u5217\u8868\u76f8\u51cf\u3002\u5c06\u4e24\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">set1 = set(list1)\nset2 = set(list2)\nresult = list(set1 - set2)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c<code>result<\/code> \u5c06\u540c\u6837\u662f <code>[1, 3]<\/code>\uff0c\u5e76\u4e14\u5728\u5904\u7406\u8f83\u5927\u6570\u636e\u65f6\u4e5f\u4f1a\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5217\u8868\u7684\u76f8\u51cf\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u96c6\u5408\u8fd0\u7b97\uff0cPython\u8fd8\u652f\u6301\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u6570\u7ec4\u64cd\u4f5c\u3002\u5982\u679c\u4f60\u7684\u5217\u8868\u5305\u542b\u6570\u5b57\uff0cNumPy\u53ef\u4ee5\u63d0\u4f9b\u66f4\u9ad8\u6548\u7684\u8ba1\u7b97\u65b9\u5f0f\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\narray1 = np.array(list1)\narray2 = np.array(list2)\nresult = np.setdiff1d(array1, array2).tolist()\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e5f\u4f1a\u8fd4\u56de <code>[1, 3]<\/code>\uff0c\u800c\u4e14\u5728\u5904\u7406\u6570\u503c\u8ba1\u7b97\u65f6\u4f1a\u66f4\u5177\u6027\u80fd\u4f18\u52bf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4e24\u4e2a\u5217\u8868\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u8fdb\u884c\u76f8\u51cf\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528\u96c6\u5408\u8fd0\u7b97\u3001\u4f7f\u7528\u5faa\u73af\u7b49\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1061684,"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\/1061667"}],"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=1061667"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061667\/revisions"}],"predecessor-version":[{"id":1061690,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061667\/revisions\/1061690"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1061684"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1061667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1061667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1061667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}