{"id":1133807,"date":"2025-01-08T21:09:57","date_gmt":"2025-01-08T13:09:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133807.html"},"modified":"2025-01-08T21:10:00","modified_gmt":"2025-01-08T13:10:00","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e4%b8%a4%e4%b8%aa%e5%88%97%e8%a1%a8%e4%b8%ad%e7%9a%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1133807.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102936\/ba7aae94-0646-4670-89af-9ad62108f779.webp\" alt=\"python\u5982\u4f55\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u6807\u51c6\u7684\u5faa\u73af\u7b49\u65b9\u6cd5\uff0c\u786e\u4fdd\u5220\u9664\u64cd\u4f5c\u7684\u9ad8\u6548\u6027\u548c\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u662f\u6700\u4e3a\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u96c6\u5408\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u4f4e\u3002\u8ba9\u6211\u4eec\u6df1\u5165\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u96c6\u5408\u64cd\u4f5c\u662f\u5904\u7406\u5217\u8868\u6570\u636e\u7684\u9ad8\u6548\u65b9\u5f0f\u3002\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u53ef\u4ee5\u5229\u7528\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c\u6765\u5feb\u901f\u5220\u9664\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e24\u4e2a\u5217\u8868<\/p>\n<p>list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4, 5, 6, 7]<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u96c6\u5408\u5e76\u8fdb\u884c\u5dee\u96c6\u64cd\u4f5c<\/strong><\/h2>\n<p>set1 = set(list1)<\/p>\n<p>set2 = set(list2)<\/p>\n<h2><strong>\u83b7\u53d6\u5dee\u96c6<\/strong><\/h2>\n<p>result1 = list(set1 - set2)<\/p>\n<p>result2 = list(set2 - set1)<\/p>\n<p>print(&quot;list1 \u5220\u9664 list2 \u4e2d\u7684\u5143\u7d20\u540e:&quot;, result1)<\/p>\n<p>print(&quot;list2 \u5220\u9664 list1 \u4e2d\u7684\u5143\u7d20\u540e:&quot;, result2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/h3>\n<\/p>\n<ol>\n<li><strong>\u96c6\u5408\u8f6c\u6362<\/strong>\uff1a\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u8fd9\u6837\u53ef\u4ee5\u5229\u7528\u96c6\u5408\u7684\u9ad8\u6548\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u5dee\u96c6\u64cd\u4f5c<\/strong>\uff1a\u901a\u8fc7\u51cf\u6cd5\u64cd\u4f5c\u83b7\u53d6\u5dee\u96c6\uff0c\u5373\u5728\u7b2c\u4e00\u4e2a\u96c6\u5408\u4e2d\u5220\u9664\u7b2c\u4e8c\u4e2a\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002<\/li>\n<\/ol>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u4e00\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8fc7\u6ee4\u6389\u4e0d\u9700\u8981\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e24\u4e2a\u5217\u8868<\/p>\n<p>list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4, 5, 6, 7]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fc7\u6ee4\u5143\u7d20<\/strong><\/h2>\n<p>result1 = [x for x in list1 if x not in list2]<\/p>\n<p>result2 = [x for x in list2 if x not in list1]<\/p>\n<p>print(&quot;list1 \u5220\u9664 list2 \u4e2d\u7684\u5143\u7d20\u540e:&quot;, result1)<\/p>\n<p>print(&quot;list2 \u5220\u9664 list1 \u4e2d\u7684\u5143\u7d20\u540e:&quot;, result2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u901a\u8fc7 <code>[x for x in list1 if x not in list2]<\/code> \u8fd9\u79cd\u5f62\u5f0f\uff0c\u53ef\u4ee5\u8fc7\u6ee4\u6389 <code>list1<\/code> \u4e2d\u5b58\u5728\u4e8e <code>list2<\/code> \u7684\u5143\u7d20\u3002<\/li>\n<li><strong>\u9ad8\u6548\u8fc7\u6ee4<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u4e8e\u5c0f\u89c4\u6a21\u5217\u8868\u975e\u5e38\u9ad8\u6548\uff0c\u4f46\u5bf9\u4e8e\u5927\u89c4\u6a21\u5217\u8868\u53ef\u80fd\u4f1a\u6709\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ol>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u6807\u51c6\u5faa\u73af\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u6807\u51c6\u7684\u5faa\u73af\u65b9\u6cd5\u662f\u6700\u76f4\u89c2\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u904d\u5386\u5217\u8868\u5e76\u624b\u52a8\u5220\u9664\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e24\u4e2a\u5217\u8868<\/p>\n<p>list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4, 5, 6, 7]<\/p>\n<h2><strong>\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\u5220\u9664\u5143\u7d20<\/strong><\/h2>\n<p>result1 = []<\/p>\n<p>for item in list1:<\/p>\n<p>    if item not in list2:<\/p>\n<p>        result1.append(item)<\/p>\n<p>result2 = []<\/p>\n<p>for item in list2:<\/p>\n<p>    if item not in list1:<\/p>\n<p>        result2.append(item)<\/p>\n<p>print(&quot;list1 \u5220\u9664 list2 \u4e2d\u7684\u5143\u7d20\u540e:&quot;, result1)<\/p>\n<p>print(&quot;list2 \u5220\u9664 list1 \u4e2d\u7684\u5143\u7d20\u540e:&quot;, result2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/h3>\n<\/p>\n<ol>\n<li><strong>\u904d\u5386\u5217\u8868<\/strong>\uff1a\u901a\u8fc7for\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u3002<\/li>\n<li><strong>\u6761\u4ef6\u5224\u65ad<\/strong>\uff1a\u5982\u679c\u5143\u7d20\u4e0d\u5728\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u7ed3\u679c\u5217\u8868\u4e2d\u3002<\/li>\n<\/ol>\n<p><h2>\u56db\u3001\u6027\u80fd\u5bf9\u6bd4\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<\/p>\n<p><h3>1\u3001\u96c6\u5408\u64cd\u4f5c\u7684\u9ad8\u6548\u6027<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u64cd\u4f5c\u5728\u5220\u9664\u5143\u7d20\u65f6\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u8fd9\u662f\u56e0\u4e3a\u96c6\u5408\u4f7f\u7528\u4e86\u54c8\u5e0c\u8868\u7ed3\u6784\u6765\u5b58\u50a8\u5143\u7d20\u3002\u56e0\u6b64\uff0c\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u96c6\u5408\u64cd\u4f5c\u662f\u6700\u4e3a\u9ad8\u6548\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u7b80\u6d01\u6027<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u5728\u8bed\u6cd5\u4e0a\u975e\u5e38\u7b80\u6d01\uff0c\u9002\u5408\u5904\u7406\u4e2d\u5c0f\u89c4\u6a21\u7684\u6570\u636e\u96c6\u3002\u7136\u800c\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n^2)\uff0c\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\u53ef\u80fd\u4f1a\u6709\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u6807\u51c6\u5faa\u73af\u7684\u7075\u6d3b\u6027<\/h3>\n<\/p>\n<p><p>\u6807\u51c6\u5faa\u73af\u65b9\u6cd5\u867d\u7136\u5728\u4ee3\u7801\u4e0a\u663e\u5f97\u5197\u957f\uff0c\u4f46\u5176\u7075\u6d3b\u6027\u4f7f\u5f97\u5b83\u53ef\u4ee5\u5904\u7406\u66f4\u591a\u590d\u6742\u7684\u6761\u4ef6\u548c\u903b\u8f91\u3002\u5728\u9700\u8981\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\u65f6\uff0c\u6807\u51c6\u5faa\u73af\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>4\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u89c4\u6a21\u548c\u5177\u4f53\u9700\u6c42\u3002\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u5efa\u8bae\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\uff1b\u5bf9\u4e8e\u4e2d\u5c0f\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u9009\u62e9\u5217\u8868\u63a8\u5bfc\u5f0f\uff1b\u5728\u5904\u7406\u590d\u6742\u903b\u8f91\u65f6\uff0c\u6807\u51c6\u5faa\u73af\u5219\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u6570\u636e\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4f9b\u9009\u62e9\uff0c\u5305\u62ec\u96c6\u5408\u64cd\u4f5c\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u6807\u51c6\u5faa\u73af\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u603b\u7684\u6765\u8bf4\uff0c<strong>\u96c6\u5408\u64cd\u4f5c\u662f\u6700\u4e3a\u9ad8\u6548\u7684\u9009\u62e9<\/strong>\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u5220\u9664\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u6216<code>del<\/code>\u8bed\u53e5\u3002<code>remove()<\/code>\u65b9\u6cd5\u4f1a\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\uff0c\u800c<code>del<\/code>\u8bed\u53e5\u53ef\u4ee5\u6839\u636e\u7d22\u5f15\u4f4d\u7f6e\u5220\u9664\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>my_list.remove(value)<\/code>\u4f1a\u5220\u9664\u503c\u4e3a<code>value<\/code>\u7684\u5143\u7d20\uff0c\u800c<code>del my_list[index]<\/code>\u5219\u4f1a\u5220\u9664\u7d22\u5f15\u4e3a<code>index<\/code>\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u4f7f\u7528\u96c6\u5408\u8fd0\u7b97\u7b26\u5982\u4f55\u9ad8\u6548\u5730\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u91cd\u590d\u6570\u636e\uff1f<\/strong><br \/>\u53ef\u4ee5\u5229\u7528Python\u7684\u96c6\u5408\uff08set\uff09\u7279\u6027\u6765\u9ad8\u6548\u5730\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u91cd\u590d\u6570\u636e\u3002\u5c06\u4e24\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u540e\uff0c\u4f7f\u7528\u96c6\u5408\u7684\u5dee\u96c6\u8fd0\u7b97\u7b26\uff08<code>-<\/code>\uff09\u53ef\u4ee5\u5feb\u901f\u627e\u51fa\u4e00\u4e2a\u5217\u8868\u4e2d\u4e0d\u5728\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>list(set(list1) - set(list2))<\/code>\u4f1a\u8fd4\u56de\u5728<code>list1<\/code>\u4e2d\u4f46\u4e0d\u5728<code>list2<\/code>\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u540c\u65f6\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u76f8\u540c\u5143\u7d20\uff1f<\/strong><br \/>\u82e5\u8981\u540c\u65f6\u4ece\u4e24\u4e2a\u5217\u8868\u4e2d\u5220\u9664\u76f8\u540c\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u65b0\u5217\u8868\u3002\u901a\u8fc7\u5bf9\u4e24\u4e2a\u5217\u8868\u8fdb\u884c\u904d\u5386\uff0c\u4fdd\u7559\u4e0d\u5728\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>list1 = [x for x in list1 if x not in list2]<\/code>\u4f1a\u66f4\u65b0<code>list1<\/code>\uff0c\u4f7f\u5176\u53ea\u5305\u542b\u4e0d\u5728<code>list2<\/code>\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u6807\u51c6\u7684\u5faa\u73af\u7b49\u65b9\u6cd5\uff0c\u786e\u4fdd\u5220\u9664\u64cd\u4f5c\u7684\u9ad8\u6548\u6027\u548c [&hellip;]","protected":false},"author":3,"featured_media":1133815,"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\/1133807"}],"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=1133807"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133807\/revisions"}],"predecessor-version":[{"id":1133817,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133807\/revisions\/1133817"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1133815"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1133807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1133807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1133807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}