{"id":1043095,"date":"2024-12-31T13:03:01","date_gmt":"2024-12-31T05:03:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1043095.html"},"modified":"2024-12-31T13:03:03","modified_gmt":"2024-12-31T05:03:03","slug":"python%e5%a6%82%e4%bd%95%e6%a0%b9%e6%8d%ae%e5%ad%97%e5%85%b8%e7%9a%84%e9%94%ae%e8%bf%9b%e8%a1%8c%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1043095.html","title":{"rendered":"python\u5982\u4f55\u6839\u636e\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/122a6015-8d8d-4448-b596-85086704beae.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u6839\u636e\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sorted()\u3001\u901a\u8fc7\u952e\u503c\u5bf9\u6392\u5e8f\u3001\u4f7f\u7528lambda\u8868\u8fbe\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6839\u636e\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684sorted()\u51fd\u6570\u6765\u5b9e\u73b0\uff0c\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u5b57\u5178\u7684\u952e\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u3002\u901a\u8fc7\u5c06\u5b57\u5178\u7684items()\u65b9\u6cd5\u4e0esorted()\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u4f60\u53ef\u4ee5\u83b7\u53d6\u6392\u5e8f\u540e\u7684\u952e\u503c\u5bf9\u5217\u8868\u3002<strong>\u901a\u8fc7lambda\u8868\u8fbe\u5f0f\u6307\u5b9a\u6392\u5e8f\u4f9d\u636e\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5b9a\u5236\u6392\u5e8f\u7684\u65b9\u5f0f<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528sorted()\u51fd\u6570<\/strong><\/h2>\n<p><p>\u4f7f\u7528sorted()\u51fd\u6570\u662f\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u7684\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002sorted()\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6392\u5e8f\u540e\u7684\u5217\u8868\u3002\u5728\u5904\u7406\u5b57\u5178\u65f6\uff0c\u4f60\u53ef\u4ee5\u5c06\u5b57\u5178\u7684items()\u65b9\u6cd5\u4e0esorted()\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h2>1. \u901a\u8fc7\u5b57\u5178\u7684items()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u5b57\u5178\u7684items()\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u952e\u503c\u5bf9\u7684\u89c6\u56fe\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u89c6\u56fe\u5bf9\u8c61\u53ef\u4ee5\u88absorted()\u51fd\u6570\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;b&#39;: 2, &#39;a&#39;: 1, &#39;c&#39;: 3}<\/p>\n<p>sorted_dict = dict(sorted(my_dict.items()))<\/p>\n<p>print(sorted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0csorted()\u51fd\u6570\u5bf9\u5b57\u5178\u7684\u952e\u503c\u5bf9\u8fdb\u884c\u4e86\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6392\u5e8f\u540e\u7684\u5217\u8868\u3002\u7136\u540e\uff0c\u8fd9\u4e2a\u5217\u8868\u88ab\u8f6c\u6362\u56de\u5b57\u5178\uff0c\u7ed3\u679c\u662f\u4e00\u4e2a\u6309\u952e\u6392\u5e8f\u540e\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h2>2. \u4f7f\u7528lambda\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u6765\u6307\u5b9a\u6392\u5e8f\u7684\u4f9d\u636e\u3002\u5728\u5904\u7406\u5b57\u5178\u65f6\uff0clambda\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6839\u636e\u952e\u6216\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;b&#39;: 2, &#39;a&#39;: 1, &#39;c&#39;: 3}<\/p>\n<p>sorted_dict = dict(sorted(my_dict.items(), key=lambda item: item[0]))<\/p>\n<p>print(sorted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0clambda\u8868\u8fbe\u5f0f<code>lambda item: item[0]<\/code>\u6307\u5b9a\u4e86\u6392\u5e8f\u4f9d\u636e\u4e3a\u952e\uff0c\u56e0\u6b64\u5b57\u5178\u5c06\u6309\u952e\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u901a\u8fc7\u952e\u503c\u5bf9\u6392\u5e8f<\/strong><\/h2>\n<p><p>\u9664\u4e86\u4f7f\u7528sorted()\u51fd\u6570\uff0c\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u6bd4\u8f83\u6765\u6392\u5e8f\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5b57\u5178\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6392\u5e8f\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>1. \u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684OrderedDict<\/h2>\n<\/p>\n<p><p>\u5728Python 3.7\u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u672c\u8eab\u662f\u6709\u5e8f\u7684\u3002\u4f46\u662f\u5728\u8f83\u65e9\u7684\u7248\u672c\u4e2d\uff0c\u4f60\u9700\u8981\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684OrderedDict\u6765\u4fdd\u6301\u5b57\u5178\u7684\u987a\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import OrderedDict<\/p>\n<p>my_dict = {&#39;b&#39;: 2, &#39;a&#39;: 1, &#39;c&#39;: 3}<\/p>\n<p>sorted_dict = OrderedDict(sorted(my_dict.items()))<\/p>\n<p>print(sorted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cOrderedDict\u4fdd\u6301\u4e86\u5b57\u5178\u7684\u987a\u5e8f\uff0c\u56e0\u6b64\u6392\u5e8f\u540e\u7684\u5b57\u5178\u4e5f\u662f\u6709\u5e8f\u7684\u3002<\/p>\n<\/p>\n<p><h2>2. \u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u51fd\u6570\u6765\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_sort(item):<\/p>\n<p>    return item[0]<\/p>\n<p>my_dict = {&#39;b&#39;: 2, &#39;a&#39;: 1, &#39;c&#39;: 3}<\/p>\n<p>sorted_dict = dict(sorted(my_dict.items(), key=custom_sort))<\/p>\n<p>print(sorted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0ccustom_sort\u51fd\u6570\u6307\u5b9a\u4e86\u6392\u5e8f\u4f9d\u636e\u4e3a\u952e\uff0c\u56e0\u6b64\u5b57\u5178\u5c06\u6309\u952e\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u5176\u4ed6\u6392\u5e8f\u65b9\u6cd5<\/strong><\/h2>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u6392\u5e8f\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6839\u636e\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h2>1. \u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u6709\u5e8f\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;b&#39;: 2, &#39;a&#39;: 1, &#39;c&#39;: 3}<\/p>\n<p>sorted_dict = {k: my_dict[k] for k in sorted(my_dict)}<\/p>\n<p>print(sorted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5b57\u5178\u63a8\u5bfc\u5f0f\u7ed3\u5408\u4e86sorted()\u51fd\u6570\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u6309\u952e\u6392\u5e8f\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h2>2. \u4f7f\u7528pandas\u5e93<\/h2>\n<\/p>\n<p><p>pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>my_dict = {&#39;b&#39;: 2, &#39;a&#39;: 1, &#39;c&#39;: 3}<\/p>\n<p>df = pd.DataFrame(list(my_dict.items()), columns=[&#39;Key&#39;, &#39;Value&#39;])<\/p>\n<p>df = df.sort_values(by=&#39;Key&#39;)<\/p>\n<p>sorted_dict = dict(zip(df[&#39;Key&#39;], df[&#39;Value&#39;]))<\/p>\n<p>print(sorted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cpandas\u5e93\u7684DataFrame\u5bf9\u8c61\u548csort_values()\u65b9\u6cd5\u88ab\u7528\u6765\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\u3002<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sorted()\u3001\u901a\u8fc7\u952e\u503c\u5bf9\u6392\u5e8f\u3001\u4f7f\u7528lambda\u8868\u8fbe\u5f0f<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b9e\u73b0\u5b57\u5178\u7684\u6392\u5e8f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f60\u662f\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fd8\u662f\u7b2c\u4e09\u65b9\u5e93\uff0c\u90fd\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u6765\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\u3002\u53ea\u9700\u4f20\u5165\u5b57\u5178\u7684\u952e\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u503c\u5c06\u662f\u4e00\u4e2a\u6309\u952e\u6392\u5e8f\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>sorted(my_dict.keys())<\/code>\u5c06\u8fd4\u56de\u4e00\u4e2a\u6309\u5b57\u5178<code>my_dict<\/code>\u7684\u952e\u5347\u5e8f\u6392\u5217\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u5bf9\u5b57\u5178\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\uff1f<\/strong><br \/>\u5bf9\u4e8e\u964d\u5e8f\u6392\u5e8f\uff0c\u53ef\u4ee5\u5728\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u65f6\u8bbe\u7f6e<code>reverse=True<\/code>\u53c2\u6570\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<code>sorted(my_dict.keys(), reverse=True)<\/code>\u3002\u8fd9\u6837\uff0c\u8fd4\u56de\u7684\u5217\u8868\u5c06\u6309\u952e\u7684\u964d\u5e8f\u6392\u5217\u3002<\/p>\n<p><strong>\u5b57\u5178\u6392\u5e8f\u540e\u5982\u4f55\u4fdd\u6301\u539f\u6709\u7684\u952e\u503c\u5bf9\u5173\u7cfb\uff1f<\/strong><br \/>\u5728\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6216<code>collections.OrderedDict<\/code>\u6765\u4fdd\u6301\u952e\u503c\u5bf9\u7684\u5173\u7cfb\u3002\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u7684\u793a\u4f8b\u5982\u4e0b\uff1a<code>sorted_dict = {key: my_dict[key] for key in sorted(my_dict.keys())}<\/code>\u3002\u8fd9\u6837\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u6309\u7167\u6392\u5e8f\u540e\u7684\u952e\u987a\u5e8f\u4fdd\u6301\u539f\u6709\u7684\u952e\u503c\u5bf9\u5173\u7cfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sorted()\u3001\u901a\u8fc7\u952e\u503c\u5bf9\u6392\u5e8f\u3001\u4f7f\u7528lambda\u8868\u8fbe\u5f0f \u5728Python\u4e2d\uff0c\u6839\u636e\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\u662f [&hellip;]","protected":false},"author":3,"featured_media":1043108,"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\/1043095"}],"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=1043095"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043095\/revisions"}],"predecessor-version":[{"id":1043111,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043095\/revisions\/1043111"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1043108"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1043095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1043095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1043095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}