{"id":1159152,"date":"2025-01-13T18:49:28","date_gmt":"2025-01-13T10:49:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1159152.html"},"modified":"2025-01-13T18:49:31","modified_gmt":"2025-01-13T10:49:31","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e5%ad%97%e5%85%b8%e9%87%8c%e9%9d%a2%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1159152.html","title":{"rendered":"python\u5982\u4f55\u5728\u5b57\u5178\u91cc\u9762\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25200941\/75707605-3a14-44cf-b576-d3769b89b4c1.webp\" alt=\"python\u5982\u4f55\u5728\u5b57\u5178\u91cc\u9762\u6392\u5e8f\" \/><\/p>\n<p><p> \u8981\u5728Python\u5b57\u5178\u91cc\u9762\u6392\u5e8f\uff0c\u53ef\u4ee5<strong>\u4f7f\u7528sorted\u51fd\u6570\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684OrderedDict<\/strong>\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528sorted\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u53ef\u4ee5\u5bf9\u5b57\u5178\u7684<strong>\u952e\u3001\u503c\u3001\u9879<\/strong>\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>1. \u6309\u952e\u6392\u5e8f<\/h4>\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_dict = dict(sorted(my_dict.items()))<\/p>\n<p>print(sorted_dict)  # Output: {&#39;apple&#39;: 3, &#39;banana&#39;: 1, &#39;cherry&#39;: 2}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>sorted()<\/code>\u51fd\u6570\u9ed8\u8ba4\u6309\u952e\u7684\u5b57\u6bcd\u987a\u5e8f\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>2. \u6309\u503c\u6392\u5e8f<\/h4>\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_dict = dict(sorted(my_dict.items(), key=lambda item: item[1]))<\/p>\n<p>print(sorted_dict)  # Output: {&#39;banana&#39;: 1, &#39;cherry&#39;: 2, &#39;apple&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>key=lambda item: item[1]<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u6309\u503c\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>3. \u6309\u9879\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u6309\u7167\u7279\u5b9a\u7684\u903b\u8f91\u6392\u5e8f\uff0c\u53ef\u4ee5\u5728<code>key<\/code>\u53c2\u6570\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\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_dict = dict(sorted(my_dict.items(), key=lambda item: (item[1], item[0])))<\/p>\n<p>print(sorted_dict)  # Output: {&#39;banana&#39;: 1, &#39;cherry&#39;: 2, &#39;apple&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u6211\u4eec\u9996\u5148\u6309\u7167\u503c\u6392\u5e8f\uff0c\u7136\u540e\u5728\u503c\u76f8\u540c\u7684\u60c5\u51b5\u4e0b\u6309\u952e\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5728\u6392\u5e8f\u7684\u540c\u65f6\u751f\u6210\u65b0\u7684\u5b57\u5178\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_dict = {k: v for k, v in sorted(my_dict.items(), key=lambda item: item[1])}<\/p>\n<p>print(sorted_dict)  # Output: {&#39;banana&#39;: 1, &#39;cherry&#39;: 2, &#39;apple&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0e\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u7c7b\u4f3c\uff0c\u4f46\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684OrderedDict<\/h3>\n<\/p>\n<p><p><code>collections<\/code>\u6a21\u5757\u4e2d\u7684<code>OrderedDict<\/code>\u53ef\u4ee5\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u6392\u5e8f\u540e\u4fdd\u6301\u5b57\u5178\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import OrderedDict<\/p>\n<p>my_dict = {&#39;apple&#39;: 3, &#39;banana&#39;: 1, &#39;cherry&#39;: 2}<\/p>\n<p>sorted_dict = OrderedDict(sorted(my_dict.items(), key=lambda item: item[1]))<\/p>\n<p>print(sorted_dict)  # Output: OrderedDict([(&#39;banana&#39;, 1), (&#39;cherry&#39;, 2), (&#39;apple&#39;, 3)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>OrderedDict<\/code>\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u53ef\u80fd\u4f1a\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u9700\u8981\u5bf9\u6392\u5e8f\u540e\u7684\u5b57\u5178\u8fdb\u884c\u8fdb\u4e00\u6b65\u64cd\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u8df5\u4e2d\u7684\u5e94\u7528\u6848\u4f8b<\/h3>\n<\/p>\n<p><h4>1. \u6309\u6210\u7ee9\u6392\u5e8f\u5b66\u751f\u540d\u5355<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5b66\u751f\u540d\u5b57\u548c\u6210\u7ee9\u7684\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u524d\u9762\u63d0\u5230\u7684\u65b9\u6cd5\u5bf9\u5b66\u751f\u6210\u7ee9\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = {&#39;John&#39;: 88, &#39;Emma&#39;: 92, &#39;Ava&#39;: 75, &#39;Olivia&#39;: 85}<\/p>\n<p>sorted_students = dict(sorted(students.items(), key=lambda item: item[1], reverse=True))<\/p>\n<p>print(sorted_students)  # Output: {&#39;Emma&#39;: 92, &#39;John&#39;: 88, &#39;Olivia&#39;: 85, &#39;Ava&#39;: 75}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>reverse=True<\/code>\u53c2\u6570\u6309\u964d\u5e8f\u6392\u5217\u6210\u7ee9\u3002<\/p>\n<\/p>\n<p><h4>2. \u6309\u5546\u54c1\u4ef7\u683c\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u5305\u542b\u5546\u54c1\u540d\u79f0\u548c\u4ef7\u683c\u7684\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u6309\u4ef7\u683c\u5bf9\u5546\u54c1\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">products = {&#39;Laptop&#39;: 1000, &#39;Phone&#39;: 500, &#39;Tablet&#39;: 750}<\/p>\n<p>sorted_products = dict(sorted(products.items(), key=lambda item: item[1]))<\/p>\n<p>print(sorted_products)  # Output: {&#39;Phone&#39;: 500, &#39;Tablet&#39;: 750, &#39;Laptop&#39;: 1000}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u6309\u4ef7\u683c\u4ece\u4f4e\u5230\u9ad8\u6392\u5e8f\u5546\u54c1\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u6392\u5e8f\u5b57\u5178\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u901a\u8fc7<strong>\u4f7f\u7528sorted\u51fd\u6570\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001OrderedDict<\/strong>\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528sorted\u51fd\u6570<\/strong>\uff1a\u9002\u7528\u4e8e\u7b80\u5355\u6392\u5e8f\u9700\u6c42\uff0c\u7075\u6d3b\u6027\u9ad8\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\uff0c\u9002\u7528\u4e8e\u9700\u8981\u751f\u6210\u65b0\u5b57\u5178\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u4f7f\u7528OrderedDict<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u6301\u987a\u5e8f\u7684\u590d\u6742\u64cd\u4f5c\u573a\u666f\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0cPython\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u96c6\uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u548c\u64cd\u4f5c\u5b57\u5178\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u6309\u952e\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\u8fd4\u56de\u4e00\u4e2a\u6392\u5e8f\u540e\u7684\u952e\u5217\u8868\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u91cd\u65b0\u6784\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;c&#39;: 3, &#39;a&#39;: 1, &#39;b&#39;: 2}\nsorted_dict = {k: my_dict[k] for k in sorted(my_dict)}\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>sorted_dict<\/code>\u5c06\u4f1a\u662f\u4e00\u4e2a\u6309\u952e\u5347\u5e8f\u6392\u5217\u7684\u65b0\u5b57\u5178\u3002<\/p>\n<p><strong>\u5728Python\u5b57\u5178\u4e2d\u5982\u4f55\u6309\u503c\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u8981\u6309\u503c\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u4e0e<code>key<\/code>\u53c2\u6570\u7ed3\u5408\uff0c\u6307\u5b9a\u6392\u5e8f\u4f9d\u636e\u4e3a\u5b57\u5178\u7684\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;c&#39;: 3, &#39;a&#39;: 1, &#39;b&#39;: 2}\nsorted_dict = dict(sorted(my_dict.items(), key=lambda item: item[1]))\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u751f\u6210\u4e00\u4e2a\u6309\u503c\u5347\u5e8f\u6392\u5217\u7684\u5b57\u5178\u3002<\/p>\n<p><strong>Python\u5b57\u5178\u6392\u5e8f\u540e\u662f\u5426\u4f1a\u6539\u53d8\u539f\u5b57\u5178\u7684\u987a\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u65e0\u5e8f\u7684\uff0c\u7279\u522b\u662f\u5728Python 3.6\u4e4b\u524d\u7684\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u7684\u63d2\u5165\u987a\u5e8f\u5e76\u6ca1\u6709\u4fdd\u8bc1\u3002\u867d\u7136\u4ecePython 3.7\u5f00\u59cb\uff0c\u5b57\u5178\u7684\u63d2\u5165\u987a\u5e8f\u662f\u4fdd\u6301\u7684\uff0c\u4f46\u4f7f\u7528\u6392\u5e8f\u65b9\u6cd5\u751f\u6210\u7684\u65b0\u5b57\u5178\u662f\u72ec\u7acb\u4e8e\u539f\u5b57\u5178\u7684\uff0c\u56e0\u6b64\u539f\u5b57\u5178\u7684\u987a\u5e8f\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\u3002\u5982\u679c\u9700\u8981\u4fdd\u7559\u539f\u5b57\u5178\uff0c\u53ef\u4ee5\u5c06\u6392\u5e8f\u540e\u7684\u7ed3\u679c\u5b58\u50a8\u5728\u4e00\u4e2a\u65b0\u53d8\u91cf\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u5b57\u5178\u91cc\u9762\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528sorted\u51fd\u6570\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684Or [&hellip;]","protected":false},"author":3,"featured_media":1159159,"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\/1159152"}],"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=1159152"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159152\/revisions"}],"predecessor-version":[{"id":1159161,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159152\/revisions\/1159161"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1159159"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1159152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1159152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1159152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}