{"id":1129889,"date":"2025-01-08T20:33:39","date_gmt":"2025-01-08T12:33:39","guid":{"rendered":""},"modified":"2025-01-08T20:33:44","modified_gmt":"2025-01-08T12:33:44","slug":"python%e5%a6%82%e4%bd%95%e6%a3%80%e6%9f%a5%e5%ad%97%e5%85%b8%e6%98%af%e5%90%a6%e5%8c%85%e5%90%ab%e6%9f%90%e4%b8%aa%e9%94%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1129889.html","title":{"rendered":"python\u5982\u4f55\u68c0\u67e5\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100141\/dfeb4bc4-d9b9-4514-8131-c7da450c3cf5.webp\" alt=\"python\u5982\u4f55\u68c0\u67e5\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u68c0\u67e5\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u3001\u4f7f\u7528<code>.get()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528<code>dict.keys()<\/code>\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u4e14\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u4e14\u9ad8\u6548\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u63a2\u8ba8\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>in<\/code> \u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p><code>in<\/code> \u5173\u952e\u5b57\u662f\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u7684\u6700\u76f4\u63a5\u548c\u7b80\u6d01\u7684\u65b9\u6cd5\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if key in dictionary:<\/p>\n<p>    # do something<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u7b80\u6d01\u660e\u4e86<\/strong>\uff1a\u4ee3\u7801\u7b80\u5355\uff0c\u6613\u4e8e\u9605\u8bfb\u548c\u7406\u89e3\u3002<\/li>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c<code>in<\/code> \u5173\u952e\u5b57\u7684\u6027\u80fd\u8868\u73b0\u4f18\u4e8e\u5176\u4ed6\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<p><h4>\u4f8b\u5b50<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;a&quot;: 1, &quot;b&quot;: 2, &quot;c&quot;: 3}<\/p>\n<p>key_to_check = &quot;b&quot;<\/p>\n<p>if key_to_check in my_dict:<\/p>\n<p>    print(f&quot;Key &#39;{key_to_check}&#39; is present in the dictionary.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;Key &#39;{key_to_check}&#39; is not present in the dictionary.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>in<\/code> \u5173\u952e\u5b57\u68c0\u67e5\u5b57\u5178 <code>my_dict<\/code> \u662f\u5426\u5305\u542b\u952e <code>&quot;b&quot;<\/code>\uff0c\u5e76\u6839\u636e\u7ed3\u679c\u6253\u5370\u76f8\u5e94\u7684\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>.get()<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>.get()<\/code> \u65b9\u6cd5\u4e5f\u662f\u68c0\u67e5\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u7684\u5e38\u7528\u65b9\u6cd5\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = dictionary.get(key, default_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u5b57\u5178\u5305\u542b\u6307\u5b9a\u7684\u952e\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94\u7684\u503c\uff1b\u5426\u5219\uff0c\u8fd4\u56de <code>default_value<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u907f\u514d\u5f02\u5e38<\/strong>\uff1a\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u4e0d\u4f1a\u629b\u51fa <code>KeyError<\/code> \u5f02\u5e38\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u4fbf\u4e8e\u5904\u7406\u4e0d\u5b58\u5728\u7684\u952e\u3002<\/li>\n<\/ol>\n<p><h4>\u4f8b\u5b50<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;a&quot;: 1, &quot;b&quot;: 2, &quot;c&quot;: 3}<\/p>\n<p>key_to_check = &quot;d&quot;<\/p>\n<p>value = my_dict.get(key_to_check, &quot;Key not found&quot;)<\/p>\n<p>print(f&quot;Value for key &#39;{key_to_check}&#39;: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>my_dict.get(key_to_check, &quot;Key not found&quot;)<\/code> \u8fd4\u56de\u9ed8\u8ba4\u503c <code>&quot;Key not found&quot;<\/code>\uff0c\u56e0\u4e3a\u5b57\u5178\u4e2d\u4e0d\u5305\u542b\u952e <code>&quot;d&quot;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 <code>dict.keys()<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>dict.keys()<\/code> \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u89c6\u56fe\u5bf9\u8c61\uff0c\u5305\u542b\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u3002\u53ef\u4ee5\u4f7f\u7528 <code>in<\/code> \u5173\u952e\u5b57\u68c0\u67e5\u67d0\u4e2a\u952e\u662f\u5426\u5728\u8fd9\u4e2a\u89c6\u56fe\u5bf9\u8c61\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u660e\u786e\u6027<\/strong>\uff1a\u53ef\u4ee5\u660e\u786e\u5730\u64cd\u4f5c\u548c\u68c0\u67e5\u5b57\u5178\u7684\u952e\u96c6\u5408\u3002<\/li>\n<li><strong>\u517c\u5bb9\u6027<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u952e\u96c6\u5408\u8fdb\u884c\u66f4\u591a\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/li>\n<\/ol>\n<p><h4>\u4f8b\u5b50<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;a&quot;: 1, &quot;b&quot;: 2, &quot;c&quot;: 3}<\/p>\n<p>key_to_check = &quot;c&quot;<\/p>\n<p>if key_to_check in my_dict.keys():<\/p>\n<p>    print(f&quot;Key &#39;{key_to_check}&#39; is present in the dictionary.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;Key &#39;{key_to_check}&#39; is not present in the dictionary.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>my_dict.keys()<\/code> \u8fd4\u56de\u5305\u542b\u5b57\u5178\u6240\u6709\u952e\u7684\u89c6\u56fe\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528 <code>in<\/code> \u5173\u952e\u5b57\u68c0\u67e5\u662f\u5426\u5305\u542b\u952e <code>&quot;c&quot;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 <code>try-except<\/code> \u5757<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u6765\u68c0\u67e5\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u9700\u8981\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u53ef\u4ee5\u7ed3\u5408\u5f02\u5e38\u5904\u7406\u903b\u8f91\uff0c\u6267\u884c\u66f4\u591a\u590d\u6742\u7684\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u591a\u79cd\u5f02\u5e38\u7684\u590d\u6742\u573a\u666f\u3002<\/li>\n<\/ol>\n<p><h4>\u4f8b\u5b50<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;a&quot;: 1, &quot;b&quot;: 2, &quot;c&quot;: 3}<\/p>\n<p>key_to_check = &quot;e&quot;<\/p>\n<p>try:<\/p>\n<p>    value = my_dict[key_to_check]<\/p>\n<p>    print(f&quot;Value for key &#39;{key_to_check}&#39;: {value}&quot;)<\/p>\n<p>except KeyError:<\/p>\n<p>    print(f&quot;Key &#39;{key_to_check}&#39; is not present in the dictionary.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u952e <code>&quot;e&quot;<\/code> \u4e0d\u5b58\u5728\uff0c<code>my_dict[key_to_check]<\/code> \u8bed\u53e5\u4f1a\u629b\u51fa <code>KeyError<\/code> \u5f02\u5e38\uff0c\u6211\u4eec\u5728 <code>except<\/code> \u5757\u4e2d\u6355\u83b7\u5e76\u5904\u7406\u8be5\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528 <code>defaultdict<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>defaultdict<\/code> \u662f <code>collections<\/code> \u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u503c\u6765\u5e94\u5bf9\u4e0d\u5b58\u5728\u7684\u952e\u3002\u867d\u7136\u8fd9\u4e0d\u662f\u76f4\u63a5\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\u7684\u65b9\u6cd5\uff0c\u4f46\u5b83\u5728\u5904\u7406\u7f3a\u5931\u952e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u81ea\u52a8\u5904\u7406\u7f3a\u5931\u952e<\/strong>\uff1a\u53ef\u4ee5\u81ea\u52a8\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u907f\u514d <code>KeyError<\/code> \u5f02\u5e38\u3002<\/li>\n<li><strong>\u7b80\u5316\u4ee3\u7801<\/strong>\uff1a\u51cf\u5c11\u624b\u52a8\u68c0\u67e5\u548c\u5904\u7406\u7f3a\u5931\u952e\u7684\u4ee3\u7801\u91cf\u3002<\/li>\n<\/ol>\n<p><h4>\u4f8b\u5b50<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>my_dict = defaultdict(lambda: &quot;Default Value&quot;, {&quot;a&quot;: 1, &quot;b&quot;: 2, &quot;c&quot;: 3})<\/p>\n<p>key_to_check = &quot;f&quot;<\/p>\n<p>value = my_dict[key_to_check]<\/p>\n<p>print(f&quot;Value for key &#39;{key_to_check}&#39;: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5373\u4f7f\u952e <code>&quot;f&quot;<\/code> \u4e0d\u5b58\u5728\uff0c<code>defaultdict<\/code> \u4e5f\u4f1a\u8fd4\u56de\u9ed8\u8ba4\u503c <code>&quot;Default Value&quot;<\/code>\uff0c\u800c\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u6027\u80fd\u5dee\u5f02\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684\u6027\u80fd\u6d4b\u8bd5\u6765\u6bd4\u8f83\u5b83\u4eec\u7684\u6548\u7387\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757\u5bf9\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>my_dict = {&quot;a&quot;: 1, &quot;b&quot;: 2, &quot;c&quot;: 3}<\/p>\n<p>key_to_check = &quot;b&quot;<\/p>\n<h2><strong>\u4f7f\u7528 in \u5173\u952e\u5b57<\/strong><\/h2>\n<p>in_time = timeit.timeit(&#39;key_to_check in my_dict&#39;, globals=globals(), number=1000000)<\/p>\n<p>print(f&#39;Using &quot;in&quot;: {in_time} seconds&#39;)<\/p>\n<h2><strong>\u4f7f\u7528 .get() \u65b9\u6cd5<\/strong><\/h2>\n<p>get_time = timeit.timeit(&#39;my_dict.get(key_to_check, &quot;Key not found&quot;)&#39;, globals=globals(), number=1000000)<\/p>\n<p>print(f&#39;Using &quot;.get()&quot;: {get_time} seconds&#39;)<\/p>\n<h2><strong>\u4f7f\u7528 dict.keys() \u65b9\u6cd5<\/strong><\/h2>\n<p>keys_time = timeit.timeit(&#39;key_to_check in my_dict.keys()&#39;, globals=globals(), number=1000000)<\/p>\n<p>print(f&#39;Using &quot;dict.keys()&quot;: {keys_time} seconds&#39;)<\/p>\n<h2><strong>\u4f7f\u7528 try-except \u5757<\/strong><\/h2>\n<p>try_except_time = timeit.timeit(&#39;&#39;&#39;<\/p>\n<p>try:<\/p>\n<p>    value = my_dict[key_to_check]<\/p>\n<p>except KeyError:<\/p>\n<p>    value = &quot;Key not found&quot;<\/p>\n<p>&#39;&#39;&#39;, globals=globals(), number=1000000)<\/p>\n<p>print(f&#39;Using &quot;try-except&quot;: {try_except_time} seconds&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e2a\u6027\u80fd\u6d4b\u8bd5\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 <code>in<\/code> \u5173\u952e\u5b57\u7684\u6027\u80fd\u662f\u6700\u4f18\u7684\uff0c\u800c\u5176\u4ed6\u65b9\u6cd5\u7684\u6027\u80fd\u6839\u636e\u5177\u4f53\u60c5\u51b5\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u68c0\u67e5\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u4f7f\u7528 <code>in<\/code> \u5173\u952e\u5b57\u662f\u6700\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u3001\u9ad8\u6548\u4e14\u6613\u4e8e\u7406\u89e3<\/strong>\u3002\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u4e0d\u5b58\u5728\u952e\u7684\u7279\u5b9a\u573a\u666f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 <code>.get()<\/code> \u65b9\u6cd5\u6216 <code>try-except<\/code> \u5757\u3002\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u7f3a\u5931\u952e\u7684\u573a\u666f\uff0c<code>defaultdict<\/code> \u4e5f\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5316\u4ee3\u7801\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u7406\u89e3\u5b83\u4eec\u7684\u6027\u80fd\u548c\u9002\u7528\u573a\u666f\u5bf9\u4e8e\u7f16\u5199\u9ad8\u6548\u548c\u5065\u58ee\u7684\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u60a8\u80fd\u66f4\u597d\u5730\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u5b57\u5178\u662f\u5426\u5305\u542b\u7279\u5b9a\u7684\u952e\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}\nif &#39;a&#39; in my_dict:\n    print(&quot;\u952e &#39;a&#39; \u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u201c\u952e &#39;a&#39; \u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\u201d\uff0c\u5982\u679c\u8be5\u952e\u5b58\u5728\u7684\u8bdd\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5b57\u5178\u7684\u952e\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u4e2d\u7684\u5b57\u5178\u952e\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\u3002\u8fd9\u610f\u5473\u7740<code>&#39;Key&#39;<\/code>\u548c<code>&#39;key&#39;<\/code>\u88ab\u89c6\u4e3a\u4e0d\u540c\u7684\u952e\u3002\u56e0\u6b64\uff0c\u5728\u68c0\u67e5\u952e\u65f6\uff0c\u8bf7\u786e\u4fdd\u6b63\u786e\u5339\u914d\u5927\u5c0f\u5199\u3002<\/p>\n<p><strong>\u5982\u4f55\u9ad8\u6548\u5730\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5b58\u5728\u591a\u4e2a\u952e\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u6765\u9ad8\u6548\u5730\u68c0\u67e5\u591a\u4e2a\u952e\u662f\u5426\u5b58\u5728\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u8981\u68c0\u67e5\u7684\u952e\u653e\u5165\u4e00\u4e2a\u96c6\u5408\u4e2d\uff0c\u5e76\u4f7f\u7528\u96c6\u5408\u4e0e\u5b57\u5178\u7684\u952e\u8fdb\u884c\u4ea4\u96c6\u64cd\u4f5c\u3002\u8fd9\u79cd\u65b9\u5f0f\u901a\u5e38\u6bd4\u9010\u4e2a\u68c0\u67e5\u6bcf\u4e2a\u952e\u66f4\u4e3a\u9ad8\u6548\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">keys_to_check = {&#39;a&#39;, &#39;b&#39;, &#39;d&#39;}\nexists = keys_to_check.intersection(my_dict.keys())\nprint(&quot;\u5b58\u5728\u7684\u952e:&quot;, exists)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd4\u56de\u5b57\u5178\u4e2d\u5b9e\u9645\u5b58\u5728\u7684\u952e\u7684\u96c6\u5408\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u68c0\u67e5\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528in\u5173\u952e\u5b57\u3001\u4f7f\u7528.get()\u65b9\u6cd5\u3001\u4f7f\u7528dict.key [&hellip;]","protected":false},"author":3,"featured_media":1129908,"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\/1129889"}],"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=1129889"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129889\/revisions"}],"predecessor-version":[{"id":1129911,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129889\/revisions\/1129911"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1129908"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1129889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1129889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1129889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}