{"id":999417,"date":"2024-12-27T09:41:22","date_gmt":"2024-12-27T01:41:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/999417.html"},"modified":"2024-12-27T09:41:23","modified_gmt":"2024-12-27T01:41:23","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%bakey","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/999417.html","title":{"rendered":"python+\u5982\u4f55\u8f93\u51fakey"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074442\/435d80b1-c4c8-49a5-b34b-aa053c293a3e.webp\" alt=\"python+\u5982\u4f55\u8f93\u51fakey\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8f93\u51fa\u5b57\u5178\u7684key\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528\u5b57\u5178\u7684keys()\u65b9\u6cd5\u3001\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5b57\u5178\u3001\u4f7f\u7528\u5217\u8868\u548c\u96c6\u5408\u8f6c\u6362\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u5b57\u5178\u7684keys()\u65b9\u6cd5\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u952e\u7684\u89c6\u56fe\u5bf9\u8c61\u3002\u901a\u8fc7for\u5faa\u73af\uff0c\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u5b57\u5178\u7684\u6bcf\u4e2a\u952e\uff0c\u4fbf\u4e8e\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u5176\u4ed6\u64cd\u4f5c\u3002\u5c06\u5b57\u5178\u7684\u952e\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u96c6\u5408\uff0c\u5219\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u952e\u7684\u8fdb\u4e00\u6b65\u64cd\u4f5c\uff0c\u5982\u6392\u5e8f\u6216\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63a2\u8ba8\u5b83\u4eec\u7684\u9002\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u5178\u7684keys()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u7684<code>keys()<\/code>\u65b9\u6cd5\u662f\u83b7\u53d6\u6240\u6709\u952e\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u89c6\u56fe\u5bf9\u8c61\uff0c\u5305\u542b\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>keys = my_dict.keys()<\/p>\n<p>print(keys)  # dict_keys([&#39;name&#39;, &#39;age&#39;, &#39;city&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.1 \u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7b80\u6d01\u660e\u4e86<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u4e8e\u7406\u89e3\u3002<\/li>\n<li><strong>\u52a8\u6001\u89c6\u56fe<\/strong>\uff1a\u8fd4\u56de\u7684\u89c6\u56fe\u5bf9\u8c61\u662f\u52a8\u6001\u7684\uff0c\u5f53\u5b57\u5178\u6539\u53d8\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u66f4\u65b0\u3002<\/li>\n<\/ul>\n<p><h4>1.2 \u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7c7b\u578b<\/strong>\uff1a<code>keys()<\/code>\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u89c6\u56fe\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5217\u8868\u3002\u5982\u679c\u9700\u8981\u5217\u8868\u5f62\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7<code>list()<\/code>\u51fd\u6570\u8f6c\u6362\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">keys_list = list(my_dict.keys())<\/p>\n<p>print(keys_list)  # [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528for\u5faa\u73af\u6765\u904d\u5386\u5b57\u5178\u7684\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for key in my_dict:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.1 \u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u76f4\u63a5\u64cd\u4f5c<\/strong>\uff1a\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u76f4\u63a5\u5bf9\u952e\u8fdb\u884c\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u7b80\u4fbf\u6027<\/strong>\uff1a\u65e0\u9700\u8c03\u7528\u989d\u5916\u7684\u65b9\u6cd5\u6216\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p><h4>2.2 \u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u5bf9\u4e8e\u5927\u578b\u5b57\u5178\u6216\u590d\u6742\u64cd\u4f5c\uff0c\u53ef\u80fd\u9700\u8981\u8003\u8651\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u5c06\u5b57\u5178\u7684\u952e\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5c06\u5b57\u5178\u7684\u952e\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u96c6\u5408\u53ef\u80fd\u66f4\u65b9\u4fbf\uff0c\u5c24\u5176\u662f\u9700\u8981\u5bf9\u952e\u8fdb\u884c\u6392\u5e8f\u6216\u53bb\u91cd\u65f6\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u8f6c\u6362\u4e3a\u5217\u8868<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">keys_list = list(my_dict.keys())<\/p>\n<p>print(keys_list)  # [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u8f6c\u6362\u4e3a\u96c6\u5408<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">keys_set = set(my_dict.keys())<\/p>\n<p>print(keys_set)  # {&#39;name&#39;, &#39;city&#39;, &#39;age&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.3 \u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u53ef\u4ee5\u5bf9\u952e\u8fdb\u884c\u6392\u5e8f\u6216\u53bb\u91cd\u3002<\/li>\n<li><strong>\u64cd\u4f5c\u4e30\u5bcc<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6216\u96c6\u5408\u7684\u5404\u79cd\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h4>3.4 \u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7c7b\u578b\u8f6c\u6362\u5f00\u9500<\/strong>\uff1a\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u96c6\u5408\u4f1a\u6709\u4e00\u5b9a\u7684\u6027\u80fd\u5f00\u9500\uff0c\u5c24\u5176\u662f\u5b57\u5178\u8f83\u5927\u65f6\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5b57\u5178\u89e3\u6790\u548c\u9ad8\u7ea7\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u9ad8\u7ea7\u573a\u666f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u89e3\u6790\uff08dictionary comprehension\uff09\u548c\u5176\u4ed6Python\u9ad8\u7ea7\u529f\u80fd\u6765\u5904\u7406\u952e\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5b57\u5178\u89e3\u6790<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u89e3\u6790\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u65b0\u7684\u5b57\u5178\uff0c\u6216\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u590d\u6742\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squared_dict = {key: len(key) for key in my_dict}<\/p>\n<p>print(squared_dict)  # {&#39;name&#39;: 4, &#39;age&#39;: 3, &#39;city&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u6a21\u5757<\/h4>\n<\/p>\n<p><p>Python\u5185\u7f6e\u51fd\u6570\u548c\u6a21\u5757\uff08\u5982<code>itertools<\/code>\u3001<code>collections<\/code>\uff09\u53ef\u4ee5\u63d0\u4f9b\u66f4\u591a\u64cd\u4f5c\u5b57\u5178\u952e\u7684\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>key_counter = Counter(my_dict)<\/p>\n<p>print(key_counter)  # Counter({&#39;name&#39;: 1, &#39;age&#39;: 1, &#39;city&#39;: 1})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u5b9e\u8df5\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8f93\u51fa\u5b57\u5178\u7684\u952e\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u8be5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u7b80\u5355\u573a\u666f<\/strong>\uff1a\u76f4\u63a5\u4f7f\u7528<code>keys()<\/code>\u65b9\u6cd5\u6216for\u5faa\u73af\u3002<\/li>\n<li><strong>\u9700\u8981\u5217\u8868<\/strong>\uff1a\u4f7f\u7528<code>list()<\/code>\u8fdb\u884c\u8f6c\u6362\u3002<\/li>\n<li><strong>\u9700\u8981\u9ad8\u7ea7\u64cd\u4f5c<\/strong>\uff1a\u8003\u8651\u4f7f\u7528\u5b57\u5178\u89e3\u6790\u6216\u5176\u4ed6Python\u9ad8\u7ea7\u529f\u80fd\u3002<\/li>\n<\/ol>\n<p><p>\u5728\u65e5\u5e38\u7f16\u7a0b\u4e2d\uff0c\u591a\u52a0\u7ec3\u4e60\u548c\u601d\u8003\uff0c\u624d\u80fd\u771f\u6b63\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u5e76\u7075\u6d3b\u8fd0\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u952e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u5b57\u5178\u7684<code>keys()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6240\u6709\u7684\u952e\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u952e\u7684\u89c6\u56fe\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6216\u8f6c\u6362\u4e3a\u5217\u8868\u6765\u67e5\u770b\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 = my_dict.keys()\nprint(list(keys))  # \u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]\n<\/code><\/pre>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u8f93\u51fa\u5b57\u5178\u7684\u952e\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>keys()<\/code>\u65b9\u6cd5\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u63d0\u53d6\u5b57\u5178\u7684\u952e\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;x&#39;: 10, &#39;y&#39;: 20, &#39;z&#39;: 30}\nkeys = [key for key in my_dict]\nprint(keys)  # \u8f93\u51fa: [&#39;x&#39;, &#39;y&#39;, &#39;z&#39;]\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u8f93\u51fa\u5b57\u5178\u7684\u952e\u53ca\u5176\u5bf9\u5e94\u7684\u503c\uff1f<\/strong><br \/>\u5982\u679c\u60f3\u8981\u8f93\u51fa\u5b57\u5178\u7684\u6bcf\u4e2a\u952e\u53ca\u5176\u5bf9\u5e94\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>items()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u89c6\u56fe\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6765\u663e\u793a\u5b83\u4eec\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}\nfor key, value in my_dict.items():\n    print(f&#39;Key: {key}, Value: {value}&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c06\u4e3a\u6bcf\u4e2a\u952e\u8f93\u51fa\u5176\u5bf9\u5e94\u7684\u503c\uff0c\u4fbf\u4e8e\u7406\u89e3\u5b57\u5178\u7684\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8f93\u51fa\u5b57\u5178\u7684key\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528\u5b57\u5178\u7684keys()\u65b9\u6cd5\u3001\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5b57\u5178\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":999426,"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\/999417"}],"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=999417"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999417\/revisions"}],"predecessor-version":[{"id":999427,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999417\/revisions\/999427"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/999426"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=999417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=999417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=999417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}