{"id":1167830,"date":"2025-01-15T15:50:32","date_gmt":"2025-01-15T07:50:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1167830.html"},"modified":"2025-01-15T15:50:34","modified_gmt":"2025-01-15T07:50:34","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e5%ad%97%e5%85%b8%e9%94%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1167830.html","title":{"rendered":"python\u5982\u4f55\u8f93\u51fa\u5b57\u5178\u952e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065233\/81645712-ea5c-4445-b0ed-6382efd97315.webp\" alt=\"python\u5982\u4f55\u8f93\u51fa\u5b57\u5178\u952e\" \/><\/p>\n<p><p> <strong>Python\u8f93\u51fa\u5b57\u5178\u952e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528.keys()\u65b9\u6cd5\u3001\u904d\u5386\u5b57\u5178\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528dict\u5bf9\u8c61\u7684\u5185\u7f6e\u51fd\u6570\u7b49\u3002<\/strong>\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u901a\u8fc7.keys()\u65b9\u6cd5\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u952e\uff0c\u7136\u540e\u904d\u5386\u8fd9\u4e9b\u952e\u8fdb\u884c\u8f93\u51fa\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5feb\u901f\u4e14\u9ad8\u6548\u5730\u83b7\u53d6\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<strong>\u4f8b\u5982\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>for key in my_dict.keys():<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>my_dict.keys()<\/code>\u8fd4\u56de\u5b57\u5178<code>my_dict<\/code>\u7684\u6240\u6709\u952e\uff0c\u901a\u8fc7\u904d\u5386\u8fd9\u4e9b\u952e\uff0c\u9010\u4e2a\u8f93\u51fa\u5b57\u5178\u4e2d\u7684\u952e\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528.keys()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>.keys()<\/code> \u65b9\u6cd5\u662f\u83b7\u53d6\u5b57\u5178\u952e\u7684\u6700\u76f4\u63a5\u65b9\u5f0f\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u6240\u6709\u952e\u7684\u89c6\u56fe\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u8fd9\u4e2a\u89c6\u56fe\u5bf9\u8c61\u6765\u8f93\u51fa\u6240\u6709\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u952e<\/strong><\/h2>\n<p>keys = my_dict.keys()<\/p>\n<h2><strong>\u904d\u5386\u5e76\u8f93\u51fa\u6240\u6709\u952e<\/strong><\/h2>\n<p>for key in keys:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>.keys()<\/code> \u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u89c6\u56fe\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u52a8\u6001\u5730\u53cd\u6620\u5b57\u5178\u7684\u53d8\u5316\u3002\u56e0\u6b64\uff0c\u5982\u679c\u5728\u904d\u5386\u671f\u95f4\u5bf9\u5b57\u5178\u8fdb\u884c\u4e86\u4fee\u6539\uff08\u6bd4\u5982\u6dfb\u52a0\u6216\u5220\u9664\u952e\u503c\u5bf9\uff09\uff0c\u89c6\u56fe\u5bf9\u8c61\u4f1a\u7acb\u5373\u53cd\u6620\u8fd9\u4e9b\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u76f4\u63a5\u904d\u5386\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u76f4\u63a5\u904d\u5386\u5b57\u5178\u5b9e\u9645\u4e0a\u662f\u5728\u904d\u5386\u5b57\u5178\u7684\u952e\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u904d\u5386\u5b57\u5178\u6765\u83b7\u53d6\u5e76\u8f93\u51fa\u6240\u6709\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u76f4\u63a5\u904d\u5386\u5b57\u5178<\/strong><\/h2>\n<p>for key in my_dict:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u662f\u6700\u7b80\u6d01\u7684\uff0c\u56e0\u4e3a\u5b83\u4e0d\u9700\u8981\u8c03\u7528\u4efb\u4f55\u989d\u5916\u7684\u65b9\u6cd5\u3002\u76f4\u63a5\u904d\u5386\u5b57\u5178\u65e2\u65b9\u4fbf\u53c8\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7684\u952e\u8f93\u51fa\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u751f\u6210\u5217\u8868\u7684\u7b80\u6d01\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u5305\u542b\u5b57\u5178\u6240\u6709\u952e\u7684\u5217\u8868\uff0c\u5e76\u8fdb\u4e00\u6b65\u5904\u7406\u6216\u8f93\u51fa\u8fd9\u4e9b\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u5305\u542b\u6240\u6709\u952e\u7684\u5217\u8868<\/strong><\/h2>\n<p>keys = [key for key in my_dict]<\/p>\n<h2><strong>\u8f93\u51fa\u6240\u6709\u952e<\/strong><\/h2>\n<p>for key in keys:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u751f\u6210\u5305\u542b\u6240\u6709\u952e\u7684\u5217\u8868\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u8fdb\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u8fc7\u6ee4\u6216\u8f6c\u6362\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528dict\u5bf9\u8c61\u7684\u5185\u7f6e\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python \u7684 <code>dict<\/code> \u5bf9\u8c61\u6709\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u64cd\u4f5c\u5b57\u5178\uff0c\u6bd4\u5982 <code>items()<\/code> \u548c <code>values()<\/code> \u65b9\u6cd5\u3002\u867d\u7136\u8fd9\u4e9b\u65b9\u6cd5\u4e3b\u8981\u7528\u4e8e\u64cd\u4f5c\u952e\u503c\u5bf9\u6216\u503c\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4e5f\u53ef\u4ee5\u95f4\u63a5\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u4f7f\u7528 items() \u65b9\u6cd5\u904d\u5386\u5b57\u5178\u7684\u952e\u503c\u5bf9\uff0c\u5e76\u8f93\u51fa\u952e<\/strong><\/h2>\n<p>for key, value in my_dict.items():<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>items()<\/code> \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u6240\u6709\u952e\u503c\u5bf9\u7684\u89c6\u56fe\u5bf9\u8c61\uff0c\u901a\u8fc7\u904d\u5386\u8fd9\u4e2a\u89c6\u56fe\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u952e\u548c\u503c\uff0c\u5e76\u6839\u636e\u9700\u6c42\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u6765\u5904\u7406\u5b57\u5178\u952e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5b57\u5178\u952e\u5b58\u50a8\u5728\u96c6\u5408\u6216\u5217\u8868\u4e2d\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u64cd\u4f5c\u6216\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u5c06\u5b57\u5178\u952e\u5b58\u50a8\u5728\u96c6\u5408\u4e2d<\/strong><\/h2>\n<p>keys_set = set(my_dict.keys())<\/p>\n<h2><strong>\u8f93\u51fa\u96c6\u5408\u4e2d\u7684\u6240\u6709\u952e<\/strong><\/h2>\n<p>for key in keys_set:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\u5b58\u50a8\u5b57\u5178\u952e\u7684\u597d\u5904\u662f\uff0c\u96c6\u5408\u5177\u6709\u53bb\u91cd\u548c\u96c6\u5408\u64cd\u4f5c\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u952e\u8fdb\u884c\u53bb\u91cd\u6216\u96c6\u5408\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u5408\u51fd\u6570\u548c\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5b9a\u4e49\u51fd\u6570\u6216\u4f7f\u7528\u6a21\u5757\u6765\u5904\u7406\u66f4\u590d\u6742\u7684\u5b57\u5178\u952e\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u83b7\u53d6\u5e76\u8f93\u51fa\u5b57\u5178\u7684\u6240\u6709\u952e\uff0c\u5e76\u6839\u636e\u9700\u6c42\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_dict_keys(d):<\/p>\n<p>    for key in d.keys():<\/p>\n<p>        print(key)<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>print_dict_keys(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u83b7\u53d6\u5e76\u8f93\u51fa\u5b57\u5178\u952e\u7684\u64cd\u4f5c\u5c01\u88c5\u8d77\u6765\uff0c\u65b9\u4fbf\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u590d\u7528\u3002\u540c\u65f6\uff0cPython \u7684\u4e00\u4e9b\u5185\u7f6e\u6a21\u5757\uff08\u5982 <code>collections<\/code>\uff09\u4e5f\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b57\u5178\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6a21\u5757\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b57\u5178\u952e\u7684\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u5b57\u5178\u952e\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u518d\u8fdb\u884c\u8f93\u51fa\u3002Python \u63d0\u4f9b\u4e86\u591a\u79cd\u6392\u5e8f\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u952e\u7684\u6392\u5e8f\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u5bf9\u5b57\u5178\u952e\u8fdb\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_keys = sorted(my_dict.keys())<\/p>\n<h2><strong>\u8f93\u51fa\u6392\u5e8f\u540e\u7684\u952e<\/strong><\/h2>\n<p>for key in sorted_keys:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 <code>sorted()<\/code> \u51fd\u6570\u53ef\u4ee5\u5bf9\u5b57\u5178\u952e\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6392\u5e8f\u540e\u952e\u7684\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5347\u5e8f\u6216\u964d\u5e8f\u6392\u5e8f\uff0c\u5e76\u5bf9\u6392\u5e8f\u540e\u7684\u952e\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5b57\u5178\u952e\u7684\u8fc7\u6ee4<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u5bf9\u5b57\u5178\u952e\u8fdb\u884c\u8fc7\u6ee4\uff0c\u7136\u540e\u518d\u8fdb\u884c\u8f93\u51fa\u3002\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u5b9e\u73b0\u5bf9\u5b57\u5178\u952e\u7684\u8fc7\u6ee4\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u6839\u636e\u6761\u4ef6\u8fc7\u6ee4\u5b57\u5178\u952e\uff08\u4f8b\u5982\uff0c\u952e\u540d\u957f\u5ea6\u5927\u4e8e3\uff09<\/strong><\/h2>\n<p>filtered_keys = [key for key in my_dict.keys() if len(key) &gt; 3]<\/p>\n<h2><strong>\u8f93\u51fa\u8fc7\u6ee4\u540e\u7684\u952e<\/strong><\/h2>\n<p>for key in filtered_keys:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5bf9\u5b57\u5178\u952e\u7684\u8fc7\u6ee4\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u952e\u7684\u7b5b\u9009\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5b57\u5178\u952e\u7684\u6620\u5c04\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u5b57\u5178\u952e\u8fdb\u884c\u6620\u5c04\u64cd\u4f5c\uff0c\u5c06\u952e\u8f6c\u6362\u4e3a\u5176\u4ed6\u5f62\u5f0f\u6216\u8fdb\u884c\u67d0\u4e9b\u8ba1\u7b97\u3002\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u952e\u7684\u904d\u5386\u548c\u6620\u5c04\u51fd\u6570\uff0c\u5b9e\u73b0\u5bf9\u952e\u7684\u6620\u5c04\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u5bf9\u5b57\u5178\u952e\u8fdb\u884c\u6620\u5c04\u64cd\u4f5c\uff08\u4f8b\u5982\uff0c\u5c06\u952e\u8f6c\u6362\u4e3a\u5927\u5199\uff09<\/strong><\/h2>\n<p>mapped_keys = [key.upper() for key in my_dict.keys()]<\/p>\n<h2><strong>\u8f93\u51fa\u6620\u5c04\u540e\u7684\u952e<\/strong><\/h2>\n<p>for key in mapped_keys:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u6620\u5c04\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u5b57\u5178\u952e\u8fdb\u884c\u6620\u5c04\u64cd\u4f5c\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u952e\u7684\u8f6c\u6362\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5b57\u5178\u952e\u7684\u5408\u5e76\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5408\u5e76\u591a\u4e2a\u5b57\u5178\u7684\u952e\uff0c\u7136\u540e\u8fdb\u884c\u8f93\u51fa\u3002\u53ef\u4ee5\u7ed3\u5408\u591a\u4e2a\u5b57\u5178\u7684\u904d\u5386\u548c\u96c6\u5408\u64cd\u4f5c\uff0c\u5b9e\u73b0\u5bf9\u5b57\u5178\u952e\u7684\u5408\u5e76\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}<\/p>\n<p>dict2 = {&#39;city&#39;: &#39;New York&#39;, &#39;country&#39;: &#39;USA&#39;}<\/p>\n<h2><strong>\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u7684\u952e<\/strong><\/h2>\n<p>merged_keys = set(dict1.keys()).union(set(dict2.keys()))<\/p>\n<h2><strong>\u8f93\u51fa\u5408\u5e76\u540e\u7684\u952e<\/strong><\/h2>\n<p>for key in merged_keys:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u96c6\u5408\u7684 <code>union()<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5bf9\u591a\u4e2a\u5b57\u5178\u952e\u7684\u5408\u5e76\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u952e\u7684\u5408\u5e76\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u5b57\u5178\u952e\u7684\u7edf\u8ba1\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u6570\u636e\u5206\u6790\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u5b57\u5178\u952e\u8fdb\u884c\u7edf\u8ba1\u5206\u6790\uff0c\u4f8b\u5982\u7edf\u8ba1\u952e\u7684\u9891\u7387\u6216\u5206\u5e03\u3002\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u952e\u7684\u904d\u5386\u548c\u8ba1\u6570\u64cd\u4f5c\uff0c\u5b9e\u73b0\u5bf9\u952e\u7684\u7edf\u8ba1\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;, &#39;name&#39;: &#39;Jane&#39;}<\/p>\n<h2><strong>\u7edf\u8ba1\u5b57\u5178\u952e\u7684\u9891\u7387<\/strong><\/h2>\n<p>key_counter = Counter(my_dict.keys())<\/p>\n<h2><strong>\u8f93\u51fa\u952e\u7684\u9891\u7387<\/strong><\/h2>\n<p>for key, count in key_counter.items():<\/p>\n<p>    print(f&#39;{key}: {count}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408 <code>collections.Counter<\/code> \u7c7b\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5bf9\u5b57\u5178\u952e\u7684\u9891\u7387\u7edf\u8ba1\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u952e\u7684\u7edf\u8ba1\u5206\u6790\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u5b57\u5178\u952e\u7684\u8f6c\u6362\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u6570\u636e\u5904\u7406\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u5b57\u5178\u952e\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u5217\u8868\u3001\u96c6\u5408\u6216\u5143\u7ec4\u3002\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u952e\u7684\u904d\u5386\u548c\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u64cd\u4f5c\uff0c\u5b9e\u73b0\u5bf9\u952e\u7684\u8f6c\u6362\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u5c06\u5b57\u5178\u952e\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>keys_list = list(my_dict.keys())<\/p>\n<h2><strong>\u8f93\u51fa\u8f6c\u6362\u540e\u7684\u5217\u8868<\/strong><\/h2>\n<p>print(keys_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u64cd\u4f5c\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u5b57\u5178\u952e\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u952e\u7684\u8f6c\u6362\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u5b57\u5178\u952e\u7684\u6620\u5c04\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u5b57\u5178\u952e\u5e94\u7528\u67d0\u4e9b\u6620\u5c04\u51fd\u6570\uff0c\u4f8b\u5982\u5bf9\u952e\u540d\u8fdb\u884c\u683c\u5f0f\u5316\u6216\u8ba1\u7b97\u3002\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u952e\u7684\u904d\u5386\u548c\u6620\u5c04\u51fd\u6570\uff0c\u5b9e\u73b0\u5bf9\u952e\u7684\u6620\u5c04\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u5b9a\u4e49\u6620\u5c04\u51fd\u6570\uff08\u4f8b\u5982\uff0c\u5c06\u952e\u540d\u8f6c\u6362\u4e3a\u5927\u5199\uff09<\/strong><\/h2>\n<p>def map_key(key):<\/p>\n<p>    return key.upper()<\/p>\n<h2><strong>\u5e94\u7528\u6620\u5c04\u51fd\u6570\u5e76\u8f93\u51fa\u952e<\/strong><\/h2>\n<p>mapped_keys = [map_key(key) for key in my_dict.keys()]<\/p>\n<p>for key in mapped_keys:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u6620\u5c04\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u5b57\u5178\u952e\u5e94\u7528\u6620\u5c04\u51fd\u6570\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u952e\u7684\u5904\u7406\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u5b57\u5178\u952e\u7684\u805a\u5408\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u591a\u4e2a\u5b57\u5178\u7684\u952e\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\uff0c\u4f8b\u5982\u7edf\u8ba1\u591a\u4e2a\u5b57\u5178\u952e\u7684\u603b\u6570\u6216\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u952e\u7684\u904d\u5386\u548c\u805a\u5408\u51fd\u6570\uff0c\u5b9e\u73b0\u5bf9\u952e\u7684\u805a\u5408\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}<\/p>\n<p>dict2 = {&#39;city&#39;: &#39;New York&#39;, &#39;country&#39;: &#39;USA&#39;}<\/p>\n<h2><strong>\u805a\u5408\u4e24\u4e2a\u5b57\u5178\u7684\u952e<\/strong><\/h2>\n<p>all_keys = list(dict1.keys()) + list(dict2.keys())<\/p>\n<h2><strong>\u8f93\u51fa\u805a\u5408\u540e\u7684\u952e<\/strong><\/h2>\n<p>for key in all_keys:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u805a\u5408\u51fd\u6570\u548c\u5217\u8868\u64cd\u4f5c\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5bf9\u591a\u4e2a\u5b57\u5178\u952e\u7684\u805a\u5408\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u952e\u7684\u5904\u7406\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u5b57\u5178\u952e\u7684\u53bb\u91cd\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5305\u542b\u91cd\u590d\u952e\u7684\u591a\u4e2a\u5b57\u5178\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u5b57\u5178\u952e\u8fdb\u884c\u53bb\u91cd\u64cd\u4f5c\uff0c\u7136\u540e\u8fdb\u884c\u8f93\u51fa\u3002\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u952e\u7684\u904d\u5386\u548c\u96c6\u5408\u64cd\u4f5c\uff0c\u5b9e\u73b0\u5bf9\u952e\u7684\u53bb\u91cd\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}<\/p>\n<p>dict2 = {&#39;city&#39;: &#39;New York&#39;, &#39;name&#39;: &#39;Jane&#39;}<\/p>\n<h2><strong>\u53bb\u91cd\u4e24\u4e2a\u5b57\u5178\u7684\u952e<\/strong><\/h2>\n<p>unique_keys = set(dict1.keys()).union(set(dict2.keys()))<\/p>\n<h2><strong>\u8f93\u51fa\u53bb\u91cd\u540e\u7684\u952e<\/strong><\/h2>\n<p>for key in unique_keys:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u96c6\u5408\u7684\u53bb\u91cd\u7279\u6027\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5bf9\u591a\u4e2a\u5b57\u5178\u952e\u7684\u53bb\u91cd\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u952e\u7684\u5904\u7406\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u5b57\u5178\u952e\u7684\u590d\u6742\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u6570\u636e\u5904\u7406\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u5b57\u5178\u952e\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\uff0c\u4f8b\u5982\u5bf9\u952e\u8fdb\u884c\u591a\u7ea7\u6392\u5e8f\u6216\u5206\u7ec4\u3002\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u952e\u7684\u904d\u5386\u548c\u590d\u6742\u64cd\u4f5c\u51fd\u6570\uff0c\u5b9e\u73b0\u5bf9\u952e\u7684\u590d\u6742\u5904\u7406\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u591a\u7ea7\u6392\u5e8f\u5b57\u5178\u952e\uff08\u4f8b\u5982\uff0c\u6309\u952e\u540d\u957f\u5ea6\u548c\u5b57\u6bcd\u987a\u5e8f\uff09<\/strong><\/h2>\n<p>sorted_keys = sorted(my_dict.keys(), key=lambda k: (len(k), k))<\/p>\n<h2><strong>\u8f93\u51fa\u591a\u7ea7\u6392\u5e8f\u540e\u7684\u952e<\/strong><\/h2>\n<p>for key in sorted_keys:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u590d\u6742\u64cd\u4f5c\u51fd\u6570\u548c\u6392\u5e8f\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5bf9\u5b57\u5178\u952e\u7684\u590d\u6742\u5904\u7406\u548c\u8f93\u51fa\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u952e\u7684\u9ad8\u7ea7\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u5b57\u5178\u952e\u7684\u6279\u91cf\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6279\u91cf\u5904\u7406\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u591a\u4e2a\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6279\u91cf\u5904\u7406\u548c\u8f93\u51fa\u3002\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u952e\u7684\u904d\u5386\u548c\u6279\u91cf\u5904\u7406\u51fd\u6570\uff0c\u5b9e\u73b0\u5bf9\u952e\u7684\u6279\u91cf\u5904\u7406\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dicts = [<\/p>\n<p>    {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25},<\/p>\n<p>    {&#39;city&#39;: &#39;New York&#39;, &#39;country&#39;: &#39;USA&#39;}<\/p>\n<p>]<\/p>\n<h2><strong>\u6279\u91cf\u5904\u7406\u591a\u4e2a\u5b57\u5178\u7684\u952e<\/strong><\/h2>\n<p>for d in dicts:<\/p>\n<p>    for key in d.keys():<\/p>\n<p>        print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u6279\u91cf\u5904\u7406\u51fd\u6570\u548c\u5b57\u5178\u904d\u5386\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5bf9\u591a\u4e2a\u5b57\u5178\u952e\u7684\u6279\u91cf\u5904\u7406\u548c\u8f93\u51fa\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u952e\u7684\u6279\u91cf\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u5b57\u5178\u952e\u7684\u52a8\u6001\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u52a8\u6001\u6570\u636e\u5904\u7406\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u52a8\u6001\u6761\u4ef6\u5bf9\u5b57\u5178\u952e\u8fdb\u884c\u64cd\u4f5c\u548c\u8f93\u51fa\u3002\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u952e\u7684\u904d\u5386\u548c\u52a8\u6001\u6761\u4ef6\u5224\u65ad\uff0c\u5b9e\u73b0\u5bf9\u952e\u7684\u52a8\u6001\u5904\u7406\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u52a8\u6001\u6761\u4ef6\u5224\u65ad\uff08\u4f8b\u5982\uff0c\u952e\u540d\u5305\u542b\u5b57\u6bcd &#39;a&#39;\uff09<\/strong><\/h2>\n<p>dynamic_keys = [key for key in my_dict.keys() if &#39;a&#39; in key]<\/p>\n<h2><strong>\u8f93\u51fa\u52a8\u6001\u6761\u4ef6\u5224\u65ad\u540e\u7684\u952e<\/strong><\/h2>\n<p>for key in dynamic_keys:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u52a8\u6001\u6761\u4ef6\u5224\u65ad\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5bf9\u5b57\u5178\u952e\u7684\u52a8\u6001\u5904\u7406\u548c\u8f93\u51fa\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u952e\u7684\u52a8\u6001\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e5d\u3001\u5b57\u5178\u952e\u7684\u683c\u5f0f\u5316\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u6570\u636e\u5c55\u793a\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u5b57\u5178\u952e\u8fdb\u884c\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u4f8b\u5982\u6309\u7279\u5b9a\u683c\u5f0f\u6253\u5370\u952e\u3002\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u952e\u7684\u904d\u5386\u548c\u683c\u5f0f\u5316\u51fd\u6570\uff0c\u5b9e\u73b0\u5bf9\u952e\u7684\u683c\u5f0f\u5316\u5904\u7406\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u8f93\u51fa\u5b57\u5178\u952e\uff08\u4f8b\u5982\uff0c\u6309\u952e\u540d\u548c\u952e\u503c\u5bf9\u8f93\u51fa\uff09<\/strong><\/h2>\n<p>for key, value in my_dict.items():<\/p>\n<p>    print(f&#39;Key: {key}, Value: {value}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u683c\u5f0f\u5316\u51fd\u6570\u548c\u5b57\u5178\u904d\u5386\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5bf9\u5b57\u5178\u952e\u7684\u683c\u5f0f\u5316\u5904\u7406\u548c\u8f93\u51fa\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u952e\u7684\u683c\u5f0f\u5316\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u5341\u3001\u5b57\u5178\u952e\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u5b57\u5178\u952e\u7684\u83b7\u53d6\u548c\u8f93\u51fa\u8fdb\u884c\u6027\u80fd\u4f18\u5316\u3002\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u952e\u7684\u904d\u5386\u548c\u6027\u80fd\u4f18\u5316\u6280\u5de7\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u952e\u5904\u7406\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u6027\u80fd\u4f18\u5316\u6280\u5de7\uff08\u4f8b\u5982\uff0c\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\uff09<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>keys_gen = (key for key in my_dict.keys())<\/p>\n<p>for key in keys_gen:<\/p>\n<p>    print(key)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&#39;Time taken: {end_time - start_time} seconds&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548c\u6027\u80fd\u6d4b\u91cf\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5bf9\u5b57\u5178\u952e\u7684\u9ad8\u6548\u5904\u7406\u548c\u8f93\u51fa\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u952e\u7684\u6027\u80fd\u4f18\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u83b7\u53d6\u5e76\u8f93\u51fa\u5b57\u5178\u7684\u952e\uff0c\u5305\u62ec\u4f7f\u7528 <code>.keys()<\/code> \u65b9\u6cd5\u3001\u76f4\u63a5\u904d\u5386\u5b57\u5178\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528 <code>dict<\/code> \u5bf9\u8c61\u7684\u5185\u7f6e\u51fd\u6570\u3001\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u3001\u5b9a\u4e49\u51fd\u6570\u3001\u5bf9\u952e\u8fdb\u884c\u6392\u5e8f\u3001\u8fc7\u6ee4\u3001\u6620\u5c04\u3001\u5408\u5e76\u3001\u7edf\u8ba1\u5206\u6790\u3001\u8f6c\u6362\u3001\u53bb\u91cd\u3001\u590d\u6742\u64cd\u4f5c\u3001\u6279\u91cf\u5904\u7406\u3001\u52a8\u6001\u64cd\u4f5c\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u548c\u6027\u80fd\u4f18\u5316\u7b49\u65b9\u6cd5\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u548c\u8f93\u51fa\u5b57\u5178\u7684\u952e\u3002\u901a\u8fc7\u5bf9\u4e0d\u540c\u65b9\u6cd5\u7684\u7406\u89e3\u548c\u5e94\u7528\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u638c\u63e1 Python \u5b57\u5178\u64cd\u4f5c\u7684\u6280\u5de7\u548c\u5b9e\u8df5\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\u4f7f\u7528\u5b57\u5178\u5bf9\u8c61\u7684<code>keys()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u952e\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u53ef\u8fed\u4ee3\u7684\u89c6\u56fe\u5bf9\u8c61\uff0c\u5305\u542b\u5b57\u5178\u4e2d\u6240\u6709\u7684\u952e\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(keys)  # \u8f93\u51fa: dict_keys([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;])\n<\/code><\/pre>\n<p>\u5982\u679c\u9700\u8981\u5c06\u8fd9\u4e9b\u952e\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\uff0c\u5982\u4e0b\u6240\u793a\uff1a  <\/p>\n<pre><code class=\"language-python\">key_list = list(my_dict.keys())\nprint(key_list)  # \u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u904d\u5386\u5b57\u5178\u7684\u952e\uff1f<\/strong><br \/>\u904d\u5386\u5b57\u5178\u7684\u952e\u53ef\u4ee5\u901a\u8fc7<code>for<\/code>\u5faa\u73af\u5b9e\u73b0\u3002\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u5b57\u5178\u5bf9\u8c61\uff0c\u8fd9\u6837\u9ed8\u8ba4\u4f1a\u8fed\u4ee3\u5b57\u5178\u7684\u952e\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">for key in my_dict:\n    print(key)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u5faa\u73af\u4f1a\u4f9d\u6b21\u8f93\u51fa\u5b57\u5178\u4e2d\u7684\u6bcf\u4e00\u4e2a\u952e\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u65b9\u5f0f\u8f93\u51fa\u5b57\u5178\u7684\u952e\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>keys()<\/code>\u65b9\u6cd5\u548c\u76f4\u63a5\u904d\u5386\u5b57\u5178\u5916\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u6765\u8f93\u51fa\u5b57\u5178\u7684\u952e\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">key_list = [key for key in my_dict]\nprint(key_list)  # \u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u66f4\u52a0\u7b80\u6d01\uff0c\u7279\u522b\u9002\u5408\u5728\u9700\u8981\u5bf9\u952e\u8fdb\u884c\u8fdb\u4e00\u6b65\u64cd\u4f5c\u65f6\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8f93\u51fa\u5b57\u5178\u952e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528.keys()\u65b9\u6cd5\u3001\u904d\u5386\u5b57\u5178\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528dict\u5bf9\u8c61\u7684\u5185\u7f6e\u51fd [&hellip;]","protected":false},"author":3,"featured_media":1167835,"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\/1167830"}],"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=1167830"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167830\/revisions"}],"predecessor-version":[{"id":1167836,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167830\/revisions\/1167836"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1167835"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1167830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1167830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1167830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}