{"id":971368,"date":"2024-12-27T05:34:19","date_gmt":"2024-12-26T21:34:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/971368.html"},"modified":"2024-12-27T05:34:22","modified_gmt":"2024-12-26T21:34:22","slug":"python%e5%88%97%e8%a1%a8%e5%a6%82%e4%bd%95%e5%af%b9%e6%af%94%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/971368.html","title":{"rendered":"python\u5217\u8868\u5982\u4f55\u5bf9\u6bd4\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194526\/5f910f3f-f623-4a5a-b2e2-7e409a27b68e.webp\" alt=\"python\u5217\u8868\u5982\u4f55\u5bf9\u6bd4\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>Python\u5217\u8868\u5bf9\u6bd4\u5185\u5bb9\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5bf9\u6bd4\u3001\u4f7f\u7528\u96c6\u5408(set)\u5bf9\u6bd4\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5bf9\u6bd4\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570(all\u3001any)\u5bf9\u6bd4\u3001\u4f7f\u7528NumPy\u5e93\u5bf9\u6bd4<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\u5bf9\u6bd4\u662f\u6700\u4e3a\u9ad8\u6548\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u96c6\u5408\u53ef\u4ee5\u5feb\u901f\u53bb\u91cd\u548c\u67e5\u627e\u4ea4\u96c6\u3001\u5e76\u96c6\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\u5bf9\u6bd4\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u5229\u7528\u96c6\u5408\u7684\u8fd0\u7b97\u6765\u5bf9\u6bd4\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u68c0\u67e5\u4e24\u4e2a\u5217\u8868\u662f\u5426\u542b\u6709\u76f8\u540c\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\u96c6\u5408\u540e\uff0c\u68c0\u67e5\u5b83\u4eec\u7684\u4ea4\u96c6\u662f\u5426\u4e3a\u7a7a\u3002\u5982\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 4]<\/p>\n<p>list2 = [3, 4, 5, 6]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408<\/strong><\/h2>\n<p>set1 = set(list1)<\/p>\n<p>set2 = set(list2)<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u7684\u4ea4\u96c6\u65b9\u6cd5\u5bf9\u6bd4<\/strong><\/h2>\n<p>common_elements = set1.intersection(set2)<\/p>\n<p>if common_elements:<\/p>\n<p>    print(&quot;\u4e24\u4e2a\u5217\u8868\u6709\u5171\u540c\u7684\u5143\u7d20:&quot;, common_elements)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u4e24\u4e2a\u5217\u8868\u6ca1\u6709\u5171\u540c\u7684\u5143\u7d20&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\uff0c<strong>\u96c6\u5408\u6570\u636e\u7ed3\u6784\u7684\u67e5\u627e\u548c\u63d2\u5165\u590d\u6742\u5ea6\u4e3aO(1)<\/strong>\uff0c\u56e0\u6b64\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u6548\u7387\u8f83\u9ad8\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5faa\u73af\u904d\u5386\u5bf9\u6bd4<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5bf9\u6bd4\u4e24\u4e2a\u5217\u8868\u7684\u5185\u5bb9\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u61c2\uff0c\u9002\u5408\u5c0f\u89c4\u6a21\u6570\u636e\u7684\u6bd4\u8f83\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u5faa\u73af\u5bf9\u6bd4\u4e24\u4e2a\u5217\u8868\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u6bd4\u8f83\u5b83\u4eec\u662f\u5426\u76f8\u7b49\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u5217\u8868list_a\u548clist_b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u6765\u5bf9\u6bd4\u5b83\u4eec\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_a = [1, 2, 3, 4]<\/p>\n<p>list_b = [2, 3, 4, 5]<\/p>\n<p>common_elements = []<\/p>\n<p>for item in list_a:<\/p>\n<p>    if item in list_b:<\/p>\n<p>        common_elements.append(item)<\/p>\n<p>print(&quot;\u5171\u540c\u5143\u7d20:&quot;, common_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u4e86list_a\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u68c0\u67e5\u5b83\u662f\u5426\u5b58\u5728\u4e8elist_b\u4e2d\u3002\u5982\u679c\u5b58\u5728\uff0c\u6211\u4eec\u5c06\u5176\u6dfb\u52a0\u5230common_elements\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u8f93\u51fa\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u5171\u540c\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\uff0c\u56e0\u4e3a\u6bcf\u6b21\u68c0\u67e5\u90fd\u9700\u8981\u904d\u5386\u6574\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u96c6\u5408\u5bf9\u6bd4<\/p>\n<\/p>\n<p><p>\u96c6\u5408\u662fPython\u4e2d\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u652f\u6301\u6570\u5b66\u96c6\u5408\u4e2d\u7684\u5404\u79cd\u8fd0\u7b97\uff0c\u5982\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6\u7b49\u3002\u4f7f\u7528\u96c6\u5408\u5bf9\u6bd4\u5217\u8868\u7684\u5185\u5bb9\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u68c0\u67e5\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u5171\u540c\u5143\u7d20\u6216\u5dee\u5f02\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u4f7f\u7528\u96c6\u5408\u7684\u8fd0\u7b97\u65b9\u6cd5\u6765\u5bf9\u6bd4\u5185\u5bb9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u96c6\u5408\u5bf9\u6bd4\u4e24\u4e2a\u5217\u8868\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_a = [1, 2, 3, 4]<\/p>\n<p>list_b = [3, 4, 5, 6]<\/p>\n<p>set_a = set(list_a)<\/p>\n<p>set_b = set(list_b)<\/p>\n<h2><strong>\u8ba1\u7b97\u4ea4\u96c6<\/strong><\/h2>\n<p>intersection = set_a &amp; set_b<\/p>\n<p>print(&quot;\u4ea4\u96c6:&quot;, intersection)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e76\u96c6<\/strong><\/h2>\n<p>union = set_a | set_b<\/p>\n<p>print(&quot;\u5e76\u96c6:&quot;, union)<\/p>\n<h2><strong>\u8ba1\u7b97\u5dee\u96c6<\/strong><\/h2>\n<p>difference = set_a - set_b<\/p>\n<p>print(&quot;\u5dee\u96c6:&quot;, difference)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u4e24\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u4f7f\u7528\u96c6\u5408\u7684\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6\u8fd0\u7b97\u6765\u5bf9\u6bd4\u5185\u5bb9\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u6548\u7387\u8f83\u9ad8\uff0c\u56e0\u4e3a\u96c6\u5408\u7684\u67e5\u627e\u548c\u63d2\u5165\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5bf9\u6bd4<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u7684\u4e00\u79cd\u7b80\u6d01\u5199\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5bf9\u6bd4\u4e24\u4e2a\u5217\u8868\u7684\u5185\u5bb9\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u5305\u542b\u5171\u540c\u5143\u7d20\u6216\u5dee\u5f02\u5143\u7d20\u7684\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u5217\u8868list_a\u548clist_b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u5bf9\u6bd4\u5b83\u4eec\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_a = [1, 2, 3, 4]<\/p>\n<p>list_b = [3, 4, 5, 6]<\/p>\n<h2><strong>\u627e\u51fa\u5171\u540c\u5143\u7d20<\/strong><\/h2>\n<p>common_elements = [item for item in list_a if item in list_b]<\/p>\n<p>print(&quot;\u5171\u540c\u5143\u7d20:&quot;, common_elements)<\/p>\n<h2><strong>\u627e\u51falist_a\u4e2d\u72ec\u6709\u7684\u5143\u7d20<\/strong><\/h2>\n<p>unique_to_a = [item for item in list_a if item not in list_b]<\/p>\n<p>print(&quot;list_a\u72ec\u6709\u5143\u7d20:&quot;, unique_to_a)<\/p>\n<h2><strong>\u627e\u51falist_b\u4e2d\u72ec\u6709\u7684\u5143\u7d20<\/strong><\/h2>\n<p>unique_to_b = [item for item in list_b if item not in list_a]<\/p>\n<p>print(&quot;list_b\u72ec\u6709\u5143\u7d20:&quot;, unique_to_b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e09\u4e2a\u65b0\u7684\u5217\u8868\uff1acommon_elements\u5305\u542b\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u5171\u540c\u5143\u7d20\uff0cunique_to_a\u5305\u542blist_a\u4e2d\u72ec\u6709\u7684\u5143\u7d20\uff0cunique_to_b\u5305\u542blist_b\u4e2d\u72ec\u6709\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u6e05\u6670\uff0c\u9002\u5408\u5904\u7406\u4e2d\u5c0f\u89c4\u6a21\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u5bf9\u6bd4<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u5982all()\u548cany()\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5bf9\u6bd4\u5217\u8868\u7684\u5185\u5bb9\u3002\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u68c0\u67e5\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u5426\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u5217\u8868list_a\u548clist_b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528all()\u548cany()\u51fd\u6570\u6765\u5bf9\u6bd4\u5b83\u4eec\u7684\u5185\u5bb9\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_a = [1, 2, 3, 4]<\/p>\n<p>list_b = [3, 4, 5, 6]<\/p>\n<h2><strong>\u68c0\u67e5list_a\u4e2d\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u5728list_b\u4e2d<\/strong><\/h2>\n<p>all_in_b = all(item in list_b for item in list_a)<\/p>\n<p>print(&quot;list_a\u7684\u6240\u6709\u5143\u7d20\u90fd\u5728list_b\u4e2d:&quot;, all_in_b)<\/p>\n<h2><strong>\u68c0\u67e5list_a\u4e2d\u662f\u5426\u5b58\u5728\u4efb\u4f55\u4e00\u4e2a\u5143\u7d20\u5728list_b\u4e2d<\/strong><\/h2>\n<p>any_in_b = any(item in list_b for item in list_a)<\/p>\n<p>print(&quot;list_a\u4e2d\u6709\u5143\u7d20\u5728list_b\u4e2d:&quot;, any_in_b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528all()\u51fd\u6570\u68c0\u67e5list_a\u4e2d\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u5728list_b\u4e2d\uff0c\u4f7f\u7528any()\u51fd\u6570\u68c0\u67e5list_a\u4e2d\u662f\u5426\u5b58\u5728\u4efb\u4f55\u4e00\u4e2a\u5143\u7d20\u5728list_b\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4f7f\u7528\u4e86Python\u5185\u7f6e\u51fd\u6570\uff0c\u4ee3\u7801\u7b80\u6d01\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528NumPy\u5e93\u5bf9\u6bd4<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u6570\u7ec4\u8fd0\u7b97\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u6765\u5bf9\u6bd4\u4e24\u4e2a\u5217\u8868\u7684\u5185\u5bb9\uff0c\u7279\u522b\u662f\u5f53\u5217\u8868\u5305\u542b\u6570\u503c\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u5bf9\u6bd4\u4e24\u4e2a\u5217\u8868\u7684\u5185\u5bb9\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>list_a = [1, 2, 3, 4]<\/p>\n<p>list_b = [3, 4, 5, 6]<\/p>\n<p>array_a = np.array(list_a)<\/p>\n<p>array_b = np.array(list_b)<\/p>\n<h2><strong>\u627e\u51fa\u5171\u540c\u5143\u7d20<\/strong><\/h2>\n<p>common_elements = np.intersect1d(array_a, array_b)<\/p>\n<p>print(&quot;\u5171\u540c\u5143\u7d20:&quot;, common_elements)<\/p>\n<h2><strong>\u627e\u51falist_a\u4e2d\u72ec\u6709\u7684\u5143\u7d20<\/strong><\/h2>\n<p>unique_to_a = np.setdiff1d(array_a, array_b)<\/p>\n<p>print(&quot;list_a\u72ec\u6709\u5143\u7d20:&quot;, unique_to_a)<\/p>\n<h2><strong>\u627e\u51falist_b\u4e2d\u72ec\u6709\u7684\u5143\u7d20<\/strong><\/h2>\n<p>unique_to_b = np.setdiff1d(array_b, array_a)<\/p>\n<p>print(&quot;list_b\u72ec\u6709\u5143\u7d20:&quot;, unique_to_b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528NumPy\u63d0\u4f9b\u7684intersect1d()\u548csetdiff1d()\u51fd\u6570\u6765\u5bf9\u6bd4\u5185\u5bb9\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u6548\u7387\u9ad8\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u7684\u6570\u503c\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5224\u65ad\u4e24\u4e2aPython\u5217\u8868\u662f\u5426\u76f8\u7b49\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u76f4\u63a5\u5224\u65ad\u4e24\u4e2a\u5217\u8868\u7684\u5185\u5bb9\u662f\u5426\u76f8\u7b49\u3002\u5982\u679c\u4e24\u4e2a\u5217\u8868\u7684\u5143\u7d20\u548c\u987a\u5e8f\u5b8c\u5168\u76f8\u540c\uff0c\u5219\u8fd4\u56deTrue\uff1b\u5426\u5219\u8fd4\u56deFalse\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">list1 = [1, 2, 3]\nlist2 = [1, 2, 3]\nprint(list1 == list2)  # \u8f93\u51fa: True\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u6bd4\u8f83\u5217\u8868\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u5916\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528<code>set()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u8fdb\u884c\u6bd4\u8f83\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u96c6\u5408\u4e0d\u8003\u8651\u5143\u7d20\u7684\u987a\u5e8f\u4e14\u4e0d\u5141\u8bb8\u91cd\u590d\u3002\u56e0\u6b64\uff0c\u5982\u679c\u9700\u8981\u68c0\u67e5\u4e24\u4e2a\u5217\u8868\u662f\u5426\u5305\u542b\u76f8\u540c\u7684\u5143\u7d20\u800c\u4e0d\u8003\u8651\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">list1 = [1, 2, 3]\nlist2 = [3, 2, 1]\nprint(set(list1) == set(list2))  # \u8f93\u51fa: True\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u627e\u51fa\u4e24\u4e2aPython\u5217\u8868\u4e4b\u95f4\u7684\u5dee\u5f02\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u627e\u51fa\u4e24\u4e2a\u5217\u8868\u4e4b\u95f4\u7684\u5dee\u5f02\u3002\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u53ef\u4ee5\u4f7f\u7528<code>difference()<\/code>\u65b9\u6cd5\u6216<code>-<\/code>\u8fd0\u7b97\u7b26\u6765\u83b7\u53d6\u53ea\u5b58\u5728\u4e8e\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">list1 = [1, 2, 3, 4]\nlist2 = [3, 4, 5, 6]\ndiff = set(list1).difference(set(list2))\nprint(diff)  # \u8f93\u51fa: {1, 2}\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4e5f\u53ef\u4ee5\u627e\u51fa\u5dee\u5f02\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5217\u8868\u5bf9\u6bd4\u5185\u5bb9\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5bf9\u6bd4\u3001\u4f7f\u7528\u96c6\u5408(set)\u5bf9\u6bd4\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5bf9\u6bd4\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570( [&hellip;]","protected":false},"author":3,"featured_media":971376,"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\/971368"}],"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=971368"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/971368\/revisions"}],"predecessor-version":[{"id":971380,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/971368\/revisions\/971380"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/971376"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=971368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=971368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=971368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}