{"id":1132900,"date":"2025-01-08T21:01:31","date_gmt":"2025-01-08T13:01:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132900.html"},"modified":"2025-01-08T21:01:34","modified_gmt":"2025-01-08T13:01:34","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e4%b8%a4%e4%b8%aa%e6%95%b0%e6%8d%ae%e6%98%af%e7%9b%b8%e7%ad%89%e7%9a%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1132900.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u4e24\u4e2a\u6570\u636e\u662f\u76f8\u7b49\u7684"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102323\/95e6333e-a516-4cc3-a1bd-a34b33c0a4a0.webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u4e24\u4e2a\u6570\u636e\u662f\u76f8\u7b49\u7684\" \/><\/p>\n<p><p> <strong>\u5224\u65ad\u4e24\u4e2a\u6570\u636e\u662f\u5426\u76f8\u7b49\u5728Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u8fd0\u7b97\u7b26\u3001\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u51fd\u6570\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u7b49\u4e8e\u8fd0\u7b97\u7b26<code>==<\/code>\u3001<code>is<\/code>\u8fd0\u7b97\u7b26\u3001<code>collections<\/code>\u6a21\u5757\u4e2d\u7684<code>Counter<\/code>\u7c7b\u3001<code>set<\/code>\u6570\u636e\u7ed3\u6784\u4ee5\u53ca<code>numpy<\/code>\u5e93\u7684<code>array_equal<\/code>\u51fd\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u7b49\u4e8e\u8fd0\u7b97\u7b26<code>==<\/code>\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u5b83\u68c0\u67e5\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u5982\u679c\u9700\u8981\u6bd4\u8f83\u5bf9\u8c61\u7684\u8eab\u4efd\uff08\u5373\u4e24\u4e2a\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528<code>is<\/code>\u8fd0\u7b97\u7b26\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u5982\u5217\u8868\u6216\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528<code>collections.Counter<\/code>\u6216<code>set<\/code>\u6765\u8fdb\u884c\u6bd4\u8f83\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u8bb2\u89e3\u6bcf\u79cd\u65b9\u6cd5\u7684\u5177\u4f53\u4f7f\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7b49\u4e8e\u8fd0\u7b97\u7b26<code>==<\/code><\/h3>\n<\/p>\n<p><p>\u7b49\u4e8e\u8fd0\u7b97\u7b26<code>==<\/code>\u662f\u5224\u65ad\u4e24\u4e2a\u6570\u636e\u662f\u5426\u76f8\u7b49\u7684\u6700\u57fa\u672c\u65b9\u6cd5\u3002\u5b83\u6bd4\u8f83\u7684\u662f\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\uff0c\u800c\u4e0d\u662f\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h4>1. \u6bd4\u8f83\u57fa\u672c\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u548c\u5e03\u5c14\u503c\u3002\u7b49\u4e8e\u8fd0\u7b97\u7b26<code>==<\/code>\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u8fd9\u4e9b\u7c7b\u578b\u7684\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 10<\/p>\n<p>print(a == b)  # \u8f93\u51fa: True<\/p>\n<p>x = 3.14<\/p>\n<p>y = 3.14<\/p>\n<p>print(x == y)  # \u8f93\u51fa: True<\/p>\n<p>str1 = &quot;hello&quot;<\/p>\n<p>str2 = &quot;hello&quot;<\/p>\n<p>print(str1 == str2)  # \u8f93\u51fa: True<\/p>\n<p>bool1 = True<\/p>\n<p>bool2 = True<\/p>\n<p>print(bool1 == bool2)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6bd4\u8f83\u590d\u6742\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u590d\u6742\u6570\u636e\u7c7b\u578b\u5305\u62ec\u5217\u8868\u3001\u5143\u7ec4\u548c\u5b57\u5178\u3002\u7b49\u4e8e\u8fd0\u7b97\u7b26<code>==<\/code>\u540c\u6837\u9002\u7528\u4e8e\u8fd9\u4e9b\u7c7b\u578b\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>print(list1 == list2)  # \u8f93\u51fa: True<\/p>\n<p>tuple1 = (1, 2, 3)<\/p>\n<p>tuple2 = (1, 2, 3)<\/p>\n<p>print(tuple1 == tuple2)  # \u8f93\u51fa: True<\/p>\n<p>dict1 = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>dict2 = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>print(dict1 == dict2)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>is<\/code>\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p><code>is<\/code>\u8fd0\u7b97\u7b26\u7528\u4e8e\u5224\u65ad\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5373\u5b83\u4eec\u662f\u5426\u5177\u6709\u76f8\u540c\u7684\u5185\u5b58\u5730\u5740\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u6bd4\u8f83\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u5b57\u7b26\u4e32\u3001\u5143\u7ec4\uff09\u548c\u5355\u4f8b\u5bf9\u8c61\uff08\u5982<code>None<\/code>\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = [1, 2, 3]<\/p>\n<p>b = [1, 2, 3]<\/p>\n<p>print(a is b)  # \u8f93\u51fa: False<\/p>\n<p>c = a<\/p>\n<p>print(a is c)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c<code>is<\/code>\u8fd0\u7b97\u7b26\u7684\u884c\u4e3a\u53ef\u80fd\u4e0e<code>==<\/code>\u76f8\u540c\uff0c\u4f46\u4e0d\u5efa\u8bae\u4f7f\u7528<code>is<\/code>\u6765\u6bd4\u8f83\u6570\u503c\u6216\u5b57\u7b26\u4e32\uff0c\u56e0\u4e3a\u8fd9\u4f9d\u8d56\u4e8ePython\u7684\u5185\u90e8\u4f18\u5316\u673a\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>collections.Counter<\/code>\u7c7b<\/h3>\n<\/p>\n<p><p><code>collections.Counter<\/code>\u7c7b\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u7edf\u8ba1\u53ef\u54c8\u5e0c\u5bf9\u8c61\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5217\u8868\u6216\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u76f8\u540c\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>list1 = [1, 2, 3, 4]<\/p>\n<p>list2 = [4, 3, 2, 1]<\/p>\n<p>print(Counter(list1) == Counter(list2))  # \u8f93\u51fa: True<\/p>\n<p>str1 = &quot;hello&quot;<\/p>\n<p>str2 = &quot;lleho&quot;<\/p>\n<p>print(Counter(str1) == Counter(str2))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>set<\/code>\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p><code>set<\/code>\u662fPython\u4e2d\u7684\u5185\u7f6e\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u96c6\u5408\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">set1 = {1, 2, 3}<\/p>\n<p>set2 = {3, 2, 1}<\/p>\n<p>print(set1 == set2)  # \u8f93\u51fa: True<\/p>\n<p>list1 = [1, 2, 3, 4]<\/p>\n<p>list2 = [4, 3, 2, 1]<\/p>\n<p>print(set(list1) == set(list2))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>numpy<\/code>\u5e93\u7684<code>array_equal<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>numpy<\/code>\u5e93\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u7684\u5f3a\u5927\u5de5\u5177\u3002<code>array_equal<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>arr1 = np.array([1, 2, 3])<\/p>\n<p>arr2 = np.array([1, 2, 3])<\/p>\n<p>print(np.array_equal(arr1, arr2))  # \u8f93\u51fa: True<\/p>\n<p>arr3 = np.array([1, 2, 4])<\/p>\n<p>print(np.array_equal(arr1, arr3))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5224\u65ad\u4e24\u4e2a\u6570\u636e\u662f\u5426\u76f8\u7b49\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5404\u6709\u4f18\u7f3a\u70b9\u3002\u5bf9\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u7b49\u4e8e\u8fd0\u7b97\u7b26<code>==<\/code>\u662f\u6700\u7b80\u4fbf\u548c\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u5f53\u9700\u8981\u6bd4\u8f83\u5bf9\u8c61\u7684\u8eab\u4efd\u65f6\uff0c\u4f7f\u7528<code>is<\/code>\u8fd0\u7b97\u7b26\u3002\u5bf9\u4e8e\u590d\u6742\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9<code>collections.Counter<\/code>\u3001<code>set<\/code>\u6216<code>numpy.array_equal<\/code>\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548\u5730\u5224\u65ad\u6570\u636e\u7684\u76f8\u7b49\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\u53d8\u91cf\u7684\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u4f8b\u5982\uff0c<code>a == b<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u5982\u679c<code>a<\/code>\u548c<code>b<\/code>\u7684\u503c\u76f8\u540c\uff0c\u5219\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002\u8fd9\u79cd\u6bd4\u8f83\u4e0d\u4ec5\u9002\u7528\u4e8e\u6570\u5b57\uff0c\u8fd8\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u3001\u5217\u8868\u548c\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5224\u65ad\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86<code>is<\/code>\u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u540c\u4e00\u4e2a\u5b9e\u4f8b\u3002<code>is<\/code>\u8fd0\u7b97\u7b26\u68c0\u67e5\u5bf9\u8c61\u7684\u8eab\u4efd\uff0c\u800c\u4e0d\u662f\u503c\u3002\u56e0\u6b64\uff0c<code>a is b<\/code>\u4ec5\u5728<code>a<\/code>\u548c<code>b<\/code>\u662f\u540c\u4e00\u5bf9\u8c61\u65f6\u8fd4\u56de<code>True<\/code>\u3002\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff0c\u53ef\u4ee5\u91cd\u5199<code>__eq__<\/code>\u65b9\u6cd5\u6765\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7684\u76f8\u7b49\u6027\u5224\u65ad\u903b\u8f91\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u6d6e\u70b9\u6570\u7684\u76f8\u7b49\u6bd4\u8f83\uff1f<\/strong><br \/>\u7531\u4e8e\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u95ee\u9898\uff0c\u76f4\u63a5\u4f7f\u7528<code>==<\/code>\u6bd4\u8f83\u4e24\u4e2a\u6d6e\u70b9\u6570\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002\u4e3a\u4e86\u5b89\u5168\u5730\u5224\u65ad\u4e24\u4e2a\u6d6e\u70b9\u6570\u662f\u5426\u76f8\u7b49\uff0c\u53ef\u4ee5\u4f7f\u7528<code>math.isclose()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u5141\u8bb8\u4f60\u8bbe\u5b9a\u4e00\u4e2a\u5bb9\u5fcd\u5ea6\uff0c\u53ea\u6709\u5728\u4e24\u4e2a\u6d6e\u70b9\u6570\u7684\u5dee\u5c0f\u4e8e\u6307\u5b9a\u7684\u5bb9\u5fcd\u5ea6\u65f6\uff0c\u624d\u8ba4\u4e3a\u5b83\u4eec\u76f8\u7b49\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u907f\u514d\u7531\u4e8e\u6d6e\u70b9\u6570\u8fd0\u7b97\u5f15\u8d77\u7684\u8bef\u5dee\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5224\u65ad\u4e24\u4e2a\u6570\u636e\u662f\u5426\u76f8\u7b49\u5728Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u8fd0\u7b97\u7b26\u3001\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u51fd\u6570\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u7b49\u4e8e\u8fd0\u7b97\u7b26 [&hellip;]","protected":false},"author":3,"featured_media":1132909,"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\/1132900"}],"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=1132900"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132900\/revisions"}],"predecessor-version":[{"id":1132912,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132900\/revisions\/1132912"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1132909"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1132900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1132900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1132900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}