{"id":1019042,"date":"2024-12-27T12:50:02","date_gmt":"2024-12-27T04:50:02","guid":{"rendered":""},"modified":"2024-12-27T12:50:04","modified_gmt":"2024-12-27T04:50:04","slug":"python%e5%88%97%e8%a1%a8%e4%b8%ad%e5%a6%82%e4%bd%95%e5%8d%87%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1019042.html","title":{"rendered":"Python\u5217\u8868\u4e2d\u5982\u4f55\u5347\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25161609\/da3466d5-08ea-46e2-96b1-fda0cfbafc7d.webp\" alt=\"Python\u5217\u8868\u4e2d\u5982\u4f55\u5347\u5e8f\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5c06\u5217\u8868\u5347\u5e8f\u6392\u5217\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4e3b\u8981\u5305\u62ec<strong>\u4f7f\u7528sort()\u65b9\u6cd5\u3001\u4f7f\u7528sorted()\u51fd\u6570\u3001\u4ee5\u53ca\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/strong>\u3002\u5176\u4e2d\uff0c<code>sort()<\/code>\u65b9\u6cd5\u662f\u5bf9\u5217\u8868\u8fdb\u884c\u539f\u5730\u6392\u5e8f\uff0c\u800c<code>sorted()<\/code>\u51fd\u6570\u5219\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u540e\u7684\u5217\u8868\u3002<strong>\u4f7f\u7528\u5185\u7f6e\u7684<code>sort()<\/code>\u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f<\/strong>\u3002<code>sort()<\/code>\u65b9\u6cd5\u4f1a\u5bf9\u5217\u8868\u672c\u8eab\u8fdb\u884c\u6392\u5e8f\uff0c\u4e0d\u4f1a\u521b\u5efa\u65b0\u7684\u5217\u8868\u5bf9\u8c61\uff0c\u56e0\u6b64\u5728\u5904\u7406\u5927\u578b\u5217\u8868\u65f6\uff0c\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5\u53ef\u80fd\u66f4\u8282\u7701\u5185\u5b58\u8d44\u6e90\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u5e76\u63a2\u8ba8\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528 <code>sort()<\/code> \u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>sort()<\/code>\u65b9\u6cd5\u662fPython\u5217\u8868\u5bf9\u8c61\u81ea\u5e26\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u4e8e\u5bf9\u5217\u8868\u8fdb\u884c\u5c31\u5730\u6392\u5e8f\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4e0d\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u800c\u662f\u76f4\u63a5\u4fee\u6539\u539f\u59cb\u5217\u8868\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff0c\u53ea\u9700\u8c03\u7528\u5217\u8868\u7684<code>sort()<\/code>\u65b9\u6cd5\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>sort()<\/code>\u65b9\u6cd5\u4f1a\u4ee5\u5347\u5e8f\u6392\u5217\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [5, 2, 9, 1, 5, 6]<\/p>\n<p>my_list.sort()<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u6392\u5e8f<\/strong><\/p>\n<\/p>\n<p><p><code>sort()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7<code>key<\/code>\u53c2\u6570\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\uff0c\u4ee5\u51b3\u5b9a\u6392\u5e8f\u7684\u4f9d\u636e\u3002<code>key<\/code>\u51fd\u6570\u4f1a\u88ab\u5e94\u7528\u5230\u5217\u8868\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a\uff0c<code>sort()<\/code>\u65b9\u6cd5\u6839\u636e<code>key<\/code>\u51fd\u6570\u7684\u8fd4\u56de\u503c\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>my_list.sort(key=len)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u4e32\u88ab\u6309\u957f\u5ea6\u6392\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7a33\u5b9a\u6027<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684<code>sort()<\/code>\u65b9\u6cd5\u662f\u7a33\u5b9a\u7684\uff0c\u8fd9\u610f\u5473\u7740\u5728\u6392\u5e8f\u8fc7\u7a0b\u4e2d\uff0c\u76f8\u7b49\u7684\u5143\u7d20\u4e0d\u4f1a\u6539\u53d8\u5b83\u4eec\u7684\u76f8\u5bf9\u987a\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528 <code>sorted()<\/code> \u51fd\u6570<\/p>\n<\/p>\n<p><p><code>sorted()<\/code>\u51fd\u6570\u4e0e<code>sort()<\/code>\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u540e\u7684\u5217\u8868\uff0c\u800c\u4e0d\u4f1a\u4fee\u6539\u539f\u59cb\u5217\u8868\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u4fdd\u7559\u539f\u59cb\u5217\u8868\u4e0d\u53d8\u7684\u60c5\u51b5\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>sorted()<\/code>\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_list = sorted(my_list)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/p>\n<p>print(my_list)  # \u539f\u59cb\u5217\u8868\u4fdd\u6301\u4e0d\u53d8: [5, 2, 9, 1, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u6392\u5e8f<\/strong><\/p>\n<\/p>\n<p><p><code>sorted()<\/code>\u51fd\u6570\u4e5f\u652f\u6301<code>key<\/code>\u53c2\u6570\uff0c\u4ee5\u4fbf\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>sorted_list = sorted(my_list, key=len)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528<code>reverse<\/code>\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u5e0c\u671b\u4ee5\u964d\u5e8f\u6392\u5217\uff0c\u53ef\u4ee5\u5c06<code>reverse<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>True<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_list = sorted(my_list, reverse=True)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [9, 6, 5, 5, 2, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u590d\u6742\u7684\u89c4\u5219\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd9\u65f6\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u7684\u5b9e\u73b0<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5904\u7406\u590d\u6742\u7684\u6392\u5e8f\u903b\u8f91\uff0c\u7136\u540e\u5c06\u5176\u4f5c\u4e3a<code>key<\/code>\u53c2\u6570\u4f20\u9012\u7ed9<code>sort()<\/code>\u6216<code>sorted()<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_sort(element):<\/p>\n<p>    return element % 2, element<\/p>\n<p>my_list = [5, 2, 9, 1, 5, 6]<\/p>\n<p>my_list.sort(key=custom_sort)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [2, 6, 1, 5, 5, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868\u9996\u5148\u6309\u5947\u5076\u6027\u6392\u5e8f\uff08\u5076\u6570\u5728\u524d\uff09\uff0c\u7136\u540e\u6309\u6570\u5b57\u5927\u5c0f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6392\u5e8f\u7a33\u5b9a\u6027\u4e0e\u590d\u6742\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u65f6\uff0c\u6392\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u590d\u6742\u5ea6\u4f9d\u8d56\u4e8e<code>key<\/code>\u51fd\u6570\u7684\u5b9e\u73b0\u3002\u901a\u5e38\uff0c<code>key<\/code>\u51fd\u6570\u7684\u8ba1\u7b97\u5e94\u8be5\u5c3d\u91cf\u7b80\u5355\uff0c\u4ee5\u907f\u514d\u589e\u52a0\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u5f00\u9500\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u6392\u5e8f\u7b97\u6cd5\u80cc\u540e\u7684\u5b9e\u73b0<\/p>\n<\/p>\n<p><p>Python\u5217\u8868\u7684\u6392\u5e8f\u65b9\u6cd5\u662f\u57fa\u4e8eTimsort\u7b97\u6cd5\uff0c\u8fd9\u662f\u4e00\u79cd\u6df7\u5408\u6392\u5e8f\u7b97\u6cd5\uff0c\u7ed3\u5408\u4e86\u5408\u5e76\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u7684\u4f18\u70b9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Timsort\u7b97\u6cd5\u7b80\u4ecb<\/strong><\/p>\n<\/p>\n<p><p>Timsort\u7b97\u6cd5\u4e13\u4e3a\u5b9e\u7528\u800c\u8bbe\u8ba1\uff0c\u7279\u522b\u9002\u5408\u4e8e\u5904\u7406\u771f\u5b9e\u4e16\u754c\u4e2d\u7684\u6570\u636e\u96c6\u3002\u5b83\u5728Python\u7684<code>list.sort()<\/code>\u548c<code>sorted()<\/code>\u51fd\u6570\u4e2d\u88ab\u91c7\u7528\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u7a33\u5b9a\u6027<\/strong>\uff1aTimsort\u662f\u7a33\u5b9a\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4e24\u4e2a\u76f8\u7b49\u7684\u5143\u7d20\u5728\u6392\u5e8f\u540e\u4fdd\u6301\u76f8\u5bf9\u987a\u5e8f\u4e0d\u53d8\u3002<\/li>\n<li><strong>\u65f6\u95f4\u590d\u6742\u5ea6<\/strong>\uff1a\u5728\u6700\u574f\u60c5\u51b5\u4e0b\uff0cTimsort\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n log n)\u3002<\/li>\n<li><strong>\u7a7a\u95f4\u590d\u6742\u5ea6<\/strong>\uff1aTimsort\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff0c\u8fd9\u4e0e\u5176\u4ed6\u5408\u5e76\u6392\u5e8f\u7b97\u6cd5\u7c7b\u4f3c\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5b9e\u73b0\u7ec6\u8282<\/strong><\/p>\n<\/p>\n<p><p>Timsort\u901a\u8fc7\u8bc6\u522b\u548c\u5229\u7528\u6570\u636e\u4e2d\u7684\u81ea\u7136\u6709\u5e8f\u6027\uff08\u79f0\u4e3a\u201crun\u201d\uff09\u6765\u63d0\u9ad8\u6548\u7387\u3002\u5b83\u5c06\u6570\u7ec4\u5206\u6210\u4e00\u7cfb\u5217\u7684runs\uff0c\u7136\u540e\u4f7f\u7528\u63d2\u5165\u6392\u5e8f\u5bf9\u5c0f\u7684runs\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u4f7f\u7528\u5408\u5e76\u6392\u5e8f\u5bf9\u5927\u7684runs\u8fdb\u884c\u5408\u5e76\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u8bc6\u522bruns<\/strong>\uff1aTimsort\u901a\u8fc7\u626b\u63cf\u6570\u7ec4\u8bc6\u522b\u5df2\u7ecf\u6392\u5e8f\u7684\u5b50\u6570\u7ec4\uff08runs\uff09\u3002<\/li>\n<li><strong>\u5408\u5e76\u7b56\u7565<\/strong>\uff1a\u4e3a\u4e86\u51cf\u5c11\u5408\u5e76\u7684\u6b21\u6570\uff0cTimsort\u4f1a\u52a8\u6001\u9009\u62e9\u5408\u5e76\u65b9\u5f0f\uff0c\u4ee5\u6700\u5c0f\u5316\u8fd0\u884c\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u6392\u5e8f\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u6392\u5e8f\u662f\u6570\u636e\u5904\u7406\u4e2d\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u51e0\u4e4e\u5728\u6240\u6709\u6570\u636e\u5904\u7406\u4efb\u52a1\u4e2d\u90fd\u4f1a\u9047\u5230\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u5206\u6790\u4e0e\u53ef\u89c6\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u8fc7\u7a0b\u4e2d\uff0c\u6392\u5e8f\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u6b65\u9aa4\u3002\u6392\u5e8f\u540e\u7684\u6570\u636e\u66f4\u5bb9\u6613\u8fdb\u884c\u5206\u6790\uff0c\u4f8b\u5982\u8bc6\u522b\u6700\u9ad8\u6216\u6700\u4f4e\u503c\uff0c\u6216\u8005\u627e\u51fa\u7279\u5b9a\u503c\u7684\u6392\u540d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u641c\u7d22\u4e0e\u68c0\u7d22<\/strong><\/p>\n<\/p>\n<p><p>\u6392\u5e8f\u662f\u9ad8\u6548\u641c\u7d22\u7b97\u6cd5\uff08\u5982\u4e8c\u5206\u641c\u7d22\uff09\u7684\u5fc5\u8981\u524d\u63d0\u3002\u5bf9\u4e8e\u9700\u8981\u9891\u7e41\u68c0\u7d22\u7684\u7cfb\u7edf\uff08\u5982\u6570\u636e\u5e93\u548c\u641c\u7d22\u5f15\u64ce\uff09\uff0c\u6392\u5e8f\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u6e05\u6d17<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u6e05\u6d17\u8fc7\u7a0b\u4e2d\uff0c\u6392\u5e8f\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u548c\u79fb\u9664\u91cd\u590d\u7684\u6216\u5f02\u5e38\u7684\u6570\u636e\u70b9\u3002\u4f8b\u5982\uff0c\u5728\u53bb\u9664\u91cd\u590d\u503c\u65f6\uff0c\u6392\u5e8f\u540e\u7684\u6570\u636e\u66f4\u5bb9\u6613\u8bc6\u522b\u91cd\u590d\u9879\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7b97\u6cd5\u4f18\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7b97\u6cd5\u4e2d\uff0c\u6392\u5e8f\u53ef\u4ee5\u7b80\u5316\u95ee\u9898\u7684\u590d\u6742\u6027\u6216\u63d0\u9ad8\u7b97\u6cd5\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u6700\u8fd1\u90bb\u641c\u7d22\u7b97\u6cd5\u4e2d\uff0c\u6392\u5e8f\u53ef\u4ee5\u5e2e\u52a9\u51cf\u5c11\u641c\u7d22\u8303\u56f4\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u6392\u5e8f\u7684\u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1Python\u7684\u5185\u7f6e\u6392\u5e8f\u65b9\u6cd5\u5df2\u7ecf\u975e\u5e38\u9ad8\u6548\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u65f6\uff0c\u4ecd\u7136\u6709\u4e00\u4e9b\u7b56\u7565\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u6027\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u53ef\u80fd\u6bd4\u5217\u8868\u66f4\u9ad8\u6548\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>collections.deque<\/code>\u53ef\u4ee5\u5728\u9700\u8981\u9891\u7e41\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u65f6\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u6392\u5e8f<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u6570\u636e\u5df2\u7ecf\u90e8\u5206\u6392\u5e8f\uff0cTimsort\u4f1a\u81ea\u52a8\u5229\u7528\u8fd9\u4e00\u70b9\uff0c\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u5e94\u7528\u4e2d\u6709\u610f\u8bc6\u5730\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u53ea\u9700\u8981\u524d\u51e0\u4e2a\u6700\u5927\u6216\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>heapq<\/code>\u6a21\u5757\u4e2d\u7684<code>nlargest()<\/code>\u548c<code>nsmallest()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e76\u884c\u5316\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8d85\u5927\u89c4\u6a21\u7684\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5b9e\u73b0\u5e76\u884c\u6392\u5e8f\u3002Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f13\u5b58\u548c\u8bb0\u5fc6\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u91cd\u590d\u7684\u8ba1\u7b97\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u6280\u672f\u6765\u5b58\u50a8\u4e2d\u95f4\u7ed3\u679c\uff0c\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>functools.lru_cache<\/code>\u88c5\u9970\u5668\u53ef\u4ee5\u7f13\u5b58\u51fd\u6570\u8c03\u7528\u7ed3\u679c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u6392\u5e8f\u4e2d\u7684\u5e38\u89c1\u95ee\u9898\u4e0e\u8bef\u533a<\/p>\n<\/p>\n<p><p>\u6392\u5e8f\u64cd\u4f5c\u4e2d\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\u548c\u8bef\u533a\uff0c\u4e86\u89e3\u8fd9\u4e9b\u95ee\u9898\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5904\u7406\u6392\u5e8f\u4efb\u52a1\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bef\u7528<code>key<\/code>\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>key<\/code>\u53c2\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u8bef\u4ee5\u4e3a<code>key<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u800c\u5b9e\u9645\u5e94\u8be5\u8fd4\u56de\u4e00\u4e2a\u7528\u4e8e\u6bd4\u8f83\u7684\u503c\u3002\u4f8b\u5982\uff0c<code>key=lambda x: x % 2<\/code>\u8fd4\u56de\u7684\u662f\u4f59\u6570\u800c\u4e0d\u662f\u5e03\u5c14\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5ffd\u7565\u6392\u5e8f\u7a33\u5b9a\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u4e2d\uff0c\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\u975e\u5e38\u91cd\u8981\u3002\u5982\u679c\u5728\u6392\u5e8f\u4e2d\u5ffd\u7565\u4e86\u7a33\u5b9a\u6027\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u5ea6<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u5f00\u53d1\u8005\u53ef\u80fd\u4f1a\u4f7f\u7528\u8fc7\u4e8e\u590d\u6742\u7684\u81ea\u5b9a\u4e49\u6392\u5e8f\u903b\u8f91\uff0c\u800c\u5b9e\u9645\u9700\u6c42\u53ef\u80fd\u53ea\u9700\u8981\u7b80\u5355\u7684\u5347\u5e8f\u6216\u964d\u5e8f\u6392\u5217\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5bf9Python\u5217\u8868\u7684\u6392\u5e8f\u65b9\u6cd5\u8fdb\u884c\u6df1\u5165\u7814\u7a76\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\uff0c\u4f18\u5316\u7b97\u6cd5\u6027\u80fd\uff0c\u5e76\u89e3\u51b3\u5404\u79cd\u5b9e\u9645\u95ee\u9898\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u5185\u7f6e\u7684<code>sort()<\/code>\u65b9\u6cd5\u8fd8\u662f<code>sorted()<\/code>\u51fd\u6570\uff0c\u90fd\u80fd\u591f\u5728\u5404\u79cd\u5e94\u7528\u573a\u666f\u4e2d\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5217\u8868\u4e2d\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sort()<\/code>\u65b9\u6cd5\u6765\u5bf9\u5217\u8868\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\u3002\u6b64\u65b9\u6cd5\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u59cb\u5217\u8868\u3002\u5982\u679c\u60a8\u5e0c\u671b\u4fdd\u7559\u539f\u59cb\u5217\u8868\u800c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [5, 2, 9, 1]\nmy_list.sort()  # \u539f\u5730\u6392\u5e8f\nprint(my_list)  # \u8f93\u51fa: [1, 2, 5, 9]\n\nsorted_list = sorted(my_list)  # \u751f\u6210\u65b0\u5217\u8868\nprint(sorted_list)  # \u8f93\u51fa: [1, 2, 5, 9]\n<\/code><\/pre>\n<p><strong>\u5728\u5217\u8868\u4e2d\u5982\u4f55\u5904\u7406\u5305\u542b\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u5982\u679c\u5217\u8868\u5305\u542b\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u548c\u6570\u5b57\uff0c\u76f4\u63a5\u4f7f\u7528<code>sort()<\/code>\u6216<code>sorted()<\/code>\u53ef\u80fd\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3aPython\u65e0\u6cd5\u786e\u5b9a\u5982\u4f55\u6bd4\u8f83\u8fd9\u4e9b\u4e0d\u540c\u7c7b\u578b\u7684\u5143\u7d20\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u60a8\u53ef\u4ee5\u5148\u8fc7\u6ee4\u6216\u8f6c\u6362\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u4f7f\u5176\u4e00\u81f4\u3002\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u5143\u7d20\u90fd\u4e3a\u53ef\u6bd4\u8f83\u7c7b\u578b\uff0c\u6bd4\u5982\u53ea\u5305\u542b\u6570\u5b57\u6216\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5982\u4f55\u5bf9\u5217\u8868\u4e2d\u7684\u5bf9\u8c61\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5305\u542b\u5bf9\u8c61\u7684\u5217\u8868\u65f6\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>key<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u6392\u5e8f\u7684\u4f9d\u636e\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u6839\u636e\u5b57\u5178\u4e2d\u7684\u67d0\u4e2a\u952e\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<pre><code class=\"language-python\">people = [{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}, {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 25}]\npeople.sort(key=lambda x: x[&#39;age&#39;])  # \u6839\u636e\u5e74\u9f84\u5347\u5e8f\u6392\u5e8f\nprint(people)  # \u8f93\u51fa: [{&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 25}, {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u8ba9\u60a8\u80fd\u591f\u7075\u6d3b\u5730\u6839\u636e\u5bf9\u8c61\u7684\u5c5e\u6027\u6765\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5c06\u5217\u8868\u5347\u5e8f\u6392\u5217\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528sort()\u65b9\u6cd5\u3001\u4f7f\u7528sorted()\u51fd\u6570\u3001\u4ee5\u53ca\u81ea [&hellip;]","protected":false},"author":3,"featured_media":1019046,"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\/1019042"}],"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=1019042"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019042\/revisions"}],"predecessor-version":[{"id":1019049,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019042\/revisions\/1019049"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1019046"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1019042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1019042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1019042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}