{"id":1140694,"date":"2025-01-08T22:23:55","date_gmt":"2025-01-08T14:23:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1140694.html"},"modified":"2025-01-08T22:23:57","modified_gmt":"2025-01-08T14:23:57","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ada%e7%ad%89%e4%ba%8eb%e7%9a%84%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1140694.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5224\u65ada\u7b49\u4e8eb\u7684\u8868\u8fbe\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103633\/44e8dffe-86cc-46be-9f4e-bb614862817a.webp\" alt=\"python\u4e2d\u5982\u4f55\u5224\u65ada\u7b49\u4e8eb\u7684\u8868\u8fbe\u5f0f\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5224\u65ada\u7b49\u4e8eb\u7684\u8868\u8fbe\u5f0f\u662f\uff1aa == b\u3002<\/strong> \u901a\u8fc7\u4f7f\u7528\u53cc\u7b49\u53f7<code>==<\/code>\u8fd0\u7b97\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u67e5\u4e24\u4e2a\u53d8\u91cf\u6216\u6570\u503c\u662f\u5426\u76f8\u7b49\u3002\u5982\u679c\u5b83\u4eec\u76f8\u7b49\uff0c\u8be5\u8868\u8fbe\u5f0f\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002<strong>\u8fd0\u7528==\u8fd0\u7b97\u7b26\u3001\u6ce8\u610f\u6570\u636e\u7c7b\u578b\u3001\u4f7f\u7528is\u8fd0\u7b97\u7b26<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u70b9\u2014\u2014\u6ce8\u610f\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u6ce8\u610f\u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u7ed3\u679c\u53ef\u80fd\u4f1a\u51fa\u4e4e\u610f\u6599\u3002\u5373\u4f7f\u6570\u503c\u770b\u8d77\u6765\u76f8\u540c\uff0c\u4f46\u7531\u4e8e\u6570\u636e\u7c7b\u578b\u4e0d\u540c\uff0c\u6bd4\u8f83\u7ed3\u679c\u53ef\u80fd\u4f1a\u662f<code>False<\/code>\u3002\u4f8b\u5982\uff0c\u6574\u6570<code>5<\/code>\u548c\u5b57\u7b26\u4e32<code>&#39;5&#39;<\/code>\u5728Python\u4e2d\u662f\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u56e0\u6b64<code>5 == &#39;5&#39;<\/code>\u4f1a\u8fd4\u56de<code>False<\/code>\u3002\u56e0\u6b64\uff0c\u5728\u8fdb\u884c\u6bd4\u8f83\u4e4b\u524d\uff0c\u786e\u4fdd\u6570\u636e\u7c7b\u578b\u4e00\u81f4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8fd0\u7528==\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u662f\u6700\u5e38\u89c1\u7684\u5224\u65ad\u53d8\u91cf\u76f8\u7b49\u7684\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u7b49\uff0c\u4ee5\u53ca\u590d\u6742\u6570\u636e\u7c7b\u578b\uff0c\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\uff0c<code>==<\/code>\u8fd0\u7b97\u7b26\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\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>c = 10.0<\/p>\n<p>print(a == c)  # \u8f93\u51fa: True<\/p>\n<p>d = &#39;10&#39;<\/p>\n<p>print(a == d)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u770b\u5230\u6574\u6570\u548c\u6d6e\u70b9\u6570\u4e4b\u95f4\u7684\u6bd4\u8f83\u4f1a\u8fd4\u56de<code>True<\/code>\uff0c\u4f46\u6574\u6570\u548c\u5b57\u7b26\u4e32\u7684\u6bd4\u8f83\u4f1a\u8fd4\u56de<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2. \u590d\u6742\u6570\u636e\u7c7b\u578b\u7684\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u6570\u636e\u7c7b\u578b\uff0c\u5982\u5217\u8868\u3001\u5143\u7ec4\u548c\u5b57\u5178\uff0c<code>==<\/code>\u8fd0\u7b97\u7b26\u4f1a\u68c0\u67e5\u5b83\u4eec\u7684\u503c\u662f\u5426\u76f8\u7b49\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><p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u770b\u5230\u5373\u4f7f\u662f\u590d\u6742\u6570\u636e\u7c7b\u578b\uff0c\u53ea\u8981\u5b83\u4eec\u7684\u5185\u5bb9\u76f8\u7b49\uff0c<code>==<\/code>\u8fd0\u7b97\u7b26\u5c31\u4f1a\u8fd4\u56de<code>True<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6ce8\u610f\u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u5982\u524d\u6240\u8ff0\uff0c\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u7ed3\u679c\u53ef\u80fd\u4f1a\u51fa\u4e4e\u610f\u6599\u3002\u56e0\u6b64\uff0c\u5728\u8fdb\u884c\u6bd4\u8f83\u4e4b\u524d\uff0c\u786e\u4fdd\u6570\u636e\u7c7b\u578b\u4e00\u81f4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><h4>1. \u7c7b\u578b\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6bd4\u8f83\u4e4b\u524d\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u7c7b\u578b\u8f6c\u6362\u6765\u786e\u4fdd\u6570\u636e\u7c7b\u578b\u4e00\u81f4\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32<code>&#39;5&#39;<\/code>\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u518d\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 5<\/p>\n<p>b = &#39;5&#39;<\/p>\n<p>print(a == b)  # \u8f93\u51fa: False<\/p>\n<p>b = int(b)<\/p>\n<p>print(a == b)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u7c7b\u578b\u68c0\u67e5<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6bd4\u8f83\u4e4b\u524d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7c7b\u578b\u68c0\u67e5\u6765\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u9519\u8bef\u3002Python\u63d0\u4f9b\u4e86<code>type()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 5<\/p>\n<p>b = &#39;5&#39;<\/p>\n<p>if type(a) == type(b):<\/p>\n<p>    print(a == b)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u6570\u636e\u7c7b\u578b\u4e0d\u4e00\u81f4&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u68c0\u67e5\u4e86\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u5982\u679c\u7c7b\u578b\u4e00\u81f4\uff0c\u624d\u8fdb\u884c\u6bd4\u8f83\uff0c\u5426\u5219\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528is\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u9664\u4e86<code>==<\/code>\u8fd0\u7b97\u7b26\uff0cPython\u8fd8\u63d0\u4f9b\u4e86<code>is<\/code>\u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u867d\u7136\u5b83\u4eec\u770b\u8d77\u6765\u76f8\u4f3c\uff0c\u4f46<code>is<\/code>\u8fd0\u7b97\u7b26\u68c0\u67e5\u7684\u662f\u5bf9\u8c61\u7684\u8eab\u4efd\uff0c\u800c\u4e0d\u662f\u503c\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 == b)  # \u8f93\u51fa: True<\/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>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u770b\u5230\u867d\u7136<code>a<\/code>\u548c<code>b<\/code>\u7684\u503c\u76f8\u7b49\uff0c\u4f46\u5b83\u4eec\u662f\u4e0d\u540c\u7684\u5bf9\u8c61\uff0c\u56e0\u6b64<code>a is b<\/code>\u8fd4\u56de<code>False<\/code>\u3002\u4f46<code>a<\/code>\u548c<code>c<\/code>\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u56e0\u6b64<code>a is c<\/code>\u8fd4\u56de<code>True<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5224\u65ada\u7b49\u4e8eb\u7684\u8868\u8fbe\u5f0f\u662f<code>a == b<\/code>\u3002\u5728\u4f7f\u7528\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8fd0\u7528==\u8fd0\u7b97\u7b26<\/strong>\uff1a\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u5224\u65ad\u53d8\u91cf\u76f8\u7b49\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u590d\u6742\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li><strong>\u6ce8\u610f\u6570\u636e\u7c7b\u578b<\/strong>\uff1a\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u7ed3\u679c\u53ef\u80fd\u4f1a\u51fa\u4e4e\u610f\u6599\u3002\u56e0\u6b64\uff0c\u5728\u8fdb\u884c\u6bd4\u8f83\u4e4b\u524d\uff0c\u786e\u4fdd\u6570\u636e\u7c7b\u578b\u4e00\u81f4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/li>\n<li><strong>\u4f7f\u7528is\u8fd0\u7b97\u7b26<\/strong>\uff1a\u5982\u679c\u9700\u8981\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528<code>is<\/code>\u8fd0\u7b97\u7b26\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u53ef\u4ee5\u66f4\u51c6\u786e\u5730\u5224\u65ad\u53d8\u91cf\u662f\u5426\u76f8\u7b49\uff0c\u4ece\u800c\u7f16\u5199\u51fa\u66f4\u5065\u58ee\u548c\u53ef\u9760\u7684Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u76f8\u7b49\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u76f8\u7b49\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>if a == b:<\/code>\u6765\u68c0\u67e5\u53d8\u91cf<code>a<\/code>\u548c<code>b<\/code>\u7684\u503c\u662f\u5426\u76f8\u540c\u3002\u5982\u679c\u76f8\u7b49\uff0c\u6761\u4ef6\u4e3a\u771f\uff0c\u60a8\u53ef\u4ee5\u5728if\u8bed\u53e5\u5185\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>==<\/code>\u8fd0\u7b97\u7b26\u4f1a\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\uff0c\u800c\u4e0d\u662f\u5b83\u4eec\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5224\u65ad\u5bf9\u8c61\u7684\u7c7b\u578b\u5e76\u8fdb\u884c\u76f8\u7b49\u6027\u6bd4\u8f83\uff1f<\/strong><br \/>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5224\u65ad\u5bf9\u8c61\u7684\u7c7b\u578b\u540e\u518d\u8fdb\u884c\u76f8\u7b49\u6027\u6bd4\u8f83\u3002\u53ef\u4ee5\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>if isinstance(a, int) and isinstance(b, int) and a == b:<\/code>\u6765\u786e\u4fdd\u53ea\u6709\u5728<code>a<\/code>\u548c<code>b<\/code>\u90fd\u662f\u6574\u6570\u65f6\u624d\u8fdb\u884c\u6bd4\u8f83\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u7c7b\u578b\u4e0d\u5339\u914d\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u6d6e\u70b9\u6570\u6bd4\u8f83\u65f6\u7684\u7cbe\u5ea6\u95ee\u9898\uff1f<\/strong><br \/>\u5f53\u6d89\u53ca\u6d6e\u70b9\u6570\u6bd4\u8f83\u65f6\uff0c\u76f4\u63a5\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002\u7531\u4e8e\u6d6e\u70b9\u6570\u7684\u8868\u793a\u7cbe\u5ea6\u95ee\u9898\uff0c\u5efa\u8bae\u4f7f\u7528<code>math.isclose()<\/code>\u51fd\u6570\u8fdb\u884c\u6bd4\u8f83\u3002\u4f8b\u5982\uff0c<code>math.isclose(a, b, rel_tol=1e-9)<\/code>\u53ef\u4ee5\u6709\u6548\u5730\u5224\u65ad\u4e24\u4e2a\u6d6e\u70b9\u6570\u662f\u5426\u76f8\u7b49\uff0c\u5141\u8bb8\u6307\u5b9a\u76f8\u5bf9\u8bef\u5dee\uff0c\u4ee5\u786e\u4fdd\u6bd4\u8f83\u7684\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5224\u65ada\u7b49\u4e8eb\u7684\u8868\u8fbe\u5f0f\u662f\uff1aa == b\u3002 \u901a\u8fc7\u4f7f\u7528\u53cc\u7b49\u53f7==\u8fd0\u7b97\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u67e5\u4e24\u4e2a\u53d8\u91cf\u6216\u6570\u503c [&hellip;]","protected":false},"author":3,"featured_media":1140704,"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\/1140694"}],"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=1140694"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140694\/revisions"}],"predecessor-version":[{"id":1140706,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140694\/revisions\/1140706"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1140704"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1140694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1140694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1140694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}