{"id":1012675,"date":"2024-12-27T11:41:12","date_gmt":"2024-12-27T03:41:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1012675.html"},"modified":"2024-12-27T11:41:18","modified_gmt":"2024-12-27T03:41:18","slug":"python-%e5%a6%82%e4%bd%95%e5%af%b9%e5%ad%97%e5%85%b8%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1012675.html","title":{"rendered":"python \u5982\u4f55\u5bf9\u5b57\u5178\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090355\/3b3ddc03-854d-496f-8d73-538608d861e6.webp\" alt=\"python \u5982\u4f55\u5bf9\u5b57\u5178\u6392\u5e8f\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u3001\u5229\u7528<code>operator<\/code>\u6a21\u5757\u3001\u901a\u8fc7<code>collections.OrderedDict<\/code>\u6765\u4fdd\u6301\u987a\u5e8f\u3002<\/strong>\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u5bf9\u5b57\u5178\u6392\u5e8f\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6309\u952e\u6216\u6309\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u6b64\u65b9\u6cd5\u7b80\u5355\u6613\u7528\u4e14\u7075\u6d3b\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u4ee5\u53ca\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528 <code>sorted()<\/code> \u51fd\u6570\u5bf9\u5b57\u5178\u6392\u5e8f<\/p>\n<\/p>\n<p><p><code>sorted()<\/code> \u662f Python \u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5bf9\u4efb\u610f\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\u3002\u5728\u5bf9\u5b57\u5178\u6392\u5e8f\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u6309\u952e\u6216\u6309\u503c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>1. \u6309\u5b57\u5178\u952e\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528 <code>sorted()<\/code> \u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u6309\u5b57\u5178\u7684\u952e\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;apple&#39;: 3, &#39;banana&#39;: 1, &#39;cherry&#39;: 2}<\/p>\n<p>sorted_by_key = dict(sorted(my_dict.items()))<\/p>\n<p>print(sorted_by_key)  # \u8f93\u51fa: {&#39;apple&#39;: 3, &#39;banana&#39;: 1, &#39;cherry&#39;: 2}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>sorted()<\/code> \u51fd\u6570\u5bf9 <code>my_dict.items()<\/code> \u8fd4\u56de\u7684\u952e\u503c\u5bf9\u5217\u8868\u6309\u952e\u8fdb\u884c\u6392\u5e8f\u3002\u6392\u5e8f\u540e\u7684\u7ed3\u679c\u88ab\u8f6c\u6362\u56de\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>2. \u6309\u5b57\u5178\u503c\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u6309\u5b57\u5178\u7684\u503c\u5bf9\u5176\u6392\u5e8f\uff0c\u53ef\u4ee5\u5728 <code>sorted()<\/code> \u51fd\u6570\u4e2d\u4f7f\u7528 <code>key<\/code> \u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sorted_by_value = dict(sorted(my_dict.items(), key=lambda item: item[1]))<\/p>\n<p>print(sorted_by_value)  # \u8f93\u51fa: {&#39;banana&#39;: 1, &#39;cherry&#39;: 2, &#39;apple&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<code>lambda item: item[1]<\/code> \u6307\u5b9a\u4e86\u6392\u5e8f\u7684\u5173\u952e\u662f\u5b57\u5178\u9879\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u70b9\u548c\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u5b9e\u73b0\u5404\u79cd\u6392\u5e8f\u9700\u6c42\u3002<\/li>\n<li><strong>\u7b80\u5355\u6613\u7528<\/strong>\uff1a\u9002\u5408\u5feb\u901f\u5b9e\u73b0\u6392\u5e8f\u9700\u6c42\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><p>\u4e8c\u3001\u4f7f\u7528 <code>operator<\/code> \u6a21\u5757\u5bf9\u5b57\u5178\u6392\u5e8f<\/p>\n<\/p>\n<p><p><code>operator<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u4fbf\u6377\u7684\u51fd\u6570\u6765\u8fdb\u884c\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u4f7f\u7528 <code>operator.itemgetter()<\/code> \u53ef\u4ee5\u7b80\u5316\u6309\u503c\u6392\u5e8f\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>1. \u6309\u5b57\u5178\u503c\u6392\u5e8f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from operator import itemgetter<\/p>\n<p>sorted_by_value = dict(sorted(my_dict.items(), key=itemgetter(1)))<\/p>\n<p>print(sorted_by_value)  # \u8f93\u51fa: {&#39;banana&#39;: 1, &#39;cherry&#39;: 2, &#39;apple&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>itemgetter(1)<\/code> \u63d0\u4f9b\u4e86\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u83b7\u53d6\u4f20\u5165\u5bf9\u8c61\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff08\u5373\u5b57\u5178\u7684\u503c\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u70b9\u548c\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<ul>\n<li><strong>\u53ef\u8bfb\u6027\u9ad8<\/strong>\uff1a\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u76f4\u89c2\u3002<\/li>\n<li><strong>\u6548\u7387\u9ad8<\/strong>\uff1a\u9002\u5408\u5bf9\u5927\u6570\u636e\u91cf\u7684\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<\/ul>\n<p><p>\u4e09\u3001\u4f7f\u7528 <code>collections.OrderedDict<\/code> \u4fdd\u6301\u6392\u5e8f\u540e\u7684\u987a\u5e8f<\/p>\n<\/p>\n<p><p>\u5728 Python 3.7 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u666e\u901a\u5b57\u5178\u5df2\u7ecf\u4fdd\u8bc1\u4e86\u63d2\u5165\u987a\u5e8f\uff0c\u4f46\u5728\u65e9\u671f\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>collections.OrderedDict<\/code> \u6765\u7ef4\u62a4\u6392\u5e8f\u540e\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528 <code>OrderedDict<\/code><\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import OrderedDict<\/p>\n<p>ordered_dict = OrderedDict(sorted(my_dict.items()))<\/p>\n<p>print(ordered_dict)  # \u8f93\u51fa: OrderedDict([(&#39;apple&#39;, 3), (&#39;banana&#39;, 1), (&#39;cherry&#39;, 2)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u548c\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<ul>\n<li><strong>\u517c\u5bb9\u6027\u597d<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u4e0e\u65e9\u671f Python \u7248\u672c\u517c\u5bb9\u7684\u9879\u76ee\u3002<\/li>\n<li><strong>\u7ef4\u62a4\u987a\u5e8f<\/strong>\uff1a\u786e\u4fdd\u5b57\u5178\u5728\u63d2\u5165\u548c\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u4fdd\u6301\u987a\u5e8f\u3002<\/li>\n<\/ul>\n<p><p>\u56db\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u590d\u6742\u6392\u5e8f<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5bf9\u5b57\u5178\u8fdb\u884c\u590d\u6742\u7684\u6392\u5e8f\uff0c\u6bd4\u5982\u5148\u6309\u952e\u6392\u5e8f\u518d\u6309\u503c\u6392\u5e8f\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>1. \u5148\u6309\u952e\u518d\u6309\u503c\u6392\u5e8f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">complex_dict = {&#39;apple&#39;: 2, &#39;banana&#39;: 3, &#39;avocado&#39;: 1}<\/p>\n<h2><strong>\u5148\u6309\u952e\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_complex_dict = sorted(complex_dict.items())<\/p>\n<h2><strong>\u7136\u540e\u6309\u503c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_complex_dict = sorted(sorted_complex_dict, key=lambda item: item[1])<\/p>\n<h2><strong>\u8f6c\u6362\u56de\u5b57\u5178<\/strong><\/h2>\n<p>sorted_complex_dict = dict(sorted_complex_dict)<\/p>\n<p>print(sorted_complex_dict)  # \u8f93\u51fa: {&#39;avocado&#39;: 1, &#39;apple&#39;: 2, &#39;banana&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u548c\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u5ea6\u81ea\u5b9a\u4e49<\/strong>\uff1a\u9002\u5408\u9700\u8981\u590d\u6742\u6392\u5e8f\u903b\u8f91\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\u5f3a<\/strong>\uff1a\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4efb\u610f\u7ec4\u5408\u6392\u5e8f\u6761\u4ef6\u3002<\/li>\n<\/ul>\n<p><p>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879\u548c\u6027\u80fd\u8003\u8651<\/p>\n<\/p>\n<p><h3>1. \u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u6392\u5e8f\u5728\u6027\u80fd\u4e0a\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002<code>sorted()<\/code> \u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a <code>O(n log n)<\/code>\uff0c\u5176\u4e2d <code>n<\/code> \u662f\u5b57\u5178\u7684\u9879\u6570\u3002\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u5b57\u5178\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6392\u5e8f\u64cd\u4f5c\u7684\u6027\u80fd\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><h3>2. Python \u7248\u672c\u5dee\u5f02<\/h3>\n<\/p>\n<p><p>\u4ece Python 3.7 \u5f00\u59cb\uff0c\u5b57\u5178\u9ed8\u8ba4\u4fdd\u7559\u4e86\u63d2\u5165\u987a\u5e8f\u3002\u5728\u8fd9\u4e4b\u524d\uff0c\u666e\u901a\u5b57\u5178\u4e0d\u4fdd\u8bc1\u987a\u5e8f\uff0c\u9700\u8981\u4f7f\u7528 <code>OrderedDict<\/code> \u6765\u5b9e\u73b0\u3002\u8fd9\u4e00\u70b9\u5728\u5904\u7406\u4e0d\u540c\u7248\u672c\u7684 Python \u65f6\u9700\u8981\u6ce8\u610f\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u662f Python \u4e2d\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u7406\u89e3\u5982\u4f55\u4f7f\u7528\u4e0d\u540c\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u66f4\u52a0\u9ad8\u6548\u548c\u7075\u6d3b\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6309\u952e\u6216\u6309\u503c\u6392\u5e8f\uff0c\u8fd8\u662f\u590d\u6742\u7684\u591a\u6761\u4ef6\u6392\u5e8f\uff0cPython \u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u6ee1\u8db3\u8fd9\u4e9b\u9700\u6c42\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728\u5404\u79cd\u573a\u666f\u4e2d\u5b9e\u73b0\u6700\u4f73\u7684\u6392\u5e8f\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\u3002\u901a\u8fc7<code>sorted()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u6392\u597d\u5e8f\u7684\u952e\u5217\u8868\u3002\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>dict<\/code>\u6784\u9020\u51fd\u6570\u5c06\u5176\u8f6c\u56de\u5b57\u5178\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;b&#39;: 2, &#39;a&#39;: 1, &#39;c&#39;: 3}\nsorted_dict = {key: my_dict[key] for key in sorted(my_dict.keys())}\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u80fd\u5f97\u5230\u4e00\u4e2a\u6309\u952e\u6392\u5e8f\u7684\u65b0\u5b57\u5178\u3002<\/p>\n<p><strong>\u5b57\u5178\u7684\u503c\u4e5f\u53ef\u4ee5\u8fdb\u884c\u6392\u5e8f\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u6839\u636e\u5b57\u5178\u7684\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>key<\/code>\u53c2\u6570\u6307\u5b9a\u6392\u5e8f\u7684\u4f9d\u636e\u3002\u4f8b\u5982\uff0c\u82e5\u60f3\u6839\u636e\u503c\u6392\u5e8f\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">sorted_by_value = dict(sorted(my_dict.items(), key=lambda item: item[1]))\n<\/code><\/pre>\n<p>\u8fd9\u6837\u4fbf\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6839\u636e\u5b57\u5178\u503c\u5347\u5e8f\u6392\u5217\u7684\u65b0\u5b57\u5178\u3002<\/p>\n<p><strong>\u5982\u4f55\u5bf9\u5b57\u5178\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\uff1f<\/strong><br \/>\u82e5\u9700\u8981\u5bf9\u5b57\u5178\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\uff0c\u53ef\u4ee5\u5728<code>sorted()<\/code>\u51fd\u6570\u4e2d\u4f7f\u7528<code>reverse=True<\/code>\u53c2\u6570\u3002\u65e0\u8bba\u662f\u6839\u636e\u952e\u8fd8\u662f\u503c\u90fd\u53ef\u4ee5\u4f7f\u7528\u6b64\u53c2\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">sorted_dict_desc = dict(sorted(my_dict.items(), key=lambda item: item[1], reverse=True))\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u6309\u503c\u964d\u5e8f\u6392\u5217\u7684\u5b57\u5178\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528sorted()\u51fd\u6570\u3001\u5229\u7528operator\u6a21\u5757\u3001\u901a\u8fc7c [&hellip;]","protected":false},"author":3,"featured_media":1012688,"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\/1012675"}],"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=1012675"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012675\/revisions"}],"predecessor-version":[{"id":1012689,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012675\/revisions\/1012689"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1012688"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1012675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1012675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1012675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}