{"id":1023218,"date":"2024-12-30T14:03:10","date_gmt":"2024-12-30T06:03:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1023218.html"},"modified":"2024-12-30T14:03:12","modified_gmt":"2024-12-30T06:03:12","slug":"%e4%bd%bf%e7%94%a8python%e5%a6%82%e4%bd%95%e6%8e%92%e5%ba%8f%e5%ad%97%e5%85%b8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1023218.html","title":{"rendered":"\u4f7f\u7528python\u5982\u4f55\u6392\u5e8f\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/459de91a-7b4b-4986-b2c8-7546ce16f48b.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u4f7f\u7528python\u5982\u4f55\u6392\u5e8f\u5b57\u5178\" \/><\/p>\n<p><p> \u4f7f\u7528Python\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u9700\u6c42\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u6309\u952e\u6392\u5e8f\u3001\u6309\u503c\u6392\u5e8f\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sorted\u3001\u4f7f\u7528lambda\u51fd\u6570<\/strong>\u3002\u5176\u4e2d\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u5229\u7528<code>sorted()<\/code>\u51fd\u6570\u548c<code>lambda<\/code>\u8868\u8fbe\u5f0f\u6765\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u6309\u952e\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b57\u5178\uff0c\u6309\u952e\u6392\u5e8f\u7684\u64cd\u4f5c\u5c31\u662f\u6839\u636e\u5b57\u5178\u7684\u952e\u6765\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>example_dict = {&#39;apple&#39;: 3, &#39;banana&#39;: 2, &#39;cherry&#39;: 5}<\/p>\n<h2><strong>\u6309\u952e\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_dict = dict(sorted(example_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\uff0c<code>sorted()<\/code>\u51fd\u6570\u5c06\u5b57\u5178\u7684\u9879\uff08<code>items()<\/code>\uff09\u6309\u952e\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u5c06\u7ed3\u679c\u91cd\u65b0\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u6309\u503c\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5b57\u5178\u7684\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u503c\u6392\u5e8f<\/p>\n<p>sorted_dict = dict(sorted(example_dict.items(), key=lambda item: item[1]))<\/p>\n<p>print(sorted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>sorted()<\/code>\u51fd\u6570\u5c06\u5b57\u5178\u7684\u9879\u6309\u503c\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sorted<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u51fd\u6570<code>sorted()<\/code>\u975e\u5e38\u5f3a\u5927\uff0c\u53ef\u4ee5\u5bf9\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u7684<code>items()<\/code>\u65b9\u6cd5\u6765\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u952e\u6392\u5e8f<\/p>\n<p>sorted_dict_keys = sorted(example_dict.items())<\/p>\n<h2><strong>\u6309\u503c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_dict_values = sorted(example_dict.items(), key=lambda item: item[1])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528lambda\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>lambda<\/code>\u51fd\u6570\u662f\u4e00\u79cd\u533f\u540d\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u7684\u5730\u65b9\u4f7f\u7528\uff0c\u901a\u5e38\u7528\u5728\u77ed\u671f\u9700\u6c42\u7684\u573a\u666f\u91cc\u3002\u7ed3\u5408<code>sorted()<\/code>\u51fd\u6570\uff0c<code>lambda<\/code>\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u6211\u4eec\u7684\u9700\u8981\u8fdb\u884c\u5b9a\u5236\u5316\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u952e\u6392\u5e8f<\/p>\n<p>sorted_dict_keys_lambda = dict(sorted(example_dict.items(), key=lambda item: item[0]))<\/p>\n<h2><strong>\u6309\u503c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_dict_values_lambda = dict(sorted(example_dict.items(), key=lambda item: item[1]))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6269\u5c55\uff1a\u6309\u591a\u4e2a\u6761\u4ef6\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u5b57\u5178\u6309\u591a\u4e2a\u6761\u4ef6\u8fdb\u884c\u6392\u5e8f\uff0c\u4f8b\u5982\u5148\u6309\u4e00\u4e2a\u952e\u6392\u5e8f\uff0c\u518d\u6309\u53e6\u4e00\u4e2a\u952e\u6392\u5e8f\u3002\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u5143\u7ec4\u7ed9<code>lambda<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u503c\u7684\u5b57\u5178<\/p>\n<p>multi_criteria_dict = {&#39;apple&#39;: (3, 2), &#39;banana&#39;: (2, 3), &#39;cherry&#39;: (5, 1)}<\/p>\n<h2><strong>\u6309\u591a\u4e2a\u6761\u4ef6\u6392\u5e8f\uff1a\u9996\u5148\u6309\u7b2c\u4e00\u4e2a\u503c\u6392\u5e8f\uff0c\u5176\u6b21\u6309\u7b2c\u4e8c\u4e2a\u503c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_multi_criteria_dict = dict(sorted(multi_criteria_dict.items(), key=lambda item: (item[1][0], item[1][1])))<\/p>\n<p>print(sorted_multi_criteria_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u6309\u952e\u6392\u5e8f\u3001\u6309\u503c\u6392\u5e8f\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sorted\u3001\u4f7f\u7528lambda\u51fd\u6570<\/strong>\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u3002<code>sorted()<\/code>\u51fd\u6570\u7ed3\u5408<code>lambda<\/code>\u8868\u8fbe\u5f0f\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u6839\u636e\u952e\u6216\u503c\u8fdb\u884c\u81ea\u5b9a\u4e49\u6392\u5e8f\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6309\u952e\u6392\u5e8f\u8fd8\u662f\u590d\u6742\u7684\u591a\u6761\u4ef6\u6392\u5e8f\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u6839\u636e\u5177\u4f53\u9700\u6c42\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u8fd9\u4e9b\u6280\u5de7\u6765\u5904\u7406\u6570\u636e\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u5b57\u5178\u6392\u5e8f\u7684\u5404\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6392\u5e8f\u5b57\u5178\u65f6\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u95ee\u9898\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6570\u636e\u7ed3\u6784\u9009\u62e9<\/strong>\uff1a\u5982\u679c\u6392\u5e8f\u64cd\u4f5c\u9891\u7e41\u4e14\u6570\u636e\u91cf\u5927\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>OrderedDict<\/code>\uff08\u6709\u5e8f\u5b57\u5178\uff09\u6216\u8005\u5176\u4ed6\u9002\u5408\u7684\u9ad8\u7ea7\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<li><strong>\u6027\u80fd\u8003\u8651<\/strong>\uff1a\u5bf9\u5927\u89c4\u6a21\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u65f6\uff0c\u6ce8\u610f\u6027\u80fd\u74f6\u9888\u3002<code>sorted()<\/code>\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(n log n)\uff0c\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002<\/li>\n<li><strong>\u6570\u636e\u5b8c\u6574\u6027<\/strong>\uff1a\u6392\u5e8f\u64cd\u4f5c\u4e0d\u4f1a\u6539\u53d8\u539f\u59cb\u5b57\u5178\uff0c\u4f46\u5982\u679c\u9700\u8981\u539f\u5730\u4fee\u6539\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c<code>dict.update()<\/code>\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p><p>\u7406\u89e3\u8fd9\u4e9b\u6ce8\u610f\u4e8b\u9879\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u52a0\u9ad8\u6548\u548c\u51c6\u786e\u5730\u8fdb\u884c\u5b57\u5178\u6392\u5e8f\u64cd\u4f5c\u3002\u5e0c\u671b\u4f60\u80fd\u901a\u8fc7\u672c\u6587\u5b66\u4e60\u5230\u5982\u4f55\u5728Python\u4e2d\u7075\u6d3b\u5730\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u719f\u7ec3\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\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><\/p>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u3002\u53ef\u4ee5\u9009\u62e9\u6309\u952e\u6216\u6309\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u5bf9\u4e8e\u6309\u952e\u6392\u5e8f\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;apple&#39;: 5, &#39;banana&#39;: 3, &#39;orange&#39;: 4}\nsorted_dict = dict(sorted(my_dict.items()))\nprint(sorted_dict)\n<\/code><\/pre>\n<p>\u5982\u679c\u60f3\u6309\u503c\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>lambda<\/code>\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-python\">sorted_dict_by_value = dict(sorted(my_dict.items(), key=lambda item: item[1]))\nprint(sorted_dict_by_value)\n<\/code><\/pre>\n<p><strong>\u5b57\u5178\u7684\u6392\u5e8f\u7ed3\u679c\u4f1a\u5f71\u54cd\u539f\u5b57\u5178\u5417\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u65e0\u5e8f\u7684\uff0c\u6392\u5e8f\u64cd\u4f5c\u4e0d\u4f1a\u6539\u53d8\u539f\u5b57\u5178\u7684\u987a\u5e8f\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\u3002\u8fd9\u610f\u5473\u7740\u539f\u5b57\u5178\u4fdd\u6301\u4e0d\u53d8\uff0c\u60a8\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u968f\u65f6\u4f7f\u7528\u5b83\u3002\u6392\u5e8f\u540e\u7684\u5b57\u5178\u4f1a\u6839\u636e\u60a8\u9009\u62e9\u7684\u6392\u5e8f\u65b9\u5f0f\u751f\u6210\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u6392\u5e8f\u540e\u7684\u5b57\u5178\uff1f<\/strong><\/p>\n<p>\u6392\u5e8f\u540e\u7684\u5b57\u5178\u662f\u4e00\u4e2a\u6709\u5e8f\u7684\u5b57\u5178\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5e38\u89c4\u7684\u5b57\u5178\u65b9\u6cd5\u8fdb\u884c\u8bbf\u95ee\u548c\u64cd\u4f5c\u3002\u53ef\u4ee5\u904d\u5386\u6392\u5e8f\u540e\u7684\u5b57\u5178\uff0c\u83b7\u53d6\u952e\u503c\u5bf9\uff0c\u6216\u8fdb\u884c\u5176\u4ed6\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">for key, value in sorted_dict.items():\n    print(f&quot;{key}: {value}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u4e8e\u9700\u8981\u6309\u7279\u5b9a\u987a\u5e8f\u5904\u7406\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u901a\u8fc7\u6309\u952e\u6392\u5e8f\u3001\u6309\u503c\u6392\u5e8f\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sorted\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1023228,"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\/1023218"}],"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=1023218"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1023218\/revisions"}],"predecessor-version":[{"id":1023230,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1023218\/revisions\/1023230"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1023228"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1023218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1023218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1023218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}