{"id":998119,"date":"2024-12-27T09:30:04","date_gmt":"2024-12-27T01:30:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/998119.html"},"modified":"2024-12-27T09:30:06","modified_gmt":"2024-12-27T01:30:06","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97key","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/998119.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8ba1\u7b97key"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073650\/1d9a4fcf-c5f6-42e3-82ee-69b84b4fac9f.webp\" alt=\"python\u4e2d\u5982\u4f55\u8ba1\u7b97key\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u5b57\u5178\u7684\u952e\uff08key\uff09\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u5982<code>len()<\/code>\u3001<code>keys()<\/code>\u548c<code>in<\/code>\u64cd\u4f5c\u7b26\u3002<\/strong>\u3001<strong>\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u83b7\u53d6\u5b57\u5178\u952e\u7684\u6570\u91cf\u3001\u4f7f\u7528<code>keys()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5b57\u5178\u952e\u7684\u5217\u8868\u3001\u901a\u8fc7<code>in<\/code>\u64cd\u4f5c\u7b26\u68c0\u67e5\u7279\u5b9a\u952e\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\u3002<\/strong> \u5176\u4e2d\uff0c<code>len()<\/code>\u51fd\u6570\u5728\u8ba1\u7b97\u5b57\u5178\u952e\u7684\u6570\u91cf\u65f6\u975e\u5e38\u5e38\u7528\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u4e14\u9ad8\u6548\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u548c\u64cd\u4f5c\u5b57\u5178\u952e\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u4e2a\u65e0\u5e8f\u7684\u3001\u53ef\u53d8\u7684\u952e\u503c\u5bf9\u96c6\u5408\u3002\u5b57\u5178\u7684\u952e\u662f\u552f\u4e00\u7684\uff0c\u4e14\u53ef\u4ee5\u662f\u4efb\u4f55\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u6574\u6570\u6216\u5143\u7ec4\u3002\u8981\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528\u82b1\u62ec\u53f7<code>{}<\/code>\u6216<code>dict()<\/code>\u6784\u9020\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u82b1\u62ec\u53f7\u521b\u5efa\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u4f7f\u7528dict()\u6784\u9020\u51fd\u6570\u521b\u5efa\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = dict(name=&#39;Alice&#39;, age=25, city=&#39;New York&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li><strong>\u83b7\u53d6\u5b57\u5178\u952e\u7684\u6570\u91cf<\/strong><\/li>\n<\/ol>\n<p><p>\u8981\u83b7\u53d6\u5b57\u5178\u4e2d\u952e\u7684\u6570\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u3002\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u800c\u76f4\u63a5\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>key_count = len(my_dict)<\/p>\n<p>print(f&quot;\u5b57\u5178\u4e2d\u7684\u952e\u6570\u91cf\u662f\uff1a{key_count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u83b7\u53d6\u5b57\u5178\u7684\u952e\u5217\u8868<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>keys()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u5b57\u5178\u4e2d\u6240\u6709\u952e\u7684\u5217\u8868\u3002\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u89c6\u56fe\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8f6c\u6362\u4e3a\u5217\u8868\u4ee5\u4fbf\u4e8e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>keys_list = list(my_dict.keys())<\/p>\n<p>print(f&quot;\u5b57\u5178\u7684\u952e\u5217\u8868\u662f\uff1a{keys_list}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5b58\u5728\u7279\u5b9a\u952e<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>in<\/code>\u64cd\u4f5c\u7b26\u53ef\u4ee5\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5b58\u5728\u7279\u5b9a\u7684\u952e\u3002\u8fd9\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b57\u5178\u5728Python\u4e2d\u662f\u4ee5\u54c8\u5e0c\u8868\u5f62\u5f0f\u5b9e\u73b0\u7684\uff0c\u67e5\u627e\u901f\u5ea6\u975e\u5e38\u5feb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>if &#39;name&#39; in my_dict:<\/p>\n<p>    print(&quot;\u952e&#39;name&#39;\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u952e&#39;name&#39;\u4e0d\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5b57\u5178\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/p>\n<\/p>\n<ol>\n<li><strong>\u904d\u5386\u5b57\u5178\u7684\u952e<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u76f4\u63a5\u904d\u5386\u5b57\u5178\u7684\u952e\u3002\u8fd9\u5728\u9700\u8981\u5bf9\u5b57\u5178\u4e2d\u7684\u6bcf\u4e00\u4e2a\u952e\u8fdb\u884c\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>for key in my_dict:<\/p>\n<p>    print(f&quot;\u5b57\u5178\u4e2d\u7684\u952e\uff1a{key}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5b57\u5178\u952e\u7684\u6392\u5e8f<\/strong><\/li>\n<\/ol>\n<p><p>\u867d\u7136\u5b57\u5178\u662f\u65e0\u5e8f\u7684\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\u3002\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>sorted_keys = sorted(my_dict.keys())<\/p>\n<p>print(f&quot;\u6392\u5e8f\u540e\u7684\u952e\u5217\u8868\uff1a{sorted_keys}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5b57\u5178\u952e\u7684\u96c6\u5408\u64cd\u4f5c<\/strong><\/li>\n<\/ol>\n<p><p>\u56e0\u4e3a\u5b57\u5178\u952e\u662f\u552f\u4e00\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u5c06\u5b57\u5178\u952e\u89c6\u4e3a\u96c6\u5408\uff0c\u5e76\u8fdb\u884c\u96c6\u5408\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u4e24\u4e2a\u5b57\u5178\u952e\u7684\u4ea4\u96c6\u3001\u5e76\u96c6\u6216\u5dee\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>dict1 = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>dict2 = {&#39;b&#39;: 4, &#39;c&#39;: 5, &#39;d&#39;: 6}<\/p>\n<h2><strong>\u83b7\u53d6\u952e\u7684\u4ea4\u96c6<\/strong><\/h2>\n<p>intersection_keys = dict1.keys() &amp; dict2.keys()<\/p>\n<p>print(f&quot;\u952e\u7684\u4ea4\u96c6\uff1a{intersection_keys}&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u952e\u7684\u5e76\u96c6<\/strong><\/h2>\n<p>union_keys = dict1.keys() | dict2.keys()<\/p>\n<p>print(f&quot;\u952e\u7684\u5e76\u96c6\uff1a{union_keys}&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u952e\u7684\u5dee\u96c6<\/strong><\/h2>\n<p>difference_keys = dict1.keys() - dict2.keys()<\/p>\n<p>print(f&quot;\u952e\u7684\u5dee\u96c6\uff1a{difference_keys}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5b57\u5178\u952e\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u8fc7\u6ee4<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u952e\u8fdb\u884c\u6570\u636e\u8fc7\u6ee4\u3002\u4f8b\u5982\uff0c\u4ece\u4e00\u4e2a\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u4e2d\u63d0\u53d6\u51fa\u7279\u5b9a\u5b57\u6bb5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>data = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;San Francisco&#39;},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 35, &#39;city&#39;: &#39;Los Angeles&#39;}<\/p>\n<p>]<\/p>\n<p>filtered_data = [{key: person[key] for key in person if key != &#39;age&#39;} for person in data]<\/p>\n<p>print(f&quot;\u8fc7\u6ee4\u540e\u7684\u6570\u636e\uff1a{filtered_data}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6570\u636e\u7edf\u8ba1<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u952e\u6765\u8fdb\u884c\u6570\u636e\u7edf\u8ba1\u3002\u4f8b\u5982\uff0c\u7edf\u8ba1\u4e00\u4e2a\u6587\u672c\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>text = &quot;apple orange banana apple apple orange&quot;<\/p>\n<p>words = text.split()<\/p>\n<p>word_count = {}<\/p>\n<p>for word in words:<\/p>\n<p>    if word in word_count:<\/p>\n<p>        word_count[word] += 1<\/p>\n<p>    else:<\/p>\n<p>        word_count[word] = 1<\/p>\n<p>print(f&quot;\u5355\u8bcd\u7edf\u8ba1\u7ed3\u679c\uff1a{word_count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6570\u636e\u8f6c\u6362<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u8f6c\u6362\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528\u5b57\u5178\u7684\u952e\u5bf9\u6570\u636e\u8fdb\u884c\u91cd\u65b0\u7ec4\u7ec7\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 25, &#39;New York&#39;]<\/p>\n<p>converted_dict = dict(zip(keys, values))<\/p>\n<p>print(f&quot;\u8f6c\u6362\u540e\u7684\u5b57\u5178\uff1a{converted_dict}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f18\u5316\u5b57\u5178\u952e\u64cd\u4f5c\u7684\u6280\u5de7<\/p>\n<\/p>\n<ol>\n<li><strong>\u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u8f83\u5927\u7684\u5b57\u5178\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u5b57\u5178\u952e\u7684\u6570\u91cf\u6216\u91cd\u590d\u83b7\u53d6\u952e\u5217\u8868\u3002\u53ef\u4ee5\u5c06\u7ed3\u679c\u7f13\u5b58\u8d77\u6765\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5408\u7406\u9009\u62e9\u952e\u7c7b\u578b<\/strong><\/li>\n<\/ol>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u952e\u7c7b\u578b\u5bf9\u4e8e\u63d0\u9ad8\u5b57\u5178\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u6574\u6570\u6216\u77ed\u5b57\u7b26\u4e32\u4f5c\u4e3a\u952e\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528\u9ed8\u8ba4\u5b57\u5178<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u8fdb\u884c\u7edf\u8ba1\u6216\u7d2f\u52a0\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528<code>collections.defaultdict<\/code>\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>from collections import defaultdict<\/p>\n<p>word_count = defaultdict(int)<\/p>\n<p>for word in words:<\/p>\n<p>    word_count[word] += 1<\/p>\n<p>print(f&quot;\u4f7f\u7528defaultdict\u7684\u5355\u8bcd\u7edf\u8ba1\u7ed3\u679c\uff1a{word_count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u3002\u4e86\u89e3\u5982\u4f55\u8ba1\u7b97\u548c\u64cd\u4f5c\u5b57\u5178\u7684\u952e\uff0c\u53ef\u4ee5\u4f7f\u6211\u4eec\u5728\u5904\u7406\u6570\u636e\u65f6\u66f4\u52a0\u9ad8\u6548\u548c\u7075\u6d3b\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u638c\u63e1\u4e86\u83b7\u53d6\u5b57\u5178\u952e\u6570\u91cf\u3001\u83b7\u53d6\u952e\u5217\u8868\u3001\u68c0\u67e5\u952e\u5b58\u5728\u6027\u7b49\u57fa\u7840\u64cd\u4f5c\uff0c\u4ee5\u53ca\u5b57\u5178\u952e\u7684\u6392\u5e8f\u3001\u96c6\u5408\u8fd0\u7b97\u548c\u5b9e\u9645\u5e94\u7528\u573a\u666f\u3002\u5e0c\u671b\u8fd9\u4e9b\u6280\u5de7\u548c\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u4f7f\u7528Python\u5b57\u5178\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u83b7\u53d6\u5b57\u5178\u4e2d\u6240\u6709\u952e\u7684\u5217\u8868\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>dict.keys()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u5b57\u5178\u4e2d\u6240\u6709\u7684\u952e\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u89c6\u56fe\u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u5982\u679c\u9700\u8981\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u5f62\u5f0f\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}\nkeys_list = list(my_dict.keys())\nprint(keys_list)  # \u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u5b57\u5178\u4e2d\u952e\u7684\u4e2a\u6570\uff1f<\/strong><br \/>\u8981\u8ba1\u7b97\u5b57\u5178\u4e2d\u952e\u7684\u6570\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u5b57\u5178\u4e2d\u952e\u7684\u603b\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}\nnum_keys = len(my_dict)\nprint(num_keys)  # \u8f93\u51fa: 3\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u68c0\u67e5\u67d0\u4e2a\u952e\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6765\u68c0\u67e5\u7279\u5b9a\u7684\u952e\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u4e14\u9ad8\u6548\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}\nkey_to_check = &#39;b&#39;\nif key_to_check in my_dict:\n    print(f&quot;{key_to_check} \u5b58\u5728\u4e8e\u5b57\u5178\u4e2d&quot;)\nelse:\n    print(f&quot;{key_to_check} \u4e0d\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u5b57\u5178\u7684\u952e\uff08key\uff09\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u5982len()\u3001keys()\u548cin\u64cd\u4f5c\u7b26\u3002\u3001 [&hellip;]","protected":false},"author":3,"featured_media":998132,"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\/998119"}],"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=998119"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998119\/revisions"}],"predecessor-version":[{"id":998135,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998119\/revisions\/998135"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/998132"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=998119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=998119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=998119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}