{"id":1176780,"date":"2025-01-15T17:49:24","date_gmt":"2025-01-15T09:49:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1176780.html"},"modified":"2025-01-15T17:49:26","modified_gmt":"2025-01-15T09:49:26","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e9%9b%86%e5%90%88%e6%8e%92%e5%ba%8fpython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1176780.html","title":{"rendered":"\u5982\u4f55\u5c06\u96c6\u5408\u6392\u5e8fpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25111853\/6b88341d-c31c-49cf-a49d-ccdbcf9e837d.webp\" alt=\"\u5982\u4f55\u5c06\u96c6\u5408\u6392\u5e8fpython\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528sorted()\u51fd\u6570\u3001\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\u540e\u4f7f\u7528sort()\u65b9\u6cd5\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528sorted()\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\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\u662f\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\u7684\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\u3002<code>sorted()<\/code>\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u6309\u987a\u5e8f\u6392\u5217\u7684\u96c6\u5408\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_set = {3, 1, 4, 2}<\/p>\n<p>sorted_list = sorted(my_set)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p><code>sorted()<\/code>\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sorted(iterable, key=None, reverse=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>iterable<\/code>\uff1a\u8981\u6392\u5e8f\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5728\u8fd9\u91cc\u662f\u96c6\u5408\uff09\u3002<\/li>\n<li><code>key<\/code>\uff1a\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u6307\u5b9a\u6839\u636e\u54ea\u4e2a\u5173\u952e\u5b57\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<li><code>reverse<\/code>\uff1a\u5982\u679c\u4e3a<code>True<\/code>\uff0c\u5219\u7ed3\u679c\u5217\u8868\u5c06\u6309\u964d\u5e8f\u6392\u5217\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u5bf9\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_set = {&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;}<\/p>\n<p>sorted_list = sorted(my_set, key=len)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [&#39;date&#39;, &#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\u540e\u4f7f\u7528sort()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u7531\u4e8e\u96c6\u5408\u672c\u8eab\u662f\u65e0\u5e8f\u7684\uff0c\u4e0d\u652f\u6301\u76f4\u63a5\u6392\u5e8f\u64cd\u4f5c\uff0c\u56e0\u6b64\u53ef\u4ee5\u5148\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_set = {3, 1, 4, 2}<\/p>\n<p>my_list = list(my_set)<\/p>\n<p>my_list.sort()<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684<code>sort()<\/code>\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\u3002<code>sort()<\/code>\u65b9\u6cd5\u4e0e<code>sorted()<\/code>\u51fd\u6570\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4f1a\u5bf9\u5217\u8868\u672c\u8eab\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u4e0d\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list.sort(key=None, reverse=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>key<\/code>\uff1a\u4e0e<code>sorted()<\/code>\u51fd\u6570\u76f8\u540c\uff0c\u6307\u5b9a\u6839\u636e\u54ea\u4e2a\u5173\u952e\u5b57\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<li><code>reverse<\/code>\uff1a\u4e0e<code>sorted()<\/code>\u51fd\u6570\u76f8\u540c\uff0c\u5982\u679c\u4e3a<code>True<\/code>\uff0c\u5219\u6309\u964d\u5e8f\u6392\u5e8f\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_set = {3, 1, 4, 2}<\/p>\n<p>my_list = list(my_set)<\/p>\n<p>my_list.sort(reverse=True)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [4, 3, 2, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u6839\u636e\u81ea\u5b9a\u4e49\u7684\u89c4\u5219\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\u3002\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u7684<code>key<\/code>\u53c2\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>def custom_sort(x):<\/p>\n<p>    return x % 2, x<\/p>\n<p>my_set = {3, 1, 4, 2}<\/p>\n<p>sorted_list = sorted(my_set, key=custom_sort)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [2, 4, 1, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5bf9\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<code>custom_sort<\/code>\u9996\u5148\u6839\u636e\u5143\u7d20\u7684\u5947\u5076\u6027\u6392\u5e8f\uff0c\u7136\u540e\u6839\u636e\u5143\u7d20\u7684\u503c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_sort(x):<\/p>\n<p>    return (x % 2, x)<\/p>\n<p>my_set = {3, 1, 4, 2}<\/p>\n<p>sorted_list = sorted(my_set, key=custom_sort)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [2, 4, 1, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>custom_sort<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff0c\u5143\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f<code>x % 2<\/code>\uff08\u5947\u5076\u6027\uff09\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u662f<code>x<\/code>\uff08\u5143\u7d20\u7684\u503c\uff09\u3002<code>sorted()<\/code>\u51fd\u6570\u6839\u636e\u8fd9\u4e2a\u5143\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u5168\u9762\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>def custom_sort(x):<\/p>\n<p>    return (x % 2, x)<\/p>\n<p>my_set = {3, 1, 4, 2}<\/p>\n<h2><strong>\u4f7f\u7528sorted()\u51fd\u6570<\/strong><\/h2>\n<p>sorted_list_1 = sorted(my_set)<\/p>\n<p>print(&quot;\u4f7f\u7528sorted()\u51fd\u6570\u6392\u5e8f:&quot;, sorted_list_1)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<h2><strong>\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\u540e\u4f7f\u7528sort()\u65b9\u6cd5<\/strong><\/h2>\n<p>my_list = list(my_set)<\/p>\n<p>my_list.sort()<\/p>\n<p>print(&quot;\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\u540e\u4f7f\u7528sort()\u65b9\u6cd5\u6392\u5e8f:&quot;, my_list)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/strong><\/h2>\n<p>sorted_list_2 = sorted(my_set, key=custom_sort)<\/p>\n<p>print(&quot;\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u6392\u5e8f:&quot;, sorted_list_2)  # \u8f93\u51fa: [2, 4, 1, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u60a8\u53ef\u4ee5\u6e05\u695a\u5730\u770b\u5230\u5982\u4f55\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6392\u5e8f\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u867d\u7136\u96c6\u5408\u901a\u5e38\u5305\u542b\u7684\u6570\u636e\u91cf\u4e0d\u4f1a\u7279\u522b\u5927\uff0c\u4f46\u4e86\u89e3\u4e0d\u540c\u6392\u5e8f\u65b9\u6cd5\u7684\u6027\u80fd\u5dee\u5f02\u4ecd\u7136\u5f88\u6709\u4ef7\u503c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528sorted()\u51fd\u6570\u7684\u6027\u80fd<\/h4>\n<\/p>\n<p><p><code>sorted()<\/code>\u51fd\u6570\u662f\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\uff0c\u4f7f\u7528\u7684\u662fTimsort\u7b97\u6cd5\u3002Timsort\u662f\u4e00\u79cd\u6df7\u5408\u6392\u5e8f\u7b97\u6cd5\uff0c\u7ed3\u5408\u4e86\u5f52\u5e76\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u7684\u4f18\u52bf\u3002\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n log n)\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u6027\u80fd\u4f18\u8d8a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>my_set = {i for i in range(1000000, 0, -1)}<\/p>\n<p>start_time = time.time()<\/p>\n<p>sorted_list = sorted(my_set)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;\u4f7f\u7528sorted()\u51fd\u6570\u6392\u5e8f\u7684\u65f6\u95f4:&quot;, end_time - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\u540e\u4f7f\u7528sort()\u65b9\u6cd5\u7684\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\u540e\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5\u7684\u6027\u80fd\u4e0e<code>sorted()<\/code>\u51fd\u6570\u7c7b\u4f3c\uff0c\u56e0\u4e3a<code>sort()<\/code>\u65b9\u6cd5\u4e5f\u662f\u57fa\u4e8eTimsort\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>my_set = {i for i in range(1000000, 0, -1)}<\/p>\n<p>my_list = list(my_set)<\/p>\n<p>start_time = time.time()<\/p>\n<p>my_list.sort()<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\u540e\u4f7f\u7528sort()\u65b9\u6cd5\u6392\u5e8f\u7684\u65f6\u95f4:&quot;, end_time - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u7684\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u65f6\uff0c\u6027\u80fd\u4e3b\u8981\u53d6\u51b3\u4e8e\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u590d\u6742\u5ea6\u3002\u5c3d\u91cf\u4fdd\u8bc1\u81ea\u5b9a\u4e49\u51fd\u6570\u7b80\u5355\u9ad8\u6548\uff0c\u4ee5\u51cf\u5c11\u6392\u5e8f\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def custom_sort(x):<\/p>\n<p>    return x % 2, x<\/p>\n<p>my_set = {i for i in range(1000000, 0, -1)}<\/p>\n<p>start_time = time.time()<\/p>\n<p>sorted_list = sorted(my_set, key=custom_sort)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u6392\u5e8f\u7684\u65f6\u95f4:&quot;, end_time - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u6bd4\u4ee5\u4e0a\u4e09\u79cd\u65b9\u6cd5\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u53d1\u73b0<code>sorted()<\/code>\u51fd\u6570\u548c\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\u540e\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5\u7684\u6027\u80fd\u76f8\u5f53\uff0c\u800c\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u7684\u6027\u80fd\u53d6\u51b3\u4e8e\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6392\u5e8f\u96c6\u5408\u7684\u9700\u6c42\u53ef\u80fd\u4f1a\u51fa\u73b0\u5728\u6570\u636e\u5904\u7406\u3001\u5206\u6790\u3001\u53ef\u89c6\u5316\u7b49\u591a\u4e2a\u9886\u57df\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5b9e\u9645\u5e94\u7528\u573a\u666f\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u96c6\u5408\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee5\u4fbf\u8fdb\u4e00\u6b65\u5206\u6790\u6216\u5904\u7406\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5bf9\u4e00\u7ec4\u4f20\u611f\u5668\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee5\u627e\u5230\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sensor_data = {23, 45, 12, 67, 34, 89, 56}<\/p>\n<p>sorted_data = sorted(sensor_data)<\/p>\n<p>print(&quot;\u6392\u5e8f\u540e\u7684\u4f20\u611f\u5668\u6570\u636e:&quot;, sorted_data)  # \u8f93\u51fa: [12, 23, 34, 45, 56, 67, 89]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0c\u6392\u5e8f\u96c6\u5408\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u8f7b\u677e\u5730\u8bc6\u522b\u8d8b\u52bf\u548c\u6a21\u5f0f\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5bf9\u80a1\u7968\u4ef7\u683c\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee5\u4fbf\u67e5\u770b\u4ef7\u683c\u53d8\u5316\u7684\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">stock_prices = {120, 115, 130, 125, 110, 135}<\/p>\n<p>sorted_prices = sorted(stock_prices)<\/p>\n<p>print(&quot;\u6392\u5e8f\u540e\u7684\u80a1\u7968\u4ef7\u683c:&quot;, sorted_prices)  # \u8f93\u51fa: [110, 115, 120, 125, 130, 135]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6570\u636e\u53ef\u89c6\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u53ef\u89c6\u5316\u8fc7\u7a0b\u4e2d\uff0c\u6392\u5e8f\u96c6\u5408\u53ef\u4ee5\u5e2e\u52a9\u60a8\u521b\u5efa\u66f4\u6613\u4e8e\u7406\u89e3\u7684\u56fe\u8868\u548c\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5bf9\u4eba\u53e3\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee5\u4fbf\u751f\u6210\u4e00\u4e2a\u6309\u4eba\u53e3\u5927\u5c0f\u6392\u5217\u7684\u67f1\u72b6\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>population_data = {&quot;City A&quot;: 500000, &quot;City B&quot;: 1500000, &quot;City C&quot;: 1000000}<\/p>\n<p>sorted_population = sorted(population_data.items(), key=lambda x: x[1])<\/p>\n<p>cities = [item[0] for item in sorted_population]<\/p>\n<p>populations = [item[1] for item in sorted_population]<\/p>\n<p>plt.bar(cities, populations)<\/p>\n<p>plt.xlabel(&#39;City&#39;)<\/p>\n<p>plt.ylabel(&#39;Population&#39;)<\/p>\n<p>plt.title(&#39;Population by City&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\u7684\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u3001\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\u540e\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u3002\u6211\u4eec\u8fd8\u8ba8\u8bba\u4e86\u6bcf\u79cd\u65b9\u6cd5\u7684\u8be6\u7ec6\u5b9e\u73b0\u3001\u6027\u80fd\u6bd4\u8f83\u548c\u5b9e\u9645\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u548c\u63a8\u8350\u7684\u65b9\u6cd5<\/strong>\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u3001\u76f4\u89c2\u3001\u6027\u80fd\u4f18\u8d8a\u3002\u5728\u9700\u8981\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>key<\/code>\u53c2\u6570\u6307\u5b9a\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u3002\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\u540e\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\u4e5f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\uff0c\u4ece\u800c\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u60a8\u5728Python\u7f16\u7a0b\u4e2d\u5904\u7406\u96c6\u5408\u6392\u5e8f\u7684\u95ee\u9898\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u96c6\u5408\u672c\u8eab\u662f\u65e0\u5e8f\u7684\uff0c\u56e0\u6b64\u4e0d\u80fd\u76f4\u63a5\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\u3002\u4f46\u53ef\u4ee5\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u6216<code>list.sort()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<code>sorted()<\/code>\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u800c<code>list.sort()<\/code>\u4f1a\u5bf9\u539f\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">my_set = {3, 1, 4, 2}\nsorted_list = sorted(my_set)  # \u8fd4\u56de [1, 2, 3, 4]\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5bf9\u96c6\u5408\u4e2d\u7684\u81ea\u5b9a\u4e49\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u8981\u5bf9\u96c6\u5408\u4e2d\u7684\u81ea\u5b9a\u4e49\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\uff0c\u60a8\u9700\u8981\u5b9a\u4e49\u6392\u5e8f\u7684\u5173\u952e\u5b57\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>key<\/code>\u53c2\u6570\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5305\u542b\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<pre><code class=\"language-python\">class Item:\n    def __init__(self, name, value):\n        self.name = name\n        self.value = value\n\nmy_set = {Item(&#39;apple&#39;, 2), Item(&#39;banana&#39;, 1), Item(&#39;cherry&#39;, 3)}\nsorted_list = sorted(my_set, key=lambda x: x.value)  # \u6839\u636evalue\u6392\u5e8f\n<\/code><\/pre>\n<p><strong>\u96c6\u5408\u6392\u5e8f\u540e\u662f\u5426\u4f1a\u5f71\u54cd\u539f\u96c6\u5408\uff1f<\/strong><br \/>\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\u65f6\uff0c\u539f\u96c6\u5408\u4e0d\u4f1a\u88ab\u6539\u53d8\u3002\u56e0\u4e3a\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u6392\u5e8f\u7684\u7ed3\u679c\u901a\u5e38\u4f1a\u5b58\u50a8\u5728\u65b0\u7684\u5217\u8868\u4e2d\u3002\u5982\u679c\u5e0c\u671b\u4fdd\u6301\u539f\u96c6\u5408\u4e0d\u53d8\uff0c\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u662f\u6700\u4f73\u9009\u62e9\u3002\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u800c\u4e0d\u4f1a\u4fee\u6539\u539f\u59cb\u96c6\u5408\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">my_set = {3, 1, 4, 2}\nsorted_list = sorted(my_set)  # \u539f\u96c6\u5408\u4ecd\u4e3a {1, 2, 3, 4}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528sorted()\u51fd\u6570\u3001\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\u540e\u4f7f\u7528sort()\u65b9\u6cd5\u3001\u4f7f\u7528\u81ea [&hellip;]","protected":false},"author":3,"featured_media":1176792,"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\/1176780"}],"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=1176780"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176780\/revisions"}],"predecessor-version":[{"id":1176795,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176780\/revisions\/1176795"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1176792"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1176780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1176780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1176780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}