{"id":1094208,"date":"2025-01-08T14:38:52","date_gmt":"2025-01-08T06:38:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094208.html"},"modified":"2025-01-08T14:38:55","modified_gmt":"2025-01-08T06:38:55","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e4%b8%a4%e4%b8%aalist%e7%9b%b8%e7%ad%89-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1094208.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u4e24\u4e2alist\u76f8\u7b49"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210430\/84163e7e-44b5-4495-820c-425764598e5f.webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u4e24\u4e2alist\u76f8\u7b49\" \/><\/p>\n<p><p> <strong>Python\u5224\u65ad\u4e24\u4e2alist\u662f\u5426\u76f8\u7b49\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\u76f4\u63a5\u4f7f\u7528 <code>==<\/code> \u8fd0\u7b97\u7b26\u3001\u4f7f\u7528 <code>collections.Counter<\/code> \u65b9\u6cd5\u3001\u4ee5\u53ca\u4f7f\u7528 <code>all()<\/code> \u65b9\u6cd5\u8fdb\u884c\u5143\u7d20\u6bd4\u8f83\u3002\u6700\u5e38\u7528\u548c\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 <code>==<\/code> \u8fd0\u7b97\u7b26\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u6bd4\u8f83\u957f\u5ea6\uff0c\u4e5f\u4f1a\u6bd4\u8f83\u5143\u7d20\u548c\u987a\u5e8f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u76f4\u63a5\u4f7f\u7528 <code>==<\/code> \u8fd0\u7b97\u7b26<\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>==<\/code> \u8fd0\u7b97\u7b26\u662f\u6700\u7b80\u5355\u548c\u6700\u76f4\u89c2\u7684\u65b9\u6cd5\uff0c\u5b83\u4f1a\u6bd4\u8f83\u4e24\u4e2a\u5217\u8868\u7684\u957f\u5ea6\u3001\u5143\u7d20\u548c\u987a\u5e8f\u662f\u5426\u5b8c\u5168\u4e00\u81f4\u3002\u5982\u679c\u4e24\u4e2a\u5217\u8868\u76f8\u7b49\uff0c\u5219\u8fd4\u56de <code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de <code>False<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [1, 2, 3]<\/p>\n<p>list3 = [3, 2, 1]<\/p>\n<p>print(list1 == list2)  # True<\/p>\n<p>print(list1 == list3)  # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u6d01\u660e\u4e86\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\u3002\u6b64\u5916\uff0c\u5b83\u7684\u6267\u884c\u6548\u7387\u4e5f\u975e\u5e38\u9ad8\uff0c\u56e0\u4e3a <code>==<\/code> \u8fd0\u7b97\u7b26\u662f\u7531 Python \u7684\u5e95\u5c42\u5b9e\u73b0\u7684\uff0c\u80fd\u591f\u5feb\u901f\u5224\u65ad\u4e24\u4e2a\u5217\u8868\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528 <code>collections.Counter<\/code> \u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>collections.Counter<\/code> \u662f\u4e00\u4e2a\u5b57\u5178\u5b50\u7c7b\uff0c\u7528\u4e8e\u8ba1\u6570\u53ef\u6563\u5217\u5bf9\u8c61\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>Counter<\/code> \u65b9\u6cd5\u5c06\u4e24\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u8ba1\u6570\u5668\u5bf9\u8c61\uff0c\u7136\u540e\u6bd4\u8f83\u8fd9\u4e24\u4e2a\u8ba1\u6570\u5668\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\u3002\u6b64\u65b9\u6cd5\u9002\u7528\u4e8e\u5224\u65ad\u4e24\u4e2a\u5217\u8868\u5143\u7d20\u76f8\u540c\u4f46\u987a\u5e8f\u4e0d\u540c\u65f6\u662f\u5426\u76f8\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [3, 2, 1]<\/p>\n<p>print(Counter(list1) == Counter(list2))  # True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u80fd\u591f\u5224\u65ad\u4e24\u4e2a\u5217\u8868\u5143\u7d20\u76f8\u540c\u4f46\u987a\u5e8f\u4e0d\u540c\u7684\u60c5\u51b5\u3002\u7f3a\u70b9\u662f <code>Counter<\/code> \u65b9\u6cd5\u4f1a\u5ffd\u7565\u5143\u7d20\u7684\u987a\u5e8f\uff0c\u56e0\u6b64\u5982\u679c\u987a\u5e8f\u5f88\u91cd\u8981\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u4e0d\u9002\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528 <code>all()<\/code> \u65b9\u6cd5\u8fdb\u884c\u5143\u7d20\u6bd4\u8f83<\/p>\n<\/p>\n<p><p><code>all()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>all()<\/code> \u65b9\u6cd5\u548c <code>zip()<\/code> \u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868\u7684\u5143\u7d20\u9010\u4e2a\u914d\u5bf9\u8fdb\u884c\u6bd4\u8f83\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [1, 2, 3]<\/p>\n<p>print(all(x == y for x, y in zip(list1, list2)))  # True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6bd4\u8f83\u6761\u4ef6\u3002\u7f3a\u70b9\u662f\u4ee3\u7801\u8f83\u4e3a\u590d\u6742\uff0c\u4e0d\u5982 <code>==<\/code> \u8fd0\u7b97\u7b26\u76f4\u89c2\u6613\u61c2\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5177\u4f53\u5b9e\u73b0\u6848\u4f8b<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u6bb5\u5177\u4f53\u5b9e\u73b0\u6848\u4f8b\uff0c\u7ed3\u5408\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\uff0c\u5224\u65ad\u4e24\u4e2a\u5217\u8868\u662f\u5426\u76f8\u7b49\uff0c\u5e76\u7ed9\u51fa\u8be6\u7ec6\u7684\u8f93\u51fa\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def are_lists_equal(list1, list2):<\/p>\n<p>    # \u4f7f\u7528 `==` \u8fd0\u7b97\u7b26\u8fdb\u884c\u6bd4\u8f83<\/p>\n<p>    if list1 == list2:<\/p>\n<p>        return &quot;\u5217\u8868\u76f8\u7b49\uff08\u4f7f\u7528 == \u8fd0\u7b97\u7b26\uff09&quot;<\/p>\n<p>    # \u4f7f\u7528 `Counter` \u65b9\u6cd5\u8fdb\u884c\u6bd4\u8f83<\/p>\n<p>    if Counter(list1) == Counter(list2):<\/p>\n<p>        return &quot;\u5217\u8868\u76f8\u7b49\uff08\u4f7f\u7528 collections.Counter \u65b9\u6cd5\uff09&quot;<\/p>\n<p>    # \u4f7f\u7528 `all()` \u65b9\u6cd5\u8fdb\u884c\u5143\u7d20\u6bd4\u8f83<\/p>\n<p>    if all(x == y for x, y in zip(list1, list2)):<\/p>\n<p>        return &quot;\u5217\u8868\u76f8\u7b49\uff08\u4f7f\u7528 all() \u65b9\u6cd5\uff09&quot;<\/p>\n<p>    return &quot;\u5217\u8868\u4e0d\u76f8\u7b49&quot;<\/p>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [1, 2, 3]<\/p>\n<p>list3 = [3, 2, 1]<\/p>\n<p>print(are_lists_equal(list1, list2))  # \u5217\u8868\u76f8\u7b49\uff08\u4f7f\u7528 == \u8fd0\u7b97\u7b26\uff09<\/p>\n<p>print(are_lists_equal(list1, list3))  # \u5217\u8868\u76f8\u7b49\uff08\u4f7f\u7528 collections.Counter \u65b9\u6cd5\uff09<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5168\u9762\u5224\u65ad\u4e24\u4e2a\u5217\u8868\u662f\u5426\u76f8\u7b49\uff0c\u5e76\u6839\u636e\u4e0d\u540c\u7684\u65b9\u6cd5\u7ed9\u51fa\u8be6\u7ec6\u7684\u8f93\u51fa\u4fe1\u606f\u3002\u8fd9\u79cd\u5b9e\u73b0\u65b9\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\uff0c\u8fd8\u53ef\u4ee5\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7075\u6d3b\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6027\u80fd\u548c\u9002\u7528\u573a\u666f\u5206\u6790<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5224\u65ad\u4e24\u4e2a\u5217\u8868\u662f\u5426\u76f8\u7b49\u7684\u65b9\u6cd5\u65f6\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\u6027\u80fd\u548c\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u5728\u6027\u80fd\u548c\u9002\u7528\u573a\u666f\u4e0a\u5404\u6709\u4f18\u52a3\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528 <code>==<\/code> \u8fd0\u7b97\u7b26<\/strong>\uff1a\u9002\u7528\u4e8e\u5217\u8868\u957f\u5ea6\u8f83\u77ed\u3001\u987a\u5e8f\u8f83\u91cd\u8981\u7684\u573a\u666f\u3002\u8be5\u65b9\u6cd5\u6267\u884c\u6548\u7387\u9ad8\uff0c\u9002\u5408\u5927\u591a\u6570\u5e38\u89c1\u60c5\u51b5\u3002<\/li>\n<li><strong>\u4f7f\u7528 <code>collections.Counter<\/code> \u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u5217\u8868\u957f\u5ea6\u8f83\u957f\u3001\u5143\u7d20\u76f8\u540c\u4f46\u987a\u5e8f\u4e0d\u91cd\u8981\u7684\u573a\u666f\u3002\u8be5\u65b9\u6cd5\u5ffd\u7565\u987a\u5e8f\uff0c\u9002\u5408\u9700\u8981\u5224\u65ad\u5143\u7d20\u662f\u5426\u5b8c\u5168\u4e00\u81f4\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u4f7f\u7528 <code>all()<\/code> \u65b9\u6cd5\u8fdb\u884c\u5143\u7d20\u6bd4\u8f83<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u81ea\u5b9a\u4e49\u6bd4\u8f83\u6761\u4ef6\u7684\u573a\u666f\u3002\u8be5\u65b9\u6cd5\u7075\u6d3b\u6027\u9ad8\uff0c\u4f46\u6267\u884c\u6548\u7387\u76f8\u5bf9\u8f83\u4f4e\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7efc\u5408\u8003\u8651\u6027\u80fd\u548c\u9002\u7528\u573a\u666f\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5224\u65ad\u4e24\u4e2a\u5217\u8868\u662f\u5426\u76f8\u7b49\u3002\u5e0c\u671b\u672c\u6587\u7684\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u6027\u80fd\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\u6bd4\u8f83\u4e24\u4e2a\u5217\u8868\u7684\u5185\u5bb9\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\u79cd\u65b9\u6cd5\u4f1a\u68c0\u67e5\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u5426\u76f8\u540c\u4e14\u987a\u5e8f\u4e00\u81f4\u3002\u5982\u679c\u9700\u8981\u6bd4\u8f83\u4e0d\u8003\u8651\u987a\u5e8f\u7684\u76f8\u7b49\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u8fdb\u884c\u6bd4\u8f83\uff0c\u4e0d\u8fc7\u8fd9\u4f1a\u5ffd\u7565\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6bd4\u8f83\u4e24\u4e2a\u5217\u8868\u7684\u5143\u7d20\u76f8\u7b49\u6027\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\u6765\u6bd4\u8f83\u5217\u8868\u3002\u53ef\u4ee5\u4f7f\u7528<code>all()<\/code>\u51fd\u6570\u4e0e\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7ed3\u5408\uff0c\u9010\u4e2a\u6bd4\u8f83\u5143\u7d20\u3002\u6b64\u5916\uff0c<code>collections.Counter<\/code>\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5217\u8868\u7684\u5143\u7d20\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u8003\u8651\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<p><strong>\u5982\u679c\u4e24\u4e2a\u5217\u8868\u5305\u542b\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5982\u4f55\u5904\u7406\u6bd4\u8f83\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6bd4\u8f83\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u5143\u7d20\u65f6\uff0c\u76f4\u63a5\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u7ed3\u679c\u3002\u4e3a\u4e86\u786e\u4fdd\u6bd4\u8f83\u7684\u51c6\u786e\u6027\uff0c\u53ef\u4ee5\u5728\u6bd4\u8f83\u524d\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u548c\u8f6c\u6362\uff0c\u786e\u4fdd\u5b83\u4eec\u662f\u76f8\u540c\u7c7b\u578b\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u56e0\u7c7b\u578b\u4e0d\u5339\u914d\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u6bd4\u8f83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5224\u65ad\u4e24\u4e2alist\u662f\u5426\u76f8\u7b49\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\u76f4\u63a5\u4f7f\u7528 == \u8fd0\u7b97\u7b26\u3001\u4f7f\u7528 collections [&hellip;]","protected":false},"author":3,"featured_media":1094215,"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\/1094208"}],"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=1094208"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094208\/revisions"}],"predecessor-version":[{"id":1094216,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094208\/revisions\/1094216"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094215"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}