{"id":1154899,"date":"2025-01-13T17:56:55","date_gmt":"2025-01-13T09:56:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1154899.html"},"modified":"2025-01-13T17:56:59","modified_gmt":"2025-01-13T09:56:59","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e6%95%b0%e5%ad%97%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1154899.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u6570\u5b57\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25184407\/c5240b86-46c6-4fc2-a878-dd765ae1f106.webp\" alt=\"python\u5982\u4f55\u7ed9\u6570\u5b57\u6392\u5e8f\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u7ed9\u6570\u5b57\u6392\u5e8f\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684sorted()\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u7684sort()\u65b9\u6cd5\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/strong>\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\u5404\u6709\u4f18\u52bf\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u4f7f\u7528\u5185\u7f6e\u7684sorted()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>sorted()\u51fd\u6570<\/strong>\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6392\u5e8f\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u4efb\u610f\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u5b83\u7684\u8bed\u6cd5\u975e\u5e38\u7b80\u5355\uff1a<code>sorted(iterable, key=None, reverse=False)<\/code>\u3002\u5176\u4e2d\uff0c<code>iterable<\/code>\u662f\u8981\u6392\u5e8f\u7684\u5bf9\u8c61\uff0c<code>key<\/code>\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u6307\u5b9a\u6392\u5e8f\u7684\u4f9d\u636e\uff0c<code>reverse<\/code>\u5219\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u662f\u5426\u8981\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528sorted()\u51fd\u6570\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_numbers = sorted(numbers)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>sorted()<\/code>\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5bf9\u6570\u5b57\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>reverse=True<\/code>\u6765\u5b9e\u73b0\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528sorted()\u51fd\u6570\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_numbers = sorted(numbers, reverse=True)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [9, 6, 5, 5, 2, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u7ed9\u6570\u5b57\u6392\u5e8f\u7684\u5404\u79cd\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528sorted()\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>sorted()\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6392\u5e8f\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u4efb\u610f\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u5b83\u7684\u8bed\u6cd5\u975e\u5e38\u7b80\u5355\uff1a<code>sorted(iterable, key=None, reverse=False)<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528sorted()\u51fd\u6570\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_numbers = sorted(numbers)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u964d\u5e8f\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e<code>reverse=True<\/code>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528sorted()\u51fd\u6570\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_numbers = sorted(numbers, reverse=True)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [9, 6, 5, 5, 2, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u81ea\u5b9a\u4e49\u6392\u5e8f\u4f9d\u636e<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e<code>key<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6392\u5e8f\u7684\u4f9d\u636e\u3002<code>key<\/code>\u53c2\u6570\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f1a\u5728\u6392\u5e8f\u65f6\u88ab\u8c03\u7528\uff0c\u7528\u4e8e\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528sorted()\u51fd\u6570\u6309\u7167\u7edd\u5bf9\u503c\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>numbers = [-5, 2, -9, 1, -5, 6]<\/p>\n<p>sorted_numbers = sorted(numbers, key=abs)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [1, 2, -5, -5, 6, -9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u7684sort()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>sort()\u65b9\u6cd5\u662f\u5217\u8868\u5bf9\u8c61\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u4e8e\u5c31\u5730\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u5b83\u4e0d\u8fd4\u56de\u65b0\u7684\u5217\u8868\uff0c\u800c\u662f\u5bf9\u539f\u5217\u8868\u8fdb\u884c\u4fee\u6539\u3002\u5176\u8bed\u6cd5\u4e0esorted()\u51fd\u6570\u7c7b\u4f3c\uff1a<code>list.sort(key=None, reverse=False)<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528sort()\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>numbers.sort()<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u964d\u5e8f\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e<code>reverse=True<\/code>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528sort()\u65b9\u6cd5\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>numbers.sort(reverse=True)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [9, 6, 5, 5, 2, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u81ea\u5b9a\u4e49\u6392\u5e8f\u4f9d\u636e<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e<code>key<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6392\u5e8f\u7684\u4f9d\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528sort()\u65b9\u6cd5\u6309\u7167\u7edd\u5bf9\u503c\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>numbers = [-5, 2, -9, 1, -5, 6]<\/p>\n<p>numbers.sort(key=abs)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, -5, -5, 6, -9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>1. \u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u7684\u6392\u5e8f\u9700\u6c42\u4e2d\uff0c\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u51fd\u6570\uff0c\u5e76\u7ed3\u5408sorted()\u6216sort()\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/p>\n<p>def custom_sort(x):<\/p>\n<p>    return x % 3, x<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u8fdb\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_numbers = sorted(numbers, key=custom_sort)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [9, 6, 5, 5, 2, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528lambda\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7b80\u5355\u7684\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528lambda\u51fd\u6570\u6765\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528lambda\u51fd\u6570\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_numbers = sorted(numbers, key=lambda x: x % 3)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [9, 6, 5, 5, 2, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u6392\u5e8f\u7a33\u5b9a\u6027<\/h2>\n<\/p>\n<p><h3>1. \u4ec0\u4e48\u662f\u6392\u5e8f\u7a33\u5b9a\u6027<\/h3>\n<\/p>\n<p><p>\u6392\u5e8f\u7a33\u5b9a\u6027\u6307\u7684\u662f\u5728\u6392\u5e8f\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u4e24\u4e2a\u5143\u7d20\u7684\u6bd4\u8f83\u7ed3\u679c\u76f8\u7b49\uff0c\u5b83\u4eec\u5728\u6392\u5e8f\u540e\u7684\u76f8\u5bf9\u4f4d\u7f6e\u4fdd\u6301\u4e0d\u53d8\u3002Python\u7684\u5185\u7f6e\u6392\u5e8f\u7b97\u6cd5\uff08Timsort\uff09\u662f\u7a33\u5b9a\u7684\uff0c\u8fd9\u610f\u5473\u7740\u76f8\u540c\u7684\u5143\u7d20\u5728\u6392\u5e8f\u540e\u4ecd\u7136\u4fdd\u6301\u5b83\u4eec\u7684\u76f8\u5bf9\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>2. \u7a33\u5b9a\u6392\u5e8f\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7a33\u5b9a\u6392\u5e8f\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728\u5bf9\u591a\u5217\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u65f6\uff0c\u53ef\u4ee5\u5148\u5bf9\u6b21\u8981\u5217\u8fdb\u884c\u6392\u5e8f\uff0c\u518d\u5bf9\u4e3b\u8981\u5217\u8fdb\u884c\u6392\u5e8f\u3002\u7531\u4e8e\u6392\u5e8f\u7684\u7a33\u5b9a\u6027\uff0c\u4e3b\u8981\u5217\u6392\u5e8f\u540e\uff0c\u6b21\u8981\u5217\u7684\u987a\u5e8f\u4ecd\u7136\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7a33\u5b9a\u6392\u5e8f\u793a\u4f8b<\/p>\n<p>data = [<\/p>\n<p>    (&#39;apple&#39;, 2),<\/p>\n<p>    (&#39;banana&#39;, 1),<\/p>\n<p>    (&#39;apple&#39;, 1),<\/p>\n<p>    (&#39;banana&#39;, 2),<\/p>\n<p>]<\/p>\n<h2><strong>\u5148\u6309\u7167\u7b2c\u4e8c\u5217\u6392\u5e8f<\/strong><\/h2>\n<p>data.sort(key=lambda x: x[1])<\/p>\n<h2><strong>\u518d\u6309\u7167\u7b2c\u4e00\u5217\u6392\u5e8f<\/strong><\/h2>\n<p>data.sort(key=lambda x: x[0])<\/p>\n<p>print(data)<\/p>\n<h2><strong>\u8f93\u51fa: [(&#39;apple&#39;, 1), (&#39;apple&#39;, 2), (&#39;banana&#39;, 1), (&#39;banana&#39;, 2)]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u6392\u5e8f\u6027\u80fd<\/h2>\n<\/p>\n<p><h3>1. \u65f6\u95f4\u590d\u6742\u5ea6<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u6392\u5e8f\u7b97\u6cd5\uff08Timsort\uff09\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n log n)\uff0c\u8fd9\u662f\u76ee\u524d\u5df2\u77e5\u7684\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u6700\u4f73\u590d\u6742\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>2. \u7a7a\u95f4\u590d\u6742\u5ea6<\/h3>\n<\/p>\n<p><p>Timsort\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff0c\u5728\u6392\u5e8f\u8fc7\u7a0b\u4e2d\u9700\u8981\u989d\u5916\u7684\u5b58\u50a8\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<p><h3>3. \u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u751f\u6210\u5668\u3001\u8fed\u4ee3\u5668\u7b49\u65b9\u5f0f\u6765\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u6765\u63d0\u9ad8\u6392\u5e8f\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u751f\u6210\u5668\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>import random<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u5927\u7684\u6570\u636e\u96c6<\/strong><\/h2>\n<p>large_numbers = (random.randint(0, 1000000) for _ in range(1000000))<\/p>\n<h2><strong>\u4f7f\u7528sorted()\u51fd\u6570\u8fdb\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_large_numbers = sorted(large_numbers)<\/p>\n<p>print(sorted_large_numbers[:10])  # \u8f93\u51fa: \u6392\u5e8f\u540e\u7684\u524d10\u4e2a\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u5e94\u7528<\/h2>\n<\/p>\n<p><h3>1. \u6570\u636e\u6e05\u6d17\u4e0e\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u6392\u5e8f\u5728\u6570\u636e\u6e05\u6d17\u4e2d\u5e38\u5e38\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u53bb\u91cd\u3001\u67e5\u627e\u5f02\u5e38\u503c\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u636e\u6e05\u6d17\u4e0e\u9884\u5904\u7406\u793a\u4f8b<\/p>\n<p>data = [5, 2, 9, 1, 5, 6, 9, 2]<\/p>\n<h2><strong>\u53bb\u91cd<\/strong><\/h2>\n<p>unique_data = sorted(set(data))<\/p>\n<p>print(unique_data)  # \u8f93\u51fa: [1, 2, 5, 6, 9]<\/p>\n<h2><strong>\u67e5\u627e\u5f02\u5e38\u503c<\/strong><\/h2>\n<p>threshold = 7<\/p>\n<p>outliers = [x for x in data if x &gt; threshold]<\/p>\n<p>print(outliers)  # \u8f93\u51fa: [9, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6392\u5e8f\u7b97\u6cd5\u7684\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u7b97\u6cd5\u7ade\u8d5b\u548c\u9762\u8bd5\u4e2d\uff0c\u6392\u5e8f\u7b97\u6cd5\u7684\u5b9e\u73b0\u662f\u5e38\u89c1\u7684\u8003\u5bdf\u70b9\u3002\u9664\u4e86\u4f7f\u7528Python\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\u5916\uff0c\u7406\u89e3\u548c\u5b9e\u73b0\u7ecf\u5178\u7684\u6392\u5e8f\u7b97\u6cd5\uff08\u5982\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u7b49\uff09\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u7684\u5b9e\u73b0<\/p>\n<p>def quicksort(arr):<\/p>\n<p>    if len(arr) &lt;= 1:<\/p>\n<p>        return arr<\/p>\n<p>    pivot = arr[len(arr) \/\/ 2]<\/p>\n<p>    left = [x for x in arr if x &lt; pivot]<\/p>\n<p>    middle = [x for x in arr if x == pivot]<\/p>\n<p>    right = [x for x in arr if x &gt; pivot]<\/p>\n<p>    return quicksort(left) + middle + quicksort(right)<\/p>\n<h2><strong>\u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u8fdb\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_numbers = quicksort(numbers)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u6570\u636e\u5e93\u67e5\u8be2\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5e93\u67e5\u8be2\u4e2d\uff0c\u6392\u5e8f\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5206\u9875\u67e5\u8be2\u3001\u67e5\u627e\u6700\u5927\u6216\u6700\u5c0f\u503c\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u636e\u5e93\u67e5\u8be2\u4f18\u5316\u793a\u4f8b<\/p>\n<p>import sqlite3<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;:memory:&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u8868<\/strong><\/h2>\n<p>cursor.execute(&#39;CREATE TABLE numbers (value INTEGER)&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>cursor.executemany(&#39;INSERT INTO numbers (value) VALUES (?)&#39;, [(5,), (2,), (9,), (1,), (5,), (6,)])<\/p>\n<h2><strong>\u67e5\u8be2\u5e76\u6392\u5e8f<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT value FROM numbers ORDER BY value&#39;)<\/p>\n<p>sorted_numbers = cursor.fetchall()<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [(1,), (2,), (5,), (5,), (6,), (9,)]<\/p>\n<h2><strong>\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7ed9\u6570\u5b57\u6392\u5e8f\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684sorted()\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u7684sort()\u65b9\u6cd5\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/strong>\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\u5404\u6709\u4f18\u52bf\u3002\u4e86\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u6392\u5e8f\u65b9\u6cd5\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u5bf9\u5404\u79cd\u6392\u5e8f\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5b66\u4e60\u548c\u5b9e\u8df5\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u5c06\u80fd\u591f\u66f4\u597d\u5730\u5904\u7406\u6570\u636e\uff0c\u5e76\u5728\u6570\u636e\u79d1\u5b66\u3001\u7b97\u6cd5\u7ade\u8d5b\u3001\u6570\u636e\u5e93\u67e5\u8be2\u4f18\u5316\u7b49\u9886\u57df\u4e2d\u6e38\u5203\u6709\u4f59\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3Python\u4e2d\u7684\u6392\u5e8f\u64cd\u4f5c\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u53d6\u5f97\u66f4\u597d\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u6570\u5b57\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sort()<\/code>\u65b9\u6cd5\u6216<code>sorted()<\/code>\u51fd\u6570\u6765\u5bf9\u6570\u5b57\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002<code>sort()<\/code>\u65b9\u6cd5\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u5217\u8868\uff0c\u800c<code>sorted()<\/code>\u51fd\u6570\u5219\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>numbers = [5, 2, 9, 1, 5, 6]<\/code>\uff0c\u8c03\u7528<code>numbers.sort()<\/code>\u4f1a\u539f\u5730\u6392\u5e8f\uff0c\u800c<code>sorted_numbers = sorted(numbers)<\/code>\u4f1a\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u5217\u8868\u3002<\/p>\n<p><strong>Python\u4e2d\u7684\u6392\u5e8f\u662f\u7a33\u5b9a\u7684\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u4e2d\u7684\u6392\u5e8f\u662f\u7a33\u5b9a\u7684\u3002\u7a33\u5b9a\u6392\u5e8f\u610f\u5473\u7740\u5982\u679c\u4e24\u4e2a\u5143\u7d20\u76f8\u7b49\uff0c\u5b83\u4eec\u5728\u6392\u5e8f\u540e\u7684\u5217\u8868\u4e2d\u4f1a\u4fdd\u6301\u539f\u6765\u7684\u76f8\u5bf9\u4f4d\u7f6e\u3002\u8fd9\u4e00\u7279\u6027\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u7279\u522b\u6709\u7528\uff0c\u6bd4\u5982\u5f53\u6570\u5b57\u5bf9\u5e94\u67d0\u4e9b\u5c5e\u6027\u65f6\uff0c\u4fdd\u6301\u539f\u6709\u987a\u5e8f\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>key<\/code>\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u6309\u7edd\u5bf9\u503c\u5bf9\u6570\u5b57\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f20\u9012<code>key=abs<\/code>\u7ed9<code>sort()<\/code>\u6216<code>sorted()<\/code>\u3002\u8fd9\u6837\uff0c\u5217\u8868\u4e2d\u7684\u6570\u5b57\u5c06\u6839\u636e\u5b83\u4eec\u7684\u7edd\u5bf9\u503c\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u6839\u636e\u6570\u503c\u5927\u5c0f\u3002\u8fd9\u6837\u7684\u7075\u6d3b\u6027\u4f7f\u5f97Python\u5728\u5904\u7406\u5404\u79cd\u6570\u636e\u6392\u5e8f\u9700\u6c42\u65f6\u975e\u5e38\u5f3a\u5927\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7ed9\u6570\u5b57\u6392\u5e8f\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684sorted()\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u7684sort()\u65b9\u6cd5\u3001\u4f7f\u7528\u81ea [&hellip;]","protected":false},"author":3,"featured_media":1154910,"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\/1154899"}],"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=1154899"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154899\/revisions"}],"predecessor-version":[{"id":1154912,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154899\/revisions\/1154912"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1154910"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1154899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1154899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1154899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}