{"id":1041744,"date":"2024-12-31T12:51:42","date_gmt":"2024-12-31T04:51:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1041744.html"},"modified":"2024-12-31T12:51:45","modified_gmt":"2024-12-31T04:51:45","slug":"python%e5%a6%82%e4%bd%95%e4%b8%a4%e4%b8%aa%e6%95%b0%e7%bb%84%e5%b7%ae%e9%9b%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1041744.html","title":{"rendered":"python\u5982\u4f55\u4e24\u4e2a\u6570\u7ec4\u5dee\u96c6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/c42e4cf1-2089-429e-b9f9-afcf2021453c.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u4e24\u4e2a\u6570\u7ec4\u5dee\u96c6\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u8ba1\u7b97\u4e24\u4e2a\u6570\u7ec4\u7684\u5dee\u96c6\u6709\u591a\u79cd\u65b9\u6cd5\u3002<strong>\u4f7f\u7528set\u6570\u636e\u7ed3\u6784\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528Numpy\u5e93<\/strong>\u662f\u5176\u4e2d\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u8ba8\u8bba\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u9996\u5148\uff0c\u6211\u4eec\u5c06\u7b80\u8981\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u7684\u6838\u5fc3\u601d\u60f3\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528set\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684<code>set<\/code>\u6570\u636e\u7ed3\u6784\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u64cd\u4f5c\u96c6\u5408\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8ba1\u7b97\u51fa\u4e24\u4e2a\u6570\u7ec4\u7684\u5dee\u96c6\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u4f4e\uff0c\u9002\u7528\u4e8e\u5143\u7d20\u552f\u4e00\u4e14\u4e0d\u5173\u5fc3\u5143\u7d20\u987a\u5e8f\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def difference_using_set(array1, array2):<\/p>\n<p>    set1 = set(array1)<\/p>\n<p>    set2 = set(array2)<\/p>\n<p>    difference = list(set1 - set2)<\/p>\n<p>    return difference<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528<code>set<\/code>\u6570\u636e\u7ed3\u6784\u8ba1\u7b97\u5dee\u96c6\u7684\u4f18\u70b9\u5728\u4e8e\u64cd\u4f5c\u7b80\u5355\u4e14\u6548\u7387\u9ad8\uff0c\u56e0\u4e3a\u96c6\u5408\u7684\u5e95\u5c42\u5b9e\u73b0\u662f\u54c8\u5e0c\u8868\uff0c\u67e5\u627e\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\u3002\u4f46\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u6709\u4e00\u4e2a\u7f3a\u70b9\uff0c\u5373\u5143\u7d20\u7684\u987a\u5e8f\u4f1a\u88ab\u6253\u4e71\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u975e\u5e38Pythonic\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u904d\u5386\u4e00\u4e2a\u5217\u8868\u5e76\u7b5b\u9009\u51fa\u4e0d\u5728\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u6765\u5b9e\u73b0\u5dee\u96c6\u3002\u8fd9\u79cd\u65b9\u6cd5\u4fdd\u7559\u4e86\u6570\u7ec4\u7684\u987a\u5e8f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u6301\u539f\u6570\u7ec4\u987a\u5e8f\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def difference_using_list_comprehension(array1, array2):<\/p>\n<p>    difference = [item for item in array1 if item not in array2]<\/p>\n<p>    return difference<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<p>\u5217\u8868\u89e3\u6790\u7684\u4f18\u52bf\u5728\u4e8e\u4ee3\u7801\u7b80\u6d01\u660e\u4e86\uff0c\u540c\u65f6\u4fdd\u7559\u4e86\u5143\u7d20\u7684\u987a\u5e8f\u3002\u7136\u800c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u4e3aO(n*m)\uff0c\u5176\u4e2dn\u548cm\u5206\u522b\u662f\u4e24\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u56e0\u6b64\uff0c\u5217\u8868\u89e3\u6790\u9002\u7528\u4e8e\u6570\u636e\u91cf\u8f83\u5c0f\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Numpy\u5e93<\/h3>\n<\/p>\n<p><p>Numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002\u4f7f\u7528Numpy\u7684<code>setdiff1d<\/code>\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u4e24\u4e2a\u6570\u7ec4\u7684\u5dee\u96c6\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def difference_using_numpy(array1, array2):<\/p>\n<p>    difference = np.setdiff1d(array1, array2)<\/p>\n<p>    return difference<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<p>Numpy\u7684<code>setdiff1d<\/code>\u51fd\u6570\u5728\u5e95\u5c42\u4f7f\u7528C\u8bed\u8a00\u5b9e\u73b0\uff0c\u6027\u80fd\u4f18\u8d8a\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u7684\u5904\u7406\u3002\u7136\u800c\uff0c\u9700\u8981\u5148\u5b89\u88c5Numpy\u5e93\uff0c\u5e76\u4e14\u4e0d\u9002\u7528\u4e8e\u4e0d\u9700\u8981\u989d\u5916\u4f9d\u8d56\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u6bd4\u8f83\u4e0e\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u6027\u80fd\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u8fd9\u51e0\u79cd\u65b9\u6cd5\u7684\u6027\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8ba1\u65f6\u5b9e\u9a8c\u5bf9\u5b83\u4eec\u8fdb\u884c\u6bd4\u8f83\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u65f6\u5b9e\u9a8c\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u5b9a\u4e49\u6d4b\u8bd5\u6570\u636e<\/strong><\/h2>\n<p>array1 = list(range(10000))<\/p>\n<p>array2 = list(range(5000, 15000))<\/p>\n<h2><strong>\u8ba1\u65f6\u51fd\u6570<\/strong><\/h2>\n<p>def time_function(func, *args):<\/p>\n<p>    start = time.time()<\/p>\n<p>    result = func(*args)<\/p>\n<p>    end = time.time()<\/p>\n<p>    return end - start<\/p>\n<h2><strong>\u8ba1\u65f6\u6bd4\u8f83<\/strong><\/h2>\n<p>print(&quot;Set:&quot;, time_function(difference_using_set, array1, array2))<\/p>\n<p>print(&quot;List Comprehension:&quot;, time_function(difference_using_list_comprehension, array1, array2))<\/p>\n<p>print(&quot;Numpy:&quot;, time_function(difference_using_numpy, array1, array2))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u9a8c\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5728\u6570\u636e\u91cf\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\uff0c<strong>\u4f7f\u7528Numpy\u5e93\u548cset\u6570\u636e\u7ed3\u6784\u7684\u65b9\u6cd5\u6027\u80fd\u4f18\u8d8a<\/strong>\uff0c\u800c\u5217\u8868\u89e3\u6790\u65b9\u6cd5\u7684\u6027\u80fd\u5219\u8f83\u5dee\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<ul>\n<li><strong>set\u6570\u636e\u7ed3\u6784<\/strong>\uff1a\u9002\u7528\u4e8e\u5143\u7d20\u552f\u4e00\u4e14\u4e0d\u5173\u5fc3\u5143\u7d20\u987a\u5e8f\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u5217\u8868\u89e3\u6790<\/strong>\uff1a\u9002\u7528\u4e8e\u6570\u636e\u91cf\u8f83\u5c0f\u4e14\u9700\u8981\u4fdd\u7559\u5143\u7d20\u987a\u5e8f\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>Numpy\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u7684\u5904\u7406\uff0c\u5c24\u5176\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u9886\u57df\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u5176\u4ed6\u65b9\u6cd5\u4e0e\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u6570\u7ec4\u5dee\u96c6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>collections.Counter<\/code>\u6765\u8ba1\u7b97\u591a\u91cd\u96c6\u5408\u7684\u5dee\u96c6\uff0c\u4f7f\u7528Pandas\u5e93\u6765\u5904\u7406\u6570\u636e\u6846\u7684\u5dee\u96c6\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528collections.Counter<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def difference_using_counter(array1, array2):<\/p>\n<p>    counter1 = Counter(array1)<\/p>\n<p>    counter2 = Counter(array2)<\/p>\n<p>    difference = list((counter1 - counter2).elements())<\/p>\n<p>    return difference<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<p><code>Counter<\/code>\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u8ba1\u6570\u5668\uff0c\u7528\u4e8e\u8ba1\u7b97\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u8ba1\u7b97\u591a\u91cd\u96c6\u5408\u7684\u5dee\u96c6\uff0c\u5373\u5141\u8bb8\u5143\u7d20\u91cd\u590d\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528Pandas\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def difference_using_pandas(array1, array2):<\/p>\n<p>    df1 = pd.DataFrame(array1, columns=[&#39;value&#39;])<\/p>\n<p>    df2 = pd.DataFrame(array2, columns=[&#39;value&#39;])<\/p>\n<p>    difference = df1[~df1[&#39;value&#39;].isin(df2[&#39;value&#39;])][&#39;value&#39;].tolist()<\/p>\n<p>    return difference<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/strong><\/p>\n<p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6570\u636e\u6846\uff0c\u5e76\u4f7f\u7528<code>isin<\/code>\u51fd\u6570\u7b5b\u9009\u51fa\u5dee\u96c6\uff0c\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u9886\u57df\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u4e86Python\u4e2d\u8ba1\u7b97\u4e24\u4e2a\u6570\u7ec4\u5dee\u96c6\u7684\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\uff0c\u5305\u62ec<strong>\u4f7f\u7528set\u6570\u636e\u7ed3\u6784\u3001\u5217\u8868\u89e3\u6790\u3001Numpy\u5e93\u3001collections.Counter\u548cPandas\u5e93<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>set\u6570\u636e\u7ed3\u6784<\/strong>\uff1a\u64cd\u4f5c\u7b80\u5355\u9ad8\u6548\uff0c\u4f46\u4e0d\u4fdd\u7559\u5143\u7d20\u987a\u5e8f\u3002<\/li>\n<li><strong>\u5217\u8868\u89e3\u6790<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\u660e\u4e86\uff0c\u4fdd\u7559\u5143\u7d20\u987a\u5e8f\uff0c\u4f46\u6027\u80fd\u8f83\u5dee\u3002<\/li>\n<li><strong>Numpy\u5e93<\/strong>\uff1a\u6027\u80fd\u4f18\u8d8a\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002<\/li>\n<li><strong>collections.Counter<\/strong>\uff1a\u9002\u7528\u4e8e\u591a\u91cd\u96c6\u5408\u5dee\u96c6\u8ba1\u7b97\u3002<\/li>\n<li><strong>Pandas\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u9886\u57df\u3002<\/li>\n<\/ul>\n<p><p>\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u4e2d\u8ba1\u7b97\u6570\u7ec4\u5dee\u96c6\u7684\u65b9\u6cd5\u3002\u65e0\u8bba\u662f\u8fdb\u884c\u65e5\u5e38\u5f00\u53d1\uff0c\u8fd8\u662f\u5904\u7406\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u80fd\u591f\u663e\u8457\u63d0\u5347\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u4e24\u4e2a\u6570\u7ec4\u7684\u5dee\u96c6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u8f7b\u677e\u8ba1\u7b97\u4e24\u4e2a\u6570\u7ec4\u7684\u5dee\u96c6\u3002\u901a\u8fc7\u5c06\u4e24\u4e2a\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u4f7f\u7528\u51cf\u6cd5\u8fd0\u7b97\u7b26\uff08<code>-<\/code>\uff09\u6216\u8c03\u7528<code>difference()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f97\u5230\u7b2c\u4e00\u4e2a\u6570\u7ec4\u4e2d\u5b58\u5728\u800c\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">array1 = [1, 2, 3, 4]\narray2 = [3, 4, 5, 6]\ndifference = set(array1) - set(array2)\nprint(difference)  # \u8f93\u51fa: {1, 2}\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6548\u7387\u8f83\u9ad8\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5185\u7f6e\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u5dee\u96c6\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u7684<code>set<\/code>\u7c7b\u578b\u63d0\u4f9b\u4e86<code>difference()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u8ba1\u7b97\u5dee\u96c6\u3002\u8c03\u7528\u683c\u5f0f\u4e3a<code>set1.difference(set2)<\/code>\uff0c\u5b83\u5c06\u8fd4\u56de\u5728<code>set1<\/code>\u4e2d\u4f46\u4e0d\u5728<code>set2<\/code>\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">array1 = [1, 2, 3, 4]\narray2 = [3, 4, 5, 6]\ndifference = set(array1).difference(set(array2))\nprint(difference)  # \u8f93\u51fa: {1, 2}\n<\/code><\/pre>\n<p>\u6b64\u65b9\u6cd5\u540c\u6837\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u9700\u8981\u660e\u786e\u8868\u8fbe\u610f\u56fe\u7684\u573a\u5408\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5dee\u96c6\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u7ed3\u679c\u7684\u987a\u5e8f\uff1f<\/strong><br \/>\u4f7f\u7528\u96c6\u5408\u8ba1\u7b97\u5dee\u96c6\u65f6\uff0c\u7ed3\u679c\u662f\u65e0\u5e8f\u7684\u3002\u5982\u679c\u9700\u8981\u4fdd\u7559\u539f\u6570\u7ec4\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u624b\u52a8\u8fc7\u6ee4\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">array1 = [1, 2, 3, 4]\narray2 = [3, 4, 5, 6]\ndifference = [item for item in array1 if item not in array2]\nprint(difference)  # \u8f93\u51fa: [1, 2]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u4fdd\u7559\u5143\u7d20\u7684\u987a\u5e8f\uff0c\u8fd8\u5141\u8bb8\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6761\u4ef6\u8fc7\u6ee4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u4e24\u4e2a\u6570\u7ec4\u7684\u5dee\u96c6\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u4f7f\u7528set\u6570\u636e\u7ed3\u6784\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528Numpy\u5e93\u662f\u5176\u4e2d\u51e0\u79cd\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1041762,"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\/1041744"}],"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=1041744"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1041744\/revisions"}],"predecessor-version":[{"id":1041764,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1041744\/revisions\/1041764"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1041762"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1041744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1041744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1041744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}