{"id":1096876,"date":"2025-01-08T15:05:58","date_gmt":"2025-01-08T07:05:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1096876.html"},"modified":"2025-01-08T15:06:01","modified_gmt":"2025-01-08T07:06:01","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-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1096876.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-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211914\/12f4b487-733b-4c2d-ba94-0dd66934efe5.webp\" alt=\"Python\u4e2d\u4e24\u4e2a\u5217\u8868\u5982\u4f55\u76f8\u51cf\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u4e24\u4e2a\u5217\u8868\u76f8\u51cf\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u3002<\/strong> \u5176\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u6700\u5e38\u7528\u4e14\u76f4\u89c2\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5b83\u80fd\u591f\u5feb\u901f\u5730\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u51cf\u53bb\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u5217\u8868\u751f\u6210\u65b9\u5f0f\uff0c\u5b83\u80fd\u591f\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u5bf9\u5217\u8868\u5143\u7d20\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u7b2c\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u68c0\u67e5\u8be5\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u7b2c\u4e8c\u4e2a\u5217\u8868\u4e2d\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u4fdd\u7559\u8be5\u5143\u7d20\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u8be5\u5217\u8868\u4e2d\u5305\u542b\u4e86\u6240\u6709\u4e0d\u5728\u7b2c\u4e8c\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\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\u5217\u8868\u751f\u6210\u65b9\u5f0f\uff0c\u5b83\u80fd\u591f\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u5bf9\u5217\u8868\u5143\u7d20\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u7b2c\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u68c0\u67e5\u8be5\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u7b2c\u4e8c\u4e2a\u5217\u8868\u4e2d\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u4fdd\u7559\u8be5\u5143\u7d20\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u8be5\u5217\u8868\u4e2d\u5305\u542b\u4e86\u6240\u6709\u4e0d\u5728\u7b2c\u4e8c\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\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<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\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u5b9e\u73b0\u4e86\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\uff0c\u6700\u7ec8\u8f93\u51fa\u7ed3\u679c\u4e3a [1, 2, 5]\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u64cd\u4f5c\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5217\u8868\u76f8\u51cf\u65b9\u6cd5\uff0c\u5b83\u5229\u7528\u4e86\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c\u6765\u5b9e\u73b0\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u3002\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c\u80fd\u591f\u5feb\u901f\u5730\u8ba1\u7b97\u51fa\u4e00\u4e2a\u96c6\u5408\u4e2d\u4e0d\u5305\u542b\u5728\u53e6\u4e00\u4e2a\u96c6\u5408\u4e2d\u7684\u5143\u7d20\uff0c\u4ece\u800c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u96c6\u5408\u3002\u6700\u540e\uff0c\u5c06\u8be5\u96c6\u5408\u8f6c\u6362\u56de\u5217\u8868\u5373\u53ef\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<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>\u4e0a\u8ff0\u4ee3\u7801\u901a\u8fc7\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c\u5b9e\u73b0\u4e86\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\uff0c\u6700\u7ec8\u8f93\u51fa\u7ed3\u679c\u4e3a [1, 2, 5]\u3002\u867d\u7136\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u7684\u65b9\u6cd5\u8f83\u4e3a\u7b80\u6d01\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u96c6\u5408\u64cd\u4f5c\u4f1a\u4e22\u5931\u5217\u8868\u4e2d\u91cd\u590d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u904d\u5386\u662f\u4e00\u79cd\u8f83\u4e3a\u76f4\u89c2\u7684\u5217\u8868\u76f8\u51cf\u65b9\u6cd5\uff0c\u901a\u8fc7\u904d\u5386\u7b2c\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u68c0\u67e5\u8be5\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u7b2c\u4e8c\u4e2a\u5217\u8868\u4e2d\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u4fdd\u7559\u8be5\u5143\u7d20\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u8be5\u5217\u8868\u4e2d\u5305\u542b\u4e86\u6240\u6709\u4e0d\u5728\u7b2c\u4e8c\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\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<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>\u4e0a\u8ff0\u4ee3\u7801\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5b9e\u73b0\u4e86\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\uff0c\u6700\u7ec8\u8f93\u51fa\u7ed3\u679c\u4e3a [1, 2, 5]\u3002\u867d\u7136\u4f7f\u7528\u5faa\u73af\u904d\u5386\u7684\u65b9\u6cd5\u8f83\u4e3a\u76f4\u89c2\uff0c\u4f46\u5728\u5904\u7406\u8f83\u5927\u7684\u5217\u8868\u65f6\uff0c\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>filter<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>filter<\/code>\u51fd\u6570\u662f\u4e00\u79cd\u5185\u7f6e\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u5b83\u80fd\u591f\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u901a\u8fc7<code>filter<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u8fc7\u6ee4\u6761\u4ef6\uff0c\u8be5\u6761\u4ef6\u7528\u4e8e\u68c0\u67e5\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u7b2c\u4e8c\u4e2a\u5217\u8868\u4e2d\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u4fdd\u7559\u8be5\u5143\u7d20\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<p>result = list(filter(lambda x: x not in list2, list1))<\/p>\n<p>print(result)  # \u8f93\u51fa [1, 2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u901a\u8fc7<code>filter<\/code>\u51fd\u6570\u5b9e\u73b0\u4e86\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\uff0c\u6700\u7ec8\u8f93\u51fa\u7ed3\u679c\u4e3a [1, 2, 5]\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>numpy<\/code>\u5e93<\/h3>\n<\/p>\n<p><p><code>numpy<\/code>\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\u3002\u901a\u8fc7<code>numpy<\/code>\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a<code>numpy<\/code>\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528<code>numpy<\/code>\u7684\u5dee\u96c6\u64cd\u4f5c\u6765\u8ba1\u7b97\u4e24\u4e2a\u6570\u7ec4\u7684\u5dee\u96c6\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u8f6c\u6362\u56de\u5217\u8868\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4]<\/p>\n<p>result = list(np.setdiff1d(list1, 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\u901a\u8fc7<code>numpy<\/code>\u5e93\u5b9e\u73b0\u4e86\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\uff0c\u6700\u7ec8\u8f93\u51fa\u7ed3\u679c\u4e3a [1, 2, 5]\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528<code>collections.Counter<\/code><\/h3>\n<\/p>\n<p><p><code>collections.Counter<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u8ba1\u6570\u7684\u5bb9\u5668\uff0c\u5b83\u80fd\u591f\u65b9\u4fbf\u5730\u7edf\u8ba1\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002\u901a\u8fc7<code>collections.Counter<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>collections.Counter<\/code>\u7edf\u8ba1\u4e24\u4e2a\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u7136\u540e\u8ba1\u7b97\u4e24\u4e2a\u8ba1\u6570\u5668\u7684\u5dee\u96c6\uff0c\u6700\u540e\u751f\u6210\u4e00\u4e2a\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<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>\u4e0a\u8ff0\u4ee3\u7801\u901a\u8fc7<code>collections.Counter<\/code>\u5b9e\u73b0\u4e86\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\uff0c\u6700\u7ec8\u8f93\u51fa\u7ed3\u679c\u4e3a [1, 2, 5]\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528<code>pandas<\/code>\u5e93<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u6570\u636e\u64cd\u4f5c\u51fd\u6570\u3002\u901a\u8fc7<code>pandas<\/code>\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a<code>pandas<\/code>\u7684<code>Series<\/code>\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>pandas<\/code>\u7684\u5dee\u96c6\u64cd\u4f5c\u6765\u8ba1\u7b97\u4e24\u4e2a<code>Series<\/code>\u5bf9\u8c61\u7684\u5dee\u96c6\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u8f6c\u6362\u56de\u5217\u8868\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4]<\/p>\n<p>result = list(pd.Series(list1).loc[~pd.Series(list1).isin(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\u901a\u8fc7<code>pandas<\/code>\u5e93\u5b9e\u73b0\u4e86\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\uff0c\u6700\u7ec8\u8f93\u51fa\u7ed3\u679c\u4e3a [1, 2, 5]\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u6027\u80fd\u5bf9\u6bd4<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u8f83\u5c0f\u7684\u5217\u8868\u65f6\uff0c\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\u7684\u6027\u80fd\u5dee\u5f02\u5e76\u4e0d\u660e\u663e\u3002\u7136\u800c\uff0c\u5728\u5904\u7406\u8f83\u5927\u7684\u5217\u8868\u65f6\uff0c\u6027\u80fd\u5dee\u5f02\u5c06\u53d8\u5f97\u8f83\u4e3a\u663e\u8457\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u96c6\u5408\u64cd\u4f5c\u901a\u5e38\u5177\u6709\u8f83\u9ad8\u7684\u6548\u7387\uff0c\u800c\u5faa\u73af\u904d\u5386\u548c<code>filter<\/code>\u51fd\u6570\u5728\u5904\u7406\u8f83\u5927\u7684\u5217\u8868\u65f6\u6548\u7387\u8f83\u4f4e\u3002<code>numpy<\/code>\u5e93\u548c<code>pandas<\/code>\u5e93\u867d\u7136\u6027\u80fd\u8f83\u9ad8\uff0c\u4f46\u9700\u8981\u989d\u5916\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0Python\u4e2d\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u64cd\u4f5c\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u96c6\u5408\u64cd\u4f5c\u3001\u5faa\u73af\u904d\u5386\u3001<code>filter<\/code>\u51fd\u6570\u3001<code>numpy<\/code>\u5e93\u3001<code>collections.Counter<\/code>\u548c<code>pandas<\/code>\u5e93\u5747\u53ef\u5b9e\u73b0\u5217\u8868\u76f8\u51cf\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5217\u8868\u76f8\u51cf\u64cd\u4f5c\uff0c\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5b9e\u73b0\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8005\u4f7f\u7528\u96c6\u5408\u8fd0\u7b97\u6765\u5b9e\u73b0\u4e24\u4e2a\u5217\u8868\u7684\u76f8\u51cf\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u65f6\uff0c\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5305\u542b\u7b2c\u4e00\u4e2a\u5217\u8868\u4e2d\u5b58\u5728\u800c\u7b2c\u4e8c\u4e2a\u5217\u8868\u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">list1 = [1, 2, 3, 4, 5]\nlist2 = [3, 4]\nresult = [item for item in list1 if item not in list2]\nprint(result)  # \u8f93\u51fa: [1, 2, 5]\n<\/code><\/pre>\n<p>\u4f7f\u7528\u96c6\u5408\u8fd0\u7b97\u65f6\uff0c\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u8fdb\u884c\u5dee\u96c6\u8fd0\u7b97\uff0c\u6700\u540e\u518d\u5c06\u7ed3\u679c\u8f6c\u6362\u56de\u5217\u8868\uff1a<\/p>\n<pre><code class=\"language-python\">result = list(set(list1) - set(list2))\nprint(result)  # \u8f93\u51fa: [1, 2, 5]\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u51cf\u6cd5\u8fd0\u7b97\u7b26\u5bf9\u5217\u8868\u8fdb\u884c\u76f8\u51cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u65e0\u6cd5\u76f4\u63a5\u5bf9\u5217\u8868\u4f7f\u7528\u51cf\u6cd5\u8fd0\u7b97\u7b26\uff08<code>-<\/code>\uff09\uff0c\u56e0\u4e3a\u8fd9\u4e0d\u662fPython\u5185\u7f6e\u7684\u64cd\u4f5c\u3002\u5982\u679c\u9700\u8981\u8fdb\u884c\u51cf\u6cd5\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528NumPy\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u6570\u7ec4\u5bf9\u8c61\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u5143\u7d20\u7ea7\u7684\u51cf\u6cd5\u8fd0\u7b97\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\narr1 = np.array(list1)\narr2 = np.array(list2)\nresult = arr1[arr1 != arr2]  # \u8fdb\u884c\u5143\u7d20\u7ea7\u7684\u6bd4\u8f83\nprint(result)  # \u8f93\u51fa: [1 2 5]\n<\/code><\/pre>\n<p>\u4f7f\u7528NumPy\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u8be5\u5e93\u3002<\/p>\n<p><strong>\u5982\u679c\u4e24\u4e2a\u5217\u8868\u4e2d\u6709\u91cd\u590d\u5143\u7d20\uff0c\u5982\u4f55\u5904\u7406\u76f8\u51cf\u7684\u7ed3\u679c\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684Counter\u7c7b\uff0c\u5b83\u80fd\u591f\u8ba1\u7b97\u6bcf\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u76f8\u51cf\u65f6\u8003\u8651\u5230\u5143\u7d20\u7684\u6570\u91cf\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import Counter\nlist1 = [1, 2, 2, 3, 4]\nlist2 = [2, 3]\ncounter1 = Counter(list1)\ncounter2 = Counter(list2)\nresult = list((counter1 - counter2).elements())\nprint(result)  # \u8f93\u51fa: [1, 2, 4]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u4e86\u5728\u76f8\u51cf\u65f6\uff0c\u7ed3\u679c\u4e2d\u4ec5\u4fdd\u7559\u5269\u4f59\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u91cd\u590d\u5143\u7d20\u7684\u6570\u91cf\u4e5f\u5f97\u5230\u5408\u7406\u7684\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u4e24\u4e2a\u5217\u8868\u76f8\u51cf\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u3002 \u5176\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u6700\u5e38\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1096889,"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\/1096876"}],"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=1096876"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096876\/revisions"}],"predecessor-version":[{"id":1096892,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096876\/revisions\/1096892"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1096889"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1096876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1096876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1096876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}