{"id":996403,"date":"2024-12-27T09:14:21","date_gmt":"2024-12-27T01:14:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/996403.html"},"modified":"2024-12-27T09:14:23","modified_gmt":"2024-12-27T01:14:23","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e6%98%af%e5%90%a6%e7%ad%89%e4%ba%8e","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/996403.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u662f\u5426\u7b49\u4e8e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072650\/0479f7e1-0f44-4a26-85ea-c68a8cbbf7f8.webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u662f\u5426\u7b49\u4e8e\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5224\u65ad\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49\uff0c\u901a\u5e38\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u3001<code>is<\/code>\u5173\u952e\u5b57\u4ee5\u53ca\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570\u3002<code>==<\/code>\u7528\u4e8e\u6bd4\u8f83\u503c\u662f\u5426\u76f8\u7b49\uff0c<code>is<\/code>\u7528\u4e8e\u6bd4\u8f83\u5bf9\u8c61\u7684\u8eab\u4efd\uff0c\u800c\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u5bf9\u8c61\u6bd4\u8f83\u3002<\/strong><code>==<\/code>\u8fd0\u7b97\u7b26\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u5b83\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u540c\uff0c\u800c\u4e0d\u662f\u5b83\u4eec\u7684\u5185\u5b58\u5730\u5740\u3002<code>is<\/code>\u5173\u952e\u5b57\u7528\u4e8e\u68c0\u67e5\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5373\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u662f\u5426\u76f8\u540c\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u80fd\u9700\u8981\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9a\u4e49\u5bf9\u8c61\u6bd4\u8f83\u7684\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8be6\u7ec6\u63cf\u8ff0\u4e2d\uff0c<strong><code>==<\/code>\u8fd0\u7b97\u7b26<\/strong>\u662f\u5224\u65ad\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49\u7684\u6700\u5e38\u7528\u65b9\u6cd5\u3002\u5b83\u9002\u7528\u4e8e\u5927\u591a\u6570\u5185\u7f6e\u6570\u636e\u7c7b\u578b\uff0c\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u7b49\u3002\u5f53\u4f7f\u7528<code>==<\/code>\u65f6\uff0cPython\u4f1a\u8c03\u7528\u5bf9\u8c61\u7684<code>__eq__<\/code>\u65b9\u6cd5\uff0c\u56e0\u6b64\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6bd4\u8f83\u884c\u4e3a\u3002\u4f8b\u5982\uff0c\u4e24\u4e2a\u5b57\u7b26\u4e32<code>&quot;hello&quot;<\/code>\u548c<code>&quot;hello&quot;<\/code>\u5728\u503c\u4e0a\u662f\u76f8\u7b49\u7684\uff0c\u6240\u4ee5<code>&quot;hello&quot; == &quot;hello&quot;<\/code>\u4f1a\u8fd4\u56de<code>True<\/code>\u3002\u7136\u800c\uff0c<code>==<\/code>\u5e76\u4e0d\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u662f\u540c\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u800c\u53ea\u6bd4\u8f83\u5b83\u4eec\u7684\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u5e38\u89c1\u7684\u5224\u65ad\u662f\u5426\u76f8\u7b49\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u3002\u8fd9\u4e2a\u8fd0\u7b97\u7b26\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5185\u7f6e\u6570\u636e\u7c7b\u578b\uff0c\u8fd9\u79cd\u65b9\u6cd5\u662f\u975e\u5e38\u76f4\u89c2\u548c\u9ad8\u6548\u7684\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u6bd4\u8f83<\/strong><\/li>\n<\/ol>\n<p><p>Python\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5e03\u5c14\u503c\u7b49\u3002\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u53ef\u4ee5\u65b9\u4fbf\u5730\u6bd4\u8f83\u8fd9\u4e9b\u7c7b\u578b\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6bd4\u8f83\u6574\u6570<\/p>\n<p>a = 10<\/p>\n<p>b = 10<\/p>\n<p>print(a == b)  # \u8f93\u51fa: True<\/p>\n<h2><strong>\u6bd4\u8f83\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>c = 3.14<\/p>\n<p>d = 3.14<\/p>\n<p>print(c == d)  # \u8f93\u51fa: True<\/p>\n<h2><strong>\u6bd4\u8f83\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>e = &quot;hello&quot;<\/p>\n<p>f = &quot;world&quot;<\/p>\n<p>print(e == f)  # \u8f93\u51fa: False<\/p>\n<h2><strong>\u6bd4\u8f83\u5e03\u5c14\u503c<\/strong><\/h2>\n<p>g = True<\/p>\n<p>h = False<\/p>\n<p>print(g == h)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>==<\/code>\u8fd0\u7b97\u7b26\u7528\u4e8e\u6bd4\u8f83\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u503c\u3002\u5982\u679c\u4e24\u4e2a\u503c\u76f8\u540c\uff0c\u5219\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5217\u8868\u3001\u5143\u7ec4\u548c\u96c6\u5408\u7684\u6bd4\u8f83<\/strong><\/li>\n<\/ol>\n<p><p>Python\u4e2d\u7684\u5217\u8868\u3001\u5143\u7ec4\u548c\u96c6\u5408\u90fd\u662f\u5bb9\u5668\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\u3002\u53ef\u4ee5\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u6bd4\u8f83\u8fd9\u4e9b\u5bb9\u5668\u7c7b\u578b\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6bd4\u8f83\u5217\u8868<\/p>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [1, 2, 3]<\/p>\n<p>print(list1 == list2)  # \u8f93\u51fa: True<\/p>\n<h2><strong>\u6bd4\u8f83\u5143\u7ec4<\/strong><\/h2>\n<p>tuple1 = (1, 2, 3)<\/p>\n<p>tuple2 = (3, 2, 1)<\/p>\n<p>print(tuple1 == tuple2)  # \u8f93\u51fa: False<\/p>\n<h2><strong>\u6bd4\u8f83\u96c6\u5408<\/strong><\/h2>\n<p>set1 = {1, 2, 3}<\/p>\n<p>set2 = {3, 2, 1}<\/p>\n<p>print(set1 == set2)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6bd4\u8f83\u5217\u8868\u548c\u5143\u7ec4\u65f6\uff0c<code>==<\/code>\u8fd0\u7b97\u7b26\u4f1a\u68c0\u67e5\u5143\u7d20\u7684\u987a\u5e8f\u548c\u5185\u5bb9\u662f\u5426\u4e00\u81f4\u3002\u800c\u5728\u96c6\u5408\u4e2d\uff0c\u987a\u5e8f\u4e0d\u91cd\u8981\uff0c\u53ea\u8981\u5185\u5bb9\u4e00\u81f4\u5c31\u4f1a\u8fd4\u56de<code>True<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>is<\/code>\u5173\u952e\u5b57<\/p>\n<\/p>\n<p><p><code>is<\/code>\u5173\u952e\u5b57\u7528\u4e8e\u68c0\u67e5\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u503c\u76f8\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bf9\u8c61\u7684\u8eab\u4efd\u6bd4\u8f83<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Python\u4e2d\uff0c\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u8eab\u4efd\u6807\u8bc6\u3002<code>is<\/code>\u5173\u952e\u5b57\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u8eab\u4efd\u6807\u8bc6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528is\u6bd4\u8f83<\/p>\n<p>a = [1, 2, 3]<\/p>\n<p>b = a<\/p>\n<p>c = [1, 2, 3]<\/p>\n<p>print(a is b)  # \u8f93\u51fa: True<\/p>\n<p>print(a is c)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>b<\/code>\u662f<code>a<\/code>\u7684\u5f15\u7528\uff0c\u56e0\u6b64<code>a is b<\/code>\u8fd4\u56de<code>True<\/code>\u3002\u800c<code>c<\/code>\u662f\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u5bf9\u8c61\uff0c\u5373\u4f7f\u5b83\u4e0e<code>a<\/code>\u7684\u5185\u5bb9\u76f8\u540c\uff0c\u4f46\u5b83\u4eec\u7684\u8eab\u4efd\u4e0d\u540c\uff0c\u56e0\u6b64<code>a is c<\/code>\u8fd4\u56de<code>False<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong><code>is<\/code>\u4e0e<code>==<\/code>\u7684\u533a\u522b<\/strong><\/li>\n<\/ol>\n<p><p><code>is<\/code>\u4e0e<code>==<\/code>\u4e4b\u95f4\u7684\u533a\u522b\u5728\u4e8e\uff0c<code>is<\/code>\u7528\u4e8e\u6bd4\u8f83\u5bf9\u8c61\u7684\u8eab\u4efd\uff0c\u800c<code>==<\/code>\u7528\u4e8e\u6bd4\u8f83\u5bf9\u8c61\u7684\u503c\u3002\u5728\u6bd4\u8f83\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\u7b49\uff09\u65f6\u5c24\u5176\u9700\u8981\u6ce8\u610f\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># `is`\u4e0e`==`\u7684\u533a\u522b<\/p>\n<p>x = [4, 5, 6]<\/p>\n<p>y = x<\/p>\n<p>z = [4, 5, 6]<\/p>\n<p>print(x == y)  # \u8f93\u51fa: True<\/p>\n<p>print(x is y)  # \u8f93\u51fa: True<\/p>\n<p>print(x == z)  # \u8f93\u51fa: True<\/p>\n<p>print(x is z)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>x<\/code>\u548c<code>y<\/code>\u5f15\u7528\u540c\u4e00\u4e2a\u5217\u8868\u5bf9\u8c61\uff0c\u56e0\u6b64<code>x is y<\/code>\u8fd4\u56de<code>True<\/code>\u3002\u4f46<code>x<\/code>\u548c<code>z<\/code>\u867d\u7136\u5185\u5bb9\u76f8\u540c\uff0c\u4f46\u5f15\u7528\u4e0d\u540c\u7684\u5bf9\u8c61\uff0c\u56e0\u6b64<code>x is z<\/code>\u8fd4\u56de<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u7684\u5bf9\u8c61\u7c7b\u578b\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570\u6765\u5b9a\u4e49\u6bd4\u8f83\u89c4\u5219\u3002\u4f8b\u5982\uff0c\u6bd4\u8f83\u4e24\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u81ea\u5b9a\u4e49\u7c7b\u7684\u6bd4\u8f83<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u7c7b\u7684<code>__eq__<\/code>\u65b9\u6cd5\u6765\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u6bd4\u8f83\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def __eq__(self, other):<\/p>\n<p>        if isinstance(other, Person):<\/p>\n<p>            return self.name == other.name and self.age == other.age<\/p>\n<p>        return False<\/p>\n<p>person1 = Person(&quot;Alice&quot;, 30)<\/p>\n<p>person2 = Person(&quot;Alice&quot;, 30)<\/p>\n<p>person3 = Person(&quot;Bob&quot;, 25)<\/p>\n<p>print(person1 == person2)  # \u8f93\u51fa: True<\/p>\n<p>print(person1 == person3)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u5b9a\u4e49<code>__eq__<\/code>\u65b9\u6cd5\uff0c\u81ea\u5b9a\u4e49\u4e86<code>Person<\/code>\u7c7b\u5b9e\u4f8b\u7684\u6bd4\u8f83\u89c4\u5219\u3002\u8fd9\u6837\uff0c\u5f53\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u6bd4\u8f83<code>Person<\/code>\u5b9e\u4f8b\u65f6\uff0c\u4f1a\u6839\u636e\u59d3\u540d\u548c\u5e74\u9f84\u6765\u5224\u65ad\u5b83\u4eec\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684<code>functools<\/code>\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p>Python\u7684<code>functools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5b9e\u7528\u7684\u5de5\u5177\u51fd\u6570\uff0c\u6bd4\u5982<code>total_ordering<\/code>\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u5bf9\u8c61\u7684\u5b8c\u6574\u6bd4\u8f83\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import total_ordering<\/p>\n<p>@total_ordering<\/p>\n<p>class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def __eq__(self, other):<\/p>\n<p>        if isinstance(other, Person):<\/p>\n<p>            return self.name == other.name and self.age == other.age<\/p>\n<p>        return False<\/p>\n<p>    def __lt__(self, other):<\/p>\n<p>        if isinstance(other, Person):<\/p>\n<p>            return self.age &lt; other.age<\/p>\n<p>        return NotImplemented<\/p>\n<p>person1 = Person(&quot;Alice&quot;, 30)<\/p>\n<p>person2 = Person(&quot;Bob&quot;, 25)<\/p>\n<p>print(person1 == person2)  # \u8f93\u51fa: False<\/p>\n<p>print(person1 &lt; person2)   # \u8f93\u51fa: False<\/p>\n<p>print(person1 &gt; person2)   # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7<code>total_ordering<\/code>\u88c5\u9970\u5668\uff0c\u53ea\u9700\u8981\u5b9a\u4e49<code>__eq__<\/code>\u548c<code>__lt__<\/code>\u65b9\u6cd5\uff0c\u5176\u4ed6\u6bd4\u8f83\u65b9\u6cd5\uff08\u5982<code>__le__<\/code>\u3001<code>__gt__<\/code>\u3001<code>__ge__<\/code>\u7b49\uff09\u4f1a\u81ea\u52a8\u751f\u6210\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528<code>numpy<\/code>\u548c<code>pandas<\/code>\u5e93\u8fdb\u884c\u6bd4\u8f83<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u5e38\u4f7f\u7528<code>numpy<\/code>\u548c<code>pandas<\/code>\u5e93\u8fdb\u884c\u6570\u7ec4\u548c\u6570\u636e\u5e27\u7684\u6bd4\u8f83\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e13\u95e8\u7684\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u8fdb\u884c\u9ad8\u6548\u7684\u6bd4\u8f83\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528<code>numpy<\/code>\u8fdb\u884c\u6570\u7ec4\u6bd4\u8f83<\/strong><\/li>\n<\/ol>\n<p><p><code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86<code>array_equal<\/code>\u51fd\u6570\uff0c\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>arr3 = np.array([3, 2, 1])<\/p>\n<p>print(np.array_equal(arr1, arr2))  # \u8f93\u51fa: True<\/p>\n<p>print(np.array_equal(arr1, arr3))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>np.array_equal<\/code>\u51fd\u6570\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u7684\u5185\u5bb9\u3002\u5982\u679c\u4e24\u4e2a\u6570\u7ec4\u7684\u5f62\u72b6\u548c\u5185\u5bb9\u4e00\u81f4\uff0c\u5219\u8fd4\u56de<code>True<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528<code>pandas<\/code>\u8fdb\u884c\u6570\u636e\u5e27\u6bd4\u8f83<\/strong><\/li>\n<\/ol>\n<p><p><code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86<code>equals<\/code>\u65b9\u6cd5\uff0c\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u6570\u636e\u5e27\u6216\u7cfb\u5217\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df1 = pd.DataFrame({&#39;A&#39;: [1, 2], &#39;B&#39;: [3, 4]})<\/p>\n<p>df2 = pd.DataFrame({&#39;A&#39;: [1, 2], &#39;B&#39;: [3, 4]})<\/p>\n<p>df3 = pd.DataFrame({&#39;A&#39;: [2, 1], &#39;B&#39;: [4, 3]})<\/p>\n<p>print(df1.equals(df2))  # \u8f93\u51fa: True<\/p>\n<p>print(df1.equals(df3))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>equals<\/code>\u65b9\u6cd5\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u6570\u636e\u5e27\u7684\u5185\u5bb9\u3002\u5982\u679c\u6570\u636e\u5e27\u7684\u5f62\u72b6\u548c\u5185\u5bb9\u4e00\u81f4\uff0c\u5219\u8fd4\u56de<code>True<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5224\u65ad\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49\u3002<code>==<\/code>\u8fd0\u7b97\u7b26\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u3002<code>is<\/code>\u5173\u952e\u5b57\u7528\u4e8e\u68c0\u67e5\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5bf9\u4e8e\u9700\u8981\u6bd4\u8f83\u5bf9\u8c61\u8eab\u4efd\u7684\u60c5\u51b5\u975e\u5e38\u6709\u7528\u3002\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49<code>__eq__<\/code>\u65b9\u6cd5\u6765\u81ea\u5b9a\u4e49\u6bd4\u8f83\u884c\u4e3a\u3002\u6b64\u5916\uff0c<code>numpy<\/code>\u548c<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e13\u95e8\u7684\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u7528\u4e8e\u9ad8\u6548\u5730\u6bd4\u8f83\u6570\u7ec4\u548c\u6570\u636e\u5e27\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u6bd4\u8f83\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\u5f53\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u76f8\u540c\uff0c\u8868\u8fbe\u5f0f\u4f1a\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">a = 5  \nb = 5  \nprint(a == b)  # \u8f93\u51fa: True\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86<code>is<\/code>\u8fd0\u7b97\u7b26\u7528\u4e8e\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4f46\u8fd9\u5e76\u4e0d\u7b49\u540c\u4e8e\u503c\u7684\u6bd4\u8f83\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u6d6e\u70b9\u6570\u6bd4\u8f83\u7684\u7cbe\u5ea6\u95ee\u9898\uff1f<\/strong><br \/>\u6d6e\u70b9\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u5b58\u50a8\u65f6\u53ef\u80fd\u4f1a\u5f15\u5165\u5fae\u5c0f\u7684\u7cbe\u5ea6\u8bef\u5dee\uff0c\u56e0\u6b64\u76f4\u63a5\u4f7f\u7528<code>==<\/code>\u8fdb\u884c\u6bd4\u8f83\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u7684<code>False<\/code>\u7ed3\u679c\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<code>math.isclose()<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5141\u8bb8\u8bbe\u7f6e\u4e00\u4e2a\u5bb9\u5fcd\u5ea6\uff0c\u9002\u7528\u4e8e\u8fd1\u4f3c\u76f8\u7b49\u7684\u5224\u65ad\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import math  \na = 0.1 + 0.2  \nb = 0.3  \nprint(math.isclose(a, b))  # \u8f93\u51fa: True\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u907f\u514d\u6d6e\u70b9\u6570\u6bd4\u8f83\u65f6\u5e38\u89c1\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u7684\u6bd4\u8f83\u540c\u6837\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u7b26\u4e32\u6bd4\u8f83\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\u3002\u82e5\u60f3\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u5c0f\u5199\u6216\u5927\u5199\u540e\u518d\u8fdb\u884c\u6bd4\u8f83\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">str1 = &quot;Hello&quot;  \nstr2 = &quot;hello&quot;  \nprint(str1 == str2)  # \u8f93\u51fa: False  \nprint(str1.lower() == str2.lower())  # \u8f93\u51fa: True\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u65e0\u8bba\u7528\u6237\u8f93\u5165\u7684\u5927\u5c0f\u5199\u5982\u4f55\uff0c\u6bd4\u8f83\u90fd\u80fd\u6b63\u786e\u8fdb\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5224\u65ad\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49\uff0c\u901a\u5e38\u4f7f\u7528==\u8fd0\u7b97\u7b26\u3001is\u5173\u952e\u5b57\u4ee5\u53ca\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570\u3002==\u7528\u4e8e\u6bd4\u8f83\u503c\u662f\u5426\u76f8\u7b49\uff0c [&hellip;]","protected":false},"author":3,"featured_media":996405,"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\/996403"}],"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=996403"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/996403\/revisions"}],"predecessor-version":[{"id":996407,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/996403\/revisions\/996407"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/996405"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=996403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=996403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=996403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}