{"id":1082897,"date":"2025-01-08T12:52:59","date_gmt":"2025-01-08T04:52:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1082897.html"},"modified":"2025-01-08T12:53:01","modified_gmt":"2025-01-08T04:53:01","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-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1082897.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-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24193731\/41347b38-ca7d-4e53-81fa-8cc28000bcc0.webp\" alt=\"python\u5982\u4f55\u6839\u636e\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u6839\u636e\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\u7684\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u3001<code>collections.OrderedDict<\/code>\u7c7b\u3001\u4ee5\u53ca\u5b57\u5178\u63a8\u5bfc\u5f0f\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u6709\u6548\u5730\u5b9e\u73b0\u5b57\u5178\u952e\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u3001<code>collections.OrderedDict<\/code>\u7c7b\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong>\u662f\u8fdb\u884c\u5b57\u5178\u952e\u6392\u5e8f\u7684\u4e3b\u8981\u65b9\u6cd5\u3002<strong><code>sorted()<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f<\/strong>\uff0c<strong><code>OrderedDict<\/code>\u7c7b\u53ef\u4ee5\u4fdd\u7559\u5b57\u5178\u7684\u987a\u5e8f<\/strong>\uff0c<strong>\u5b57\u5178\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u65b0\u7684\u6392\u5e8f\u540e\u7684\u5b57\u5178<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd<code>sorted()<\/code>\u51fd\u6570\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u8fdb\u884c\u5b57\u5178\u952e\u6392\u5e8f<\/h3>\n<\/p>\n<p><p><code>sorted()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u540e\u7684\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u83b7\u53d6\u5b57\u5178\u952e\u7684\u6392\u5e8f\u5217\u8868\uff0c\u7136\u540e\u6784\u9020\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u539f\u59cb\u5b57\u5178<\/p>\n<p>original_dict = {&#39;apple&#39;: 3, &#39;banana&#39;: 2, &#39;cherry&#39;: 5, &#39;date&#39;: 1}<\/p>\n<h2><strong>\u4f7f\u7528sorted()\u51fd\u6570\u5bf9\u5b57\u5178\u952e\u8fdb\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_keys = sorted(original_dict.keys())<\/p>\n<h2><strong>\u6784\u9020\u4e00\u4e2a\u65b0\u7684\u5b57\u5178<\/strong><\/h2>\n<p>sorted_dict = {k: original_dict[k] for k in sorted_keys}<\/p>\n<p>print(sorted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u9996\u5148\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u5bf9\u539f\u59cb\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u5229\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6784\u9020\u4e86\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\u3002\u6700\u7ec8\u8f93\u51fa\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u6309\u952e\u6392\u5e8f\u540e\u7684\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code>{&#39;apple&#39;: 3, &#39;banana&#39;: 2, &#39;cherry&#39;: 5, &#39;date&#39;: 1}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>collections.OrderedDict<\/code>\u7c7b<\/h3>\n<\/p>\n<p><p>\u4ecePython 3.7\u5f00\u59cb\uff0c\u5b57\u5178\u7684\u63d2\u5165\u987a\u5e8f\u9ed8\u8ba4\u662f\u6709\u5e8f\u7684\uff0c\u4f46\u662f\u4e3a\u4e86\u517c\u5bb9\u65e7\u7248\u672c\uff0c\u6216\u8005\u4e3a\u4e86\u660e\u786e\u8868\u793a\u9700\u8981\u6709\u5e8f\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>collections.OrderedDict<\/code>\u7c7b\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import OrderedDict<\/p>\n<h2><strong>\u539f\u59cb\u5b57\u5178<\/strong><\/h2>\n<p>original_dict = {&#39;apple&#39;: 3, &#39;banana&#39;: 2, &#39;cherry&#39;: 5, &#39;date&#39;: 1}<\/p>\n<h2><strong>\u4f7f\u7528sorted()\u51fd\u6570\u5bf9\u5b57\u5178\u952e\u8fdb\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_keys = sorted(original_dict.keys())<\/p>\n<h2><strong>\u6784\u9020\u4e00\u4e2a\u6709\u5e8f\u5b57\u5178<\/strong><\/h2>\n<p>sorted_dict = OrderedDict((k, original_dict[k]) for k in sorted_keys)<\/p>\n<p>print(sorted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>OrderedDict<\/code>\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u5b57\u5178\u7684\u63d2\u5165\u987a\u5e8f\u88ab\u4fdd\u7559\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u751f\u6210\u6392\u5e8f\u540e\u7684\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u5b57\u5178\u952e\u7684\u6392\u5e8f\u548c\u65b0\u5b57\u5178\u7684\u6784\u9020\uff0c\u975e\u5e38\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u539f\u59cb\u5b57\u5178<\/p>\n<p>original_dict = {&#39;apple&#39;: 3, &#39;banana&#39;: 2, &#39;cherry&#39;: 5, &#39;date&#39;: 1}<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6392\u5e8f\u5e76\u751f\u6210\u65b0\u5b57\u5178<\/strong><\/h2>\n<p>sorted_dict = {k: original_dict[k] for k in sorted(original_dict)}<\/p>\n<p>print(sorted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u4e0e\u524d\u9762\u7684\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u90fd\u662f\u5229\u7528<code>sorted()<\/code>\u51fd\u6570\u5bf9\u952e\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u751f\u6210\u65b0\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u66f4\u591a\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u6839\u636e\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u89c4\u5219\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\uff0c\u4f8b\u5982\u6839\u636e\u952e\u7684\u957f\u5ea6\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u539f\u59cb\u5b57\u5178<\/p>\n<p>original_dict = {&#39;apple&#39;: 3, &#39;banana&#39;: 2, &#39;cherry&#39;: 5, &#39;date&#39;: 1}<\/p>\n<h2><strong>\u6839\u636e\u952e\u7684\u957f\u5ea6\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_dict = {k: original_dict[k] for k in sorted(original_dict, key=len)}<\/p>\n<p>print(sorted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>sorted()<\/code>\u51fd\u6570\u7684<code>key<\/code>\u53c2\u6570\u88ab\u8bbe\u7f6e\u4e3a<code>len<\/code>\u51fd\u6570\uff0c\u4ece\u800c\u6839\u636e\u952e\u7684\u957f\u5ea6\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6839\u636e\u6570\u503c\u5927\u5c0f\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u9700\u8981\u6839\u636e\u5b57\u5178\u503c\u7684\u5927\u5c0f\u5bf9\u952e\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u539f\u59cb\u5b57\u5178<\/p>\n<p>original_dict = {&#39;apple&#39;: 3, &#39;banana&#39;: 2, &#39;cherry&#39;: 5, &#39;date&#39;: 1}<\/p>\n<h2><strong>\u6839\u636e\u5b57\u5178\u503c\u7684\u5927\u5c0f\u6392\u5e8f\u952e<\/strong><\/h2>\n<p>sorted_dict = {k: original_dict[k] for k in sorted(original_dict, key=original_dict.get)}<\/p>\n<p>print(sorted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc<code>sorted()<\/code>\u51fd\u6570\u7684<code>key<\/code>\u53c2\u6570\u88ab\u8bbe\u7f6e\u4e3a<code>original_dict.get<\/code>\u51fd\u6570\uff0c\u4ece\u800c\u6839\u636e\u5b57\u5178\u503c\u7684\u5927\u5c0f\u8fdb\u884c\u952e\u7684\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6027\u80fd\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8f83\u5927\u7684\u5b57\u5178\uff0c\u6392\u5e8f\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u4e00\u822c\u6765\u8bf4\uff0c<code>sorted()<\/code>\u51fd\u6570\u548c\u5b57\u5178\u63a8\u5bfc\u5f0f\u7684\u7ec4\u5408\u5728\u6027\u80fd\u4e0a\u8868\u73b0\u8f83\u597d\uff0c\u4f46\u5982\u679c\u5bf9\u6027\u80fd\u6709\u66f4\u9ad8\u8981\u6c42\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6839\u636e\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong><code>sorted()<\/code>\u51fd\u6570\u3001<code>collections.OrderedDict<\/code>\u7c7b\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong>\u662f\u8fdb\u884c\u5b57\u5178\u952e\u6392\u5e8f\u7684\u4e3b\u8981\u65b9\u6cd5\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u6839\u636e\u81ea\u5b9a\u4e49\u89c4\u5219\u8fdb\u884c\u6392\u5e8f\uff0c\u5982\u6839\u636e\u952e\u7684\u957f\u5ea6\u6216\u5b57\u5178\u503c\u7684\u5927\u5c0f\u8fdb\u884c\u6392\u5e8f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u573a\u666f\u548c\u6027\u80fd\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u4ee5\u4e0a\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u5168\u9762\u7406\u89e3\u548c\u638c\u63e1Python\u4e2d\u6839\u636e\u5b57\u5178\u952e\u8fdb\u884c\u6392\u5e8f\u7684\u65b9\u6cd5\u548c\u6280\u5de7\u3002\u5982\u679c\u6709\u4efb\u4f55\u7591\u95ee\u6216\u8fdb\u4e00\u6b65\u7684\u9700\u6c42\uff0c\u6b22\u8fce\u968f\u65f6\u4ea4\u6d41\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\u3002\u53ef\u4ee5\u4f20\u5165\u5b57\u5178\u7684<code>keys()<\/code>\u65b9\u6cd5\uff0c\u8fd4\u56de\u4e00\u4e2a\u6309\u952e\u6392\u5e8f\u7684\u5217\u8868\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;banana&#39;: 3, &#39;apple&#39;: 5, &#39;orange&#39;: 2}\nsorted_keys = sorted(my_dict.keys())\nprint(sorted_keys)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u6309\u5b57\u5178\u952e\u6392\u5e8f\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u6839\u636e\u5b57\u5178\u7684\u503c\u8fdb\u884c\u6392\u5e8f\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u5141\u8bb8\u6839\u636e\u5b57\u5178\u7684\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u7684<code>key<\/code>\u53c2\u6570\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a  <\/p>\n<pre><code class=\"language-python\">sorted_by_value = sorted(my_dict.items(), key=lambda item: item[1])\nprint(sorted_by_value)  # \u8f93\u51fa: [(&#39;orange&#39;, 2), (&#39;banana&#39;, 3), (&#39;apple&#39;, 5)]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u6309\u503c\u6392\u5e8f\u7684\u5143\u7ec4\u5217\u8868\u3002<\/p>\n<p><strong>\u5728\u6392\u5e8f\u65f6\u53ef\u4ee5\u6307\u5b9a\u6392\u5e8f\u987a\u5e8f\u5417\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u5728\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>reverse=True<\/code>\u6765\u5b9e\u73b0\u964d\u5e8f\u6392\u5e8f\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a  <\/p>\n<pre><code class=\"language-python\">sorted_keys_desc = sorted(my_dict.keys(), reverse=True)\nprint(sorted_keys_desc)  # \u8f93\u51fa: [&#39;orange&#39;, &#39;banana&#39;, &#39;apple&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5b57\u5178\u7684\u952e\u5c06\u6309\u7167\u964d\u5e8f\u6392\u5217\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6839\u636e\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\u7684\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528sorted()\u51fd\u6570\u3001collections.Ord [&hellip;]","protected":false},"author":3,"featured_media":1082903,"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\/1082897"}],"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=1082897"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082897\/revisions"}],"predecessor-version":[{"id":1082904,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082897\/revisions\/1082904"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1082903"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1082897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1082897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1082897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}