{"id":1132526,"date":"2025-01-08T20:58:10","date_gmt":"2025-01-08T12:58:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132526.html"},"modified":"2025-01-08T20:58:13","modified_gmt":"2025-01-08T12:58:13","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e5%85%b8%e7%9a%84key%e5%80%bc%e5%ad%98%e5%88%b0%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1132526.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5b57\u5178\u7684key\u503c\u5b58\u5230\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102037\/4d15e6f7-e682-497c-a713-088c85fa4697.webp\" alt=\"python\u5982\u4f55\u5c06\u5b57\u5178\u7684key\u503c\u5b58\u5230\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u5178\u7684key\u503c\u5b58\u5230\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4f8b\u5982\u4f7f\u7528\u5b57\u5178\u7684<code>keys()<\/code>\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5faa\u73af\u7b49\u3002\u6700\u7b80\u5355\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>keys()<\/code>\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6027\u80fd\u8f83\u9ad8\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u53ca\u5176\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528<code>keys()<\/code>\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>Python\u5b57\u5178\u7684<code>keys()<\/code>\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u952e\u7684\u89c6\u56fe\u5bf9\u8c61\uff0c\u53ef\u4ee5\u76f4\u63a5\u8f6c\u6362\u4e3a\u5217\u8868\u5b58\u50a8\u5b57\u5178\u7684key\u503c\u3002\u8fd9\u662f\u6700\u7b80\u5355\u548c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>keys_list = list(my_dict.keys())<\/p>\n<p>print(keys_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>my_dict.keys()<\/code>\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\u89c6\u56fe\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5305\u542b\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u3002\u7136\u540e\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5217\u8868\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7528\u6765\u5904\u7406\u5b57\u5178\u7684key\u503c\uff0c\u8fd8\u53ef\u4ee5\u5728\u5904\u7406\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u4e00\u4e9b\u8fc7\u6ee4\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>keys_list = [key for key in my_dict]<\/p>\n<p>print(keys_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u4f7f\u7528\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u901a\u8fc7\u904d\u5386\u5b57\u5178\u7684\u6bcf\u4e2a\u952e\uff0c\u5c06\u5176\u653e\u5165\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001\u4f7f\u7528\u5faa\u73af<\/strong><\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1\u4e0d\u5982\u524d\u4e24\u79cd\u65b9\u6cd5\u7b80\u6d01\uff0c\u4f46\u4f7f\u7528\u5faa\u73af\u65b9\u6cd5\u53ef\u4ee5\u63d0\u4f9b\u66f4\u591a\u7684\u7075\u6d3b\u6027\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5728\u5904\u7406\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>keys_list = []<\/p>\n<p>for key in my_dict:<\/p>\n<p>    keys_list.append(key)<\/p>\n<p>print(keys_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u624b\u52a8\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u6bcf\u4e2a\u952e\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u6027\u80fd\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u4e5f\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u7684\u56e0\u7d20\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c<code>keys()<\/code>\u65b9\u6cd5\u7684\u6027\u80fd\u6700\u597d\uff0c\u5176\u6b21\u662f\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u6700\u540e\u662f\u5faa\u73af\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6027\u80fd\u6bd4\u8f83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>my_dict = {i: i*2 for i in range(1000000)}<\/p>\n<h2><strong>Using keys() method<\/strong><\/h2>\n<p>start = time.time()<\/p>\n<p>keys_list = list(my_dict.keys())<\/p>\n<p>end = time.time()<\/p>\n<p>print(f&quot;keys() method took {end - start} seconds&quot;)<\/p>\n<h2><strong>Using list comprehension<\/strong><\/h2>\n<p>start = time.time()<\/p>\n<p>keys_list = [key for key in my_dict]<\/p>\n<p>end = time.time()<\/p>\n<p>print(f&quot;List comprehension took {end - start} seconds&quot;)<\/p>\n<h2><strong>Using loop<\/strong><\/h2>\n<p>start = time.time()<\/p>\n<p>keys_list = []<\/p>\n<p>for key in my_dict:<\/p>\n<p>    keys_list.append(key)<\/p>\n<p>end = time.time()<\/p>\n<p>print(f&quot;Loop took {end - start} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u6027\u80fd\u6bd4\u8f83\u4e2d\uff0c\u53ef\u4ee5\u6e05\u695a\u5730\u770b\u5230<code>keys()<\/code>\u65b9\u6cd5\u5728\u5904\u7406\u5927\u5b57\u5178\u65f6\u7684\u6548\u7387\u6700\u9ad8\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u7b80\u5355\u4efb\u52a1<\/strong>\uff1a\u5982\u679c\u53ea\u9700\u8981\u7b80\u5355\u5730\u5c06\u5b57\u5178\u7684key\u503c\u5b58\u5165\u5217\u8868\uff0c\u4f7f\u7528<code>keys()<\/code>\u65b9\u6cd5\u662f\u6700\u597d\u7684\u9009\u62e9\u3002<\/li>\n<li><strong>\u8fc7\u6ee4\u64cd\u4f5c<\/strong>\uff1a\u5982\u679c\u9700\u8981\u5728\u5b58\u50a8key\u503c\u7684\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u8fc7\u6ee4\u6216\u5176\u4ed6\u64cd\u4f5c\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u4f1a\u66f4\u4e3a\u5408\u9002\u3002<\/li>\n<li><strong>\u590d\u6742\u64cd\u4f5c<\/strong>\uff1a\u5982\u679c\u9700\u8981\u5728\u5904\u7406\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u590d\u6742\u7684\u8ba1\u7b97\u6216\u64cd\u4f5c\uff0c\u5faa\u73af\u65b9\u6cd5\u63d0\u4f9b\u4e86\u6700\u5927\u7684\u7075\u6d3b\u6027\u3002<\/li>\n<\/ol>\n<p><p><strong>\u516d\u3001\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u5178\u7684key\u503c\u5b58\u5230\u5217\u8868\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4e09\u79cd\uff1a\u4f7f\u7528<code>keys()<\/code>\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5faa\u73af\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u7b80\u5355\u7684\u4efb\u52a1\uff0c<code>keys()<\/code>\u65b9\u6cd5\u662f\u6700\u4f73\u9009\u62e9\uff1b\u5bf9\u4e8e\u9700\u8981\u8fdb\u884c\u8fc7\u6ee4\u6216\u5176\u4ed6\u64cd\u4f5c\u7684\u4efb\u52a1\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u66f4\u4e3a\u5408\u9002\uff1b\u800c\u5bf9\u4e8e\u590d\u6742\u64cd\u4f5c\uff0c\u5faa\u73af\u65b9\u6cd5\u63d0\u4f9b\u4e86\u6700\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5c06\u5b57\u5178\u7684key\u503c\u5b58\u5230\u5217\u8868\u4e2d\u3002\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u8981\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u8fdb\u884c\u6743\u8861\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u5b57\u5178\u7684\u952e\u63d0\u53d6\u5230\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>keys()<\/code>\u65b9\u6cd5\u6765\u63d0\u53d6\u5b57\u5178\u7684\u952e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\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>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u5b57\u5178\u7684\u952e\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4ece\u5b57\u5178\u4e2d\u63d0\u53d6\u952e\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u4e00\u79cd\u7b80\u6d01\u7684\u63d0\u53d6\u548c\u5904\u7406\u6570\u636e\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u5b57\u5178\u7684\u952e\u63d0\u53d6\u5230\u5217\u8868\u4e2d\uff1a<\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}\nkeys_list = [key for key in my_dict]\nprint(keys_list)  # \u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u5728\u5904\u7406\u5927\u5b57\u5178\u65f6\u6548\u7387\u4e5f\u5f88\u9ad8\u3002<\/p>\n<p><strong>\u5728\u63d0\u53d6\u5b57\u5178\u7684\u952e\u65f6\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9\u5417\uff1f<\/strong><br \/>\u63d0\u53d6\u5b57\u5178\u952e\u65f6\u9700\u8981\u6ce8\u610f\u5b57\u5178\u7684\u987a\u5e8f\u3002Python 3.7\u53ca\u4ee5\u4e0a\u7248\u672c\u7684\u5b57\u5178\u662f\u6709\u5e8f\u7684\uff0c\u8fd9\u610f\u5473\u7740\u952e\u7684\u987a\u5e8f\u4e0e\u63d2\u5165\u987a\u5e8f\u76f8\u540c\u3002\u5982\u679c\u4f7f\u7528\u7684\u662f\u65e9\u671f\u7248\u672c\u7684Python\uff0c\u5b57\u5178\u5219\u662f\u65e0\u5e8f\u7684\uff0c\u63d0\u53d6\u7684\u952e\u987a\u5e8f\u53ef\u80fd\u4f1a\u4e0e\u539f\u59cb\u63d2\u5165\u987a\u5e8f\u4e0d\u540c\u3002\u6b64\u5916\uff0c\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u7c7b\u578b\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5143\u7ec4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u5b57\u5178\u7684key\u503c\u5b58\u5230\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4f8b\u5982\u4f7f\u7528\u5b57\u5178\u7684keys()\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5faa\u73af\u7b49\u3002\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1132536,"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\/1132526"}],"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=1132526"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132526\/revisions"}],"predecessor-version":[{"id":1132539,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132526\/revisions\/1132539"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1132536"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1132526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1132526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1132526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}