{"id":1012827,"date":"2024-12-27T11:42:37","date_gmt":"2024-12-27T03:42:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1012827.html"},"modified":"2024-12-27T11:42:39","modified_gmt":"2024-12-27T03:42:39","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python-array%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1012827.html","title":{"rendered":"\u5982\u4f55\u7528python array\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090440\/3b183e72-7341-4d59-8175-2b227902be73.webp\" alt=\"\u5982\u4f55\u7528python array\u6392\u5e8f\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<br \/><strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5bf9\u6570\u7ec4\uff08array\uff09\u8fdb\u884c\u6392\u5e8f<\/strong>\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\u3001\u4f7f\u7528Numpy\u5e93\u3001\u4ee5\u53ca\u5b9e\u73b0\u81ea\u5b9a\u4e49\u6392\u5e8f\u7b97\u6cd5\u3002\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\u5982<code>sorted()<\/code>\u548c\u6570\u7ec4\u5bf9\u8c61\u7684<code>sort()<\/code>\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u7b80\u6d01\u9ad8\u6548\u4e14\u6613\u4e8e\u4f7f\u7528\u3002Numpy\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u548c\u66f4\u9ad8\u7684\u6027\u80fd\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u7ec4\u3002\u81ea\u5b9a\u4e49\u6392\u5e8f\u7b97\u6cd5\u5219\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u9700\u6c42\u8fdb\u884c\u5b9a\u5236\u3002\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u662f\u5176\u4e2d\u4e00\u79cd\u7b80\u5355\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002<code>sorted()<\/code>\u51fd\u6570\u53ef\u4ee5\u5bf9\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u540e\u7684\u5217\u8868\u3002\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5c06\u5f85\u6392\u5e8f\u7684\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u5373\u53ef\u3002\u6b64\u5916\uff0c<code>sorted()<\/code>\u51fd\u6570\u8fd8\u652f\u6301\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\uff0c\u901a\u8fc7\u4f20\u5165\u5173\u952e\u5b57\u53c2\u6570<code>key<\/code>\u548c<code>reverse<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e24\u79cd\u5185\u7f6e\u65b9\u6cd5\u6765\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a<code>sort()<\/code>\u65b9\u6cd5\u548c<code>sorted()<\/code>\u51fd\u6570\u3002<code>sort()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5217\u8868\u5bf9\u8c61\uff0c\u4f1a\u76f4\u63a5\u5728\u539f\u6570\u7ec4\u4e0a\u8fdb\u884c\u6392\u5e8f\uff1b<code>sorted()<\/code>\u51fd\u6570\u5219\u9002\u7528\u4e8e\u6240\u6709\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u540e\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>sort()<\/code>\u65b9\u6cd5\u662fPython\u5217\u8868\u7684\u5185\u7f6e\u65b9\u6cd5\u3002\u5b83\u4f1a\u5bf9\u5217\u8868\u8fdb\u884c\u539f\u5730\u6392\u5e8f\uff0c\u5373\u4e0d\u4f1a\u8fd4\u56de\u65b0\u7684\u5217\u8868\uff0c\u800c\u662f\u5728\u539f\u59cb\u5217\u8868\u4e0a\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]<\/p>\n<p>array.sort()<\/p>\n<p>print(array)  # \u8f93\u51fa: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>reverse<\/code>\u53c2\u6570\u6307\u5b9a\u6392\u5e8f\u987a\u5e8f\u3002\u9ed8\u8ba4\u662f\u5347\u5e8f\u6392\u5e8f\uff0c\u5f53<code>reverse=True<\/code>\u65f6\uff0c\u4f1a\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]<\/p>\n<p>array.sort(reverse=True)<\/p>\n<p>print(array)  # \u8f93\u51fa: [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>sorted()<\/code>\u51fd\u6570\u9002\u7528\u4e8e\u6240\u6709\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u540e\u7684\u5217\u8868\uff0c\u800c\u4e0d\u4f1a\u4fee\u6539\u539f\u59cb\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]<\/p>\n<p>sorted_array = sorted(array)<\/p>\n<p>print(sorted_array)  # \u8f93\u51fa: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]<\/p>\n<p>print(array)  # \u539f\u6570\u7ec4\u672a\u53d8: [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u540c\u6837\uff0c<code>sorted()<\/code>\u51fd\u6570\u4e5f\u652f\u6301<code>reverse<\/code>\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sorted_array_desc = sorted(array, reverse=True)<\/p>\n<p>print(sorted_array_desc)  # \u8f93\u51fa: [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528Numpy\u5e93\u8fdb\u884c\u6392\u5e8f<\/p>\n<\/p>\n<p><p>Numpy\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u7684\u5f3a\u5927\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002Numpy\u7684<code>sort()<\/code>\u51fd\u6570\u9002\u7528\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u5e76\u4e14\u652f\u6301\u6307\u5b9a\u6392\u5e8f\u8f74\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u6392\u5e8f<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Numpy\u7684<code>sort()<\/code>\u51fd\u6570\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5])<\/p>\n<p>sorted_array = np.sort(array)<\/p>\n<p>print(sorted_array)  # \u8f93\u51fa: [1 1 2 3 3 4 5 5 5 6 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6307\u5b9a\u6392\u5e8f\u8f74<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u6307\u5b9a\u6cbf\u54ea\u4e2a\u8f74\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = np.array([[3, 1, 4], [1, 5, 9], [2, 6, 5]])<\/p>\n<p>sorted_matrix = np.sort(matrix, axis=0)<\/p>\n<p>print(sorted_matrix)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>[[1 1 4]<\/strong><\/h2>\n<h2><strong> [2 5 5]<\/strong><\/h2>\n<h2><strong> [3 6 9]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>axis=0<\/code>\u8868\u793a\u6cbf\u7740\u5217\u6392\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u81ea\u5b9a\u4e49\u6392\u5e8f\u7b97\u6cd5<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u5185\u7f6e\u7684\u6392\u5e8f\u65b9\u6cd5\u65e0\u6cd5\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\uff0c\u8fd9\u65f6\u53ef\u4ee5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u6392\u5e8f\u7b97\u6cd5\u3002\u5e38\u89c1\u7684\u6392\u5e8f\u7b97\u6cd5\u5305\u62ec\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u63d2\u5165\u6392\u5e8f\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5feb\u901f\u6392\u5e8f<\/strong><\/p>\n<\/p>\n<p><p>\u5feb\u901f\u6392\u5e8f\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5206\u6cbb\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def quick_sort(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 quick_sort(left) + middle + quick_sort(right)<\/p>\n<p>array = [3, 6, 8, 10, 1, 2, 1]<\/p>\n<p>print(quick_sort(array))  # \u8f93\u51fa: [1, 1, 2, 3, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5f52\u5e76\u6392\u5e8f<\/strong><\/p>\n<\/p>\n<p><p>\u5f52\u5e76\u6392\u5e8f\u662f\u4e00\u79cd\u7a33\u5b9a\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_sort(arr):<\/p>\n<p>    if len(arr) &gt; 1:<\/p>\n<p>        mid = len(arr) \/\/ 2<\/p>\n<p>        left_half = arr[:mid]<\/p>\n<p>        right_half = arr[mid:]<\/p>\n<p>        merge_sort(left_half)<\/p>\n<p>        merge_sort(right_half)<\/p>\n<p>        i = j = k = 0<\/p>\n<p>        while i &lt; len(left_half) and j &lt; len(right_half):<\/p>\n<p>            if left_half[i] &lt; right_half[j]:<\/p>\n<p>                arr[k] = left_half[i]<\/p>\n<p>                i += 1<\/p>\n<p>            else:<\/p>\n<p>                arr[k] = right_half[j]<\/p>\n<p>                j += 1<\/p>\n<p>            k += 1<\/p>\n<p>        while i &lt; len(left_half):<\/p>\n<p>            arr[k] = left_half[i]<\/p>\n<p>            i += 1<\/p>\n<p>            k += 1<\/p>\n<p>        while j &lt; len(right_half):<\/p>\n<p>            arr[k] = right_half[j]<\/p>\n<p>            j += 1<\/p>\n<p>            k += 1<\/p>\n<p>array = [12, 11, 13, 5, 6, 7]<\/p>\n<p>merge_sort(array)<\/p>\n<p>print(array)  # \u8f93\u51fa: [5, 6, 7, 11, 12, 13]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u6392\u5e8f\u7684\u6548\u7387\u4e0e\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u6392\u5e8f\u65b9\u6cd5\u65f6\uff0c\u6548\u7387\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002Python\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\u548cNumpy\u5e93\u7684\u6392\u5e8f\u65b9\u6cd5\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u662f\u6700\u4f73\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u4eec\u7ecf\u8fc7\u9ad8\u5ea6\u4f18\u5316\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u65f6\u95f4\u590d\u6742\u5ea6<\/strong><\/p>\n<\/p>\n<p><p>\u5927\u591a\u6570\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n log n)\uff0c\u8fd9\u5bf9\u4e8e\u4e00\u822c\u7528\u9014\u662f\u975e\u5e38\u9ad8\u6548\u7684\u3002\u81ea\u5b9a\u4e49\u6392\u5e8f\u7b97\u6cd5\u7684\u6548\u7387\u53ef\u80fd\u4e0d\u5982\u5185\u7f6e\u65b9\u6cd5\uff0c\u9664\u975e\u9488\u5bf9\u7279\u5b9a\u60c5\u51b5\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7a7a\u95f4\u590d\u6742\u5ea6<\/strong><\/p>\n<\/p>\n<p><p>\u5185\u7f6e\u65b9\u6cd5\u901a\u5e38\u4e5f\u5177\u6709\u8f83\u4f4e\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u3002Numpy\u5e93\u7684\u6392\u5e8f\u65b9\u6cd5\u662f\u539f\u5730\u6392\u5e8f\uff0c\u80fd\u6709\u6548\u964d\u4f4e\u7a7a\u95f4\u5f00\u9500\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e76\u884c\u8ba1\u7b97<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\uff0c\u8003\u8651\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u6765\u52a0\u5feb\u6392\u5e8f\u901f\u5ea6\u3002Numpy\u548cPandas\u7b49\u5e93\u652f\u6301\u5e76\u884c\u5316\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5728\u591a\u6838CPU\u4e0a\u5b9e\u73b0\u66f4\u5feb\u7684\u6392\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u822c\u7528\u9014\uff0c\u5efa\u8bae\u4f7f\u7528Python\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\u6216Numpy\u5e93\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4eec\u6613\u4e8e\u4f7f\u7528\u4e14\u7ecf\u8fc7\u9ad8\u5ea6\u4f18\u5316\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8003\u8651\u6570\u636e\u7c7b\u578b\u548c\u7ed3\u6784<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u6216\u591a\u7ef4\u6570\u7ec4\u65f6\uff0cNumpy\u5e93\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u6027\u80fd\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u5316\u81ea\u5b9a\u4e49\u7b97\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5b9e\u73b0\u81ea\u5b9a\u4e49\u6392\u5e8f\u7b97\u6cd5\uff0c\u52a1\u5fc5\u8003\u8651\u7b97\u6cd5\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5e76\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5bf9Python\u4e2d\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u6ee1\u8db3\u5404\u79cd\u4e0d\u540c\u7684\u7f16\u7a0b\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u548c\u6570\u7ec4\u5bf9\u8c61\u7684<code>sort()<\/code>\u65b9\u6cd5\u3002<code>sorted()<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u800c<code>sort()<\/code>\u65b9\u6cd5\u4f1a\u5c31\u5730\u5bf9\u539f\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u5177\u4f53\u64cd\u4f5c\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">arr = [5, 2, 9, 1, 5, 6]\nsorted_arr = sorted(arr)  # \u8fd4\u56de\u65b0\u6392\u5e8f\u7684\u5217\u8868\narr.sort()                # \u5c31\u5730\u6392\u5e8f\n<\/code><\/pre>\n<p><strong>Python\u6570\u7ec4\u6392\u5e8f\u652f\u6301\u54ea\u4e9b\u6392\u5e8f\u65b9\u5f0f\uff1f<\/strong><br \/>Python\u7684\u6392\u5e8f\u529f\u80fd\u975e\u5e38\u7075\u6d3b\uff0c\u652f\u6301\u591a\u79cd\u6392\u5e8f\u65b9\u5f0f\u3002\u53ef\u4ee5\u901a\u8fc7<code>key<\/code>\u53c2\u6570\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\uff0c\u4f8b\u5982\u6309\u7edd\u5bf9\u503c\u3001\u5b57\u7b26\u4e32\u957f\u5ea6\u7b49\u8fdb\u884c\u6392\u5e8f\u3002\u6b64\u5916\uff0c<code>reverse<\/code>\u53c2\u6570\u53ef\u4ee5\u5b9e\u73b0\u964d\u5e8f\u6392\u5217\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">arr = [5, -2, 9, 1, -5, 6]\nsorted_arr = sorted(arr, key=abs, reverse=True)  # \u6309\u7edd\u5bf9\u503c\u964d\u5e8f\u6392\u5e8f\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5bf9\u5305\u542b\u5b57\u7b26\u4e32\u7684Python\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5bf9\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u6392\u5e8f\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u6216<code>sort()<\/code>\u65b9\u6cd5\uff0c\u6392\u5e8f\u4f1a\u6839\u636e\u5b57\u6bcd\u987a\u5e8f\u8fdb\u884c\u3002\u53ef\u4ee5\u901a\u8fc7<code>key<\/code>\u53c2\u6570\u6307\u5b9a\u6392\u5e8f\u65b9\u5f0f\uff0c\u4f8b\u5982\u6309\u5b57\u7b26\u4e32\u957f\u5ea6\u6392\u5e8f\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">str_arr = [&#39;banana&#39;, &#39;apple&#39;, &#39;cherry&#39;]\nsorted_str_arr = sorted(str_arr)  # \u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\nstr_arr.sort(key=len)              # \u6309\u5b57\u7b26\u4e32\u957f\u5ea6\u6392\u5e8f\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5bf9\u6570\u7ec4\uff08array\uff09\u8fdb\u884c\u6392\u5e8f\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\u3001\u4f7f\u7528Nump [&hellip;]","protected":false},"author":3,"featured_media":1012836,"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\/1012827"}],"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=1012827"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012827\/revisions"}],"predecessor-version":[{"id":1012838,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1012827\/revisions\/1012838"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1012836"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1012827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1012827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1012827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}