{"id":1101539,"date":"2025-01-08T15:50:43","date_gmt":"2025-01-08T07:50:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101539.html"},"modified":"2025-01-08T15:50:46","modified_gmt":"2025-01-08T07:50:46","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e4%b8%a4%e4%b8%aalist%e6%98%af%e5%90%a6%e7%9b%b8%e5%90%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1101539.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u4e24\u4e2alist\u662f\u5426\u76f8\u540c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064254\/eb20b77c-9b08-4321-b4e7-d91511e41878.webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u4e24\u4e2alist\u662f\u5426\u76f8\u540c\" \/><\/p>\n<p><p> <strong>Python\u5224\u65ad\u4e24\u4e2alist\u662f\u5426\u76f8\u540c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528==\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u3001\u4f7f\u7528all()\u51fd\u6570\u3001\u4f7f\u7528collections.Counter\u3001\u4f7f\u7528sort()\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\u4f7f\u7528 == \u8fd0\u7b97\u7b26\u662f\u6700\u76f4\u63a5\u548c\u7b80\u5355\u7684\u5224\u65ad\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528==\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 == \u8fd0\u7b97\u7b26\u662f\u5224\u65ad\u4e24\u4e2a\u5217\u8868\u662f\u5426\u76f8\u540c\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u3002\u8fd9\u4e2a\u8fd0\u7b97\u7b26\u4f1a\u9010\u5143\u7d20\u6bd4\u8f83\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5982\u679c\u6240\u6709\u5143\u7d20\u90fd\u76f8\u540c\u4e14\u987a\u5e8f\u4e00\u81f4\uff0c\u5219\u8fd4\u56deTrue\uff0c\u5426\u5219\u8fd4\u56deFalse\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [1, 2, 3]<\/p>\n<p>if list1 == list2:<\/p>\n<p>    print(&quot;The lists are the same.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The lists are different.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0clist1\u548clist2\u5305\u542b\u76f8\u540c\u7684\u5143\u7d20\u5e76\u4e14\u987a\u5e8f\u4e00\u81f4\uff0c\u56e0\u6b64\u8f93\u51fa\u4e3a&quot;The lists are the same.&quot;\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u96c6\u5408\uff08set\uff09<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\uff08set\uff09\u662f\u65e0\u5e8f\u7684\uff0c\u5e76\u4e14\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u3002\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 == \u8fd0\u7b97\u7b26\u5224\u65ad\u4e24\u4e2a\u96c6\u5408\u662f\u5426\u76f8\u540c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5ffd\u7565\u5143\u7d20\u987a\u5e8f\u548c\u91cd\u590d\u5143\u7d20\u7684\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 3]<\/p>\n<p>list2 = [3, 2, 1]<\/p>\n<p>if set(list1) == set(list2):<\/p>\n<p>    print(&quot;The lists cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n the same elements.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The lists contain different elements.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u867d\u7136list1\u548clist2\u7684\u987a\u5e8f\u548c\u91cd\u590d\u5143\u7d20\u4e0d\u540c\uff0c\u4f46\u5b83\u4eec\u8f6c\u6362\u4e3a\u96c6\u5408\u540e\u5305\u542b\u76f8\u540c\u7684\u5143\u7d20\uff0c\u56e0\u6b64\u8f93\u51fa\u4e3a&quot;The lists contain the same elements.&quot;\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528all()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>all()\u51fd\u6570\u53ef\u4ee5\u4e0e\u5217\u8868\u751f\u6210\u5f0f\uff08list comprehension\uff09\u4e00\u8d77\u4f7f\u7528\uff0c\u9010\u5143\u7d20\u6bd4\u8f83\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u5982\u679c\u6240\u6709\u6bd4\u8f83\u7ed3\u679c\u90fd\u4e3aTrue\uff0c\u5219\u8fd4\u56deTrue\uff0c\u5426\u5219\u8fd4\u56deFalse\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [1, 2, 3]<\/p>\n<p>if len(list1) == len(list2) and all(list1[i] == list2[i] for i in range(len(list1))):<\/p>\n<p>    print(&quot;The lists are the same.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The lists are different.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u6bd4\u8f83\u4e24\u4e2a\u5217\u8868\u7684\u957f\u5ea6\uff0c\u5982\u679c\u957f\u5ea6\u76f8\u540c\uff0c\u5219\u4f7f\u7528all()\u51fd\u6570\u9010\u5143\u7d20\u6bd4\u8f83\u5b83\u4eec\u662f\u5426\u76f8\u540c\u3002\u7531\u4e8elist1\u548clist2\u5305\u542b\u76f8\u540c\u7684\u5143\u7d20\u5e76\u4e14\u987a\u5e8f\u4e00\u81f4\uff0c\u56e0\u6b64\u8f93\u51fa\u4e3a&quot;The lists are the same.&quot;\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528collections.Counter<\/h3>\n<\/p>\n<p><p>collections.Counter\u662f\u4e00\u4e2a\u5b57\u5178\u5b50\u7c7b\uff0c\u7528\u4e8e\u8ba1\u6570\u54c8\u5e0c\u5bf9\u8c61\u3002\u5c06\u5217\u8868\u8f6c\u6362\u4e3aCounter\u5bf9\u8c61\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 == \u8fd0\u7b97\u7b26\u5224\u65ad\u4e24\u4e2aCounter\u5bf9\u8c61\u662f\u5426\u76f8\u540c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5ffd\u7565\u5143\u7d20\u987a\u5e8f\u7684\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>list1 = [1, 2, 3, 3]<\/p>\n<p>list2 = [3, 2, 1, 3]<\/p>\n<p>if Counter(list1) == Counter(list2):<\/p>\n<p>    print(&quot;The lists contain the same elements with the same frequency.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The lists contain different elements or frequencies.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0clist1\u548clist2\u5305\u542b\u76f8\u540c\u7684\u5143\u7d20\u5e76\u4e14\u9891\u7387\u4e00\u81f4\uff0c\u56e0\u6b64\u8f93\u51fa\u4e3a&quot;The lists contain the same elements with the same frequency.&quot;\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528sort()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5c06\u4e24\u4e2a\u5217\u8868\u6392\u5e8f\u540e\u518d\u4f7f\u7528 == \u8fd0\u7b97\u7b26\u6bd4\u8f83\u5b83\u4eec\u662f\u5426\u76f8\u540c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5ffd\u7565\u5143\u7d20\u987a\u5e8f\u7684\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [3, 1, 2]<\/p>\n<p>list2 = [1, 2, 3]<\/p>\n<p>if sorted(list1) == sorted(list2):<\/p>\n<p>    print(&quot;The lists contain the same elements.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The lists contain different elements.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6392\u5e8f\u540e\u7684list1\u548clist2\u5305\u542b\u76f8\u540c\u7684\u5143\u7d20\uff0c\u56e0\u6b64\u8f93\u51fa\u4e3a&quot;The lists contain the same elements.&quot;\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u662fPython\u5224\u65ad\u4e24\u4e2a\u5217\u8868\u662f\u5426\u76f8\u540c\u7684\u51e0\u79cd\u5e38\u7528\u65b9\u6cd5\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u6cd5\u8fdb\u884c\u6bd4\u8f83\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528==\u8fd0\u7b97\u7b26<\/strong>\uff1a\u9002\u7528\u4e8e\u5143\u7d20\u987a\u5e8f\u5fc5\u987b\u4e00\u81f4\u7684\u6bd4\u8f83\u3002<\/li>\n<li><strong>\u4f7f\u7528\u96c6\u5408\uff08set\uff09<\/strong>\uff1a\u9002\u7528\u4e8e\u5ffd\u7565\u5143\u7d20\u987a\u5e8f\u548c\u91cd\u590d\u5143\u7d20\u7684\u6bd4\u8f83\u3002<\/li>\n<li><strong>\u4f7f\u7528all()\u51fd\u6570<\/strong>\uff1a\u9002\u7528\u4e8e\u5143\u7d20\u987a\u5e8f\u5fc5\u987b\u4e00\u81f4\u7684\u9010\u5143\u7d20\u6bd4\u8f83\u3002<\/li>\n<li><strong>\u4f7f\u7528collections.Counter<\/strong>\uff1a\u9002\u7528\u4e8e\u5ffd\u7565\u5143\u7d20\u987a\u5e8f\uff0c\u4f46\u8003\u8651\u5143\u7d20\u9891\u7387\u7684\u6bd4\u8f83\u3002<\/li>\n<li><strong>\u4f7f\u7528sort()\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u5ffd\u7565\u5143\u7d20\u987a\u5e8f\u7684\u6bd4\u8f83\u3002<\/li>\n<\/ol>\n<p><p>\u4e0d\u540c\u7684\u65b9\u6cd5\u6709\u4e0d\u540c\u7684\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u65b9\u6cd5\u8fdb\u884c\u5224\u65ad\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6bd4\u8f83\u4e24\u4e2a\u5217\u8868\u7684\u5185\u5bb9\u662f\u5426\u5b8c\u5168\u4e00\u81f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u76f4\u63a5\u6bd4\u8f83\u4e24\u4e2a\u5217\u8868\u3002\u8fd9\u4e2a\u8fd0\u7b97\u7b26\u4f1a\u68c0\u67e5\u4e24\u4e2a\u5217\u8868\u7684\u957f\u5ea6\u4ee5\u53ca\u5b83\u4eec\u5bf9\u5e94\u7684\u5143\u7d20\u662f\u5426\u76f8\u540c\u3002\u5982\u679c\u5217\u8868\u7684\u5143\u7d20\u548c\u987a\u5e8f\u90fd\u4e00\u81f4\uff0c\u5219\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">list1 = [1, 2, 3]\nlist2 = [1, 2, 3]\nresult = list1 == list2  # result\u4e3aTrue\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u679c\u5217\u8868\u5305\u542b\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8be5\u5982\u4f55\u5224\u65ad\u5b83\u4eec\u662f\u5426\u76f8\u7b49\uff1f<\/strong><br \/>\u5373\u4f7f\u5217\u8868\u4e2d\u5305\u542b\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u4ecd\u7136\u6709\u6548\u3002Python\u4f1a\u9010\u4e2a\u6bd4\u8f83\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u53ca\u5176\u7c7b\u578b\u3002\u5982\u679c\u7c7b\u578b\u4e0d\u5339\u914d\uff0c\u7ed3\u679c\u5c06\u4f1a\u662f<code>False<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">list1 = [1, &#39;two&#39;, 3.0]\nlist2 = [1, &#39;two&#39;, 3]\nresult = list1 == list2  # result\u4e3aFalse\uff0c\u56e0\u4e3a3.0\u4e0e3\u7c7b\u578b\u4e0d\u540c\n<\/code><\/pre>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5224\u65ad\u4e24\u4e2a\u5217\u8868\u662f\u5426\u76f8\u540c\uff0c\u800c\u4e0d\u4f9d\u8d56\u4e8e\u5143\u7d20\u987a\u5e8f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u8fdb\u884c\u65e0\u5e8f\u6bd4\u8f83\u3002\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u53ef\u4ee5\u5ffd\u7565\u5143\u7d20\u7684\u987a\u5e8f\u548c\u91cd\u590d\u9879\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">list1 = [1, 2, 3]\nlist2 = [3, 2, 1]\nresult = set(list1) == set(list2)  # result\u4e3aTrue\n<\/code><\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u4e22\u5931\u91cd\u590d\u5143\u7d20\u7684\u4fe1\u606f\u3002\u5982\u679c\u4f60\u9700\u8981\u8003\u8651\u5143\u7d20\u7684\u6570\u91cf\u548c\u987a\u5e8f\uff0c\u5efa\u8bae\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5224\u65ad\u4e24\u4e2alist\u662f\u5426\u76f8\u540c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528==\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u3001\u4f7f\u7528all() [&hellip;]","protected":false},"author":3,"featured_media":1101556,"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\/1101539"}],"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=1101539"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101539\/revisions"}],"predecessor-version":[{"id":1101558,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101539\/revisions\/1101558"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1101556"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1101539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1101539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1101539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}