{"id":1174610,"date":"2025-01-15T17:20:38","date_gmt":"2025-01-15T09:20:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174610.html"},"modified":"2025-01-15T17:20:47","modified_gmt":"2025-01-15T09:20:47","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%8e%92%e5%ba%8f%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174610.html","title":{"rendered":"\u5982\u4f55\u7528python\u6392\u5e8f\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25110314\/c93e65b5-e4b4-4273-8747-7c4963b2c239.webp\" alt=\"\u5982\u4f55\u7528python\u6392\u5e8f\u6570\u5b57\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5bf9\u6570\u5b57\u8fdb\u884c\u6392\u5e8f\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u7684sorted()\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u7684sort()\u65b9\u6cd5\u3001\u5b9e\u73b0\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u3002<\/strong>\u5176\u4e2d\uff0c<strong>sorted()\u51fd\u6570<\/strong>\u662f\u6700\u5e38\u7528\u4e14\u7075\u6d3b\u7684\u6392\u5e8f\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u5bf9\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u4e14\u4e0d\u4f1a\u6539\u53d8\u539f\u59cb\u5bf9\u8c61\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5df2\u6392\u5e8f\u5217\u8868\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u51e0\u79cd\u65b9\u6cd5\u6765\u6392\u5e8f\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5185\u7f6e\u7684sorted()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u8fdb\u884c\u6392\u5e8f\u3002<code>sorted()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65b0\u7684\u5df2\u6392\u5e8f\u7684\u5217\u8868\uff0c\u4e0d\u4f1a\u6539\u53d8\u539f\u59cb\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_numbers = sorted(numbers)<\/p>\n<p>print(sorted_numbers)<\/p>\n<h2><strong>\u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528sorted()\u51fd\u6570\u7684\u53c2\u6570<\/h4>\n<\/p>\n<p><p><code>sorted()<\/code>\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u5305\u62ec<code>key<\/code>\u548c<code>reverse<\/code>\uff0c\u8fd9\u4f7f\u5f97\u5b83\u975e\u5e38\u7075\u6d3b\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>key\u53c2\u6570<\/strong>\uff1a\u7528\u4e8e\u6307\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u6b64\u51fd\u6570\u5e94\u7528\u4e8e\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<li><strong>reverse\u53c2\u6570<\/strong>\uff1a\u5982\u679c\u4e3a<code>True<\/code>\uff0c\u5219\u5217\u8868\u5143\u7d20\u5c06\u6309\u964d\u5e8f\u6392\u5217\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_numbers = sorted(numbers, reverse=True)<\/p>\n<p>print(sorted_numbers)<\/p>\n<h2><strong>\u8f93\u51fa: [9, 6, 5, 5, 2, 1]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5217\u8868\u7684sort()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u5bf9\u8c61\u672c\u8eab\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>sort()<\/code>\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u5bf9\u5217\u8868\u8fdb\u884c\u539f\u5730\u6392\u5e8f\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u59cb\u5217\u8868\uff0c\u800c\u4e0d\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>numbers.sort()<\/p>\n<p>print(numbers)<\/p>\n<h2><strong>\u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528sort()\u65b9\u6cd5\u7684\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u4e0e<code>sorted()<\/code>\u51fd\u6570\u7c7b\u4f3c\uff0c<code>sort()<\/code>\u65b9\u6cd5\u4e5f\u63a5\u53d7<code>key<\/code>\u548c<code>reverse<\/code>\u4e24\u4e2a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>numbers.sort(reverse=True)<\/p>\n<p>print(numbers)<\/p>\n<h2><strong>\u8f93\u51fa: [9, 6, 5, 5, 2, 1]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u6309\u7167\u7279\u5b9a\u7684\u89c4\u5219\u5bf9\u6570\u5b57\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528key\u53c2\u6570\u81ea\u5b9a\u4e49\u6392\u5e8f<\/h4>\n<\/p>\n<p><p><code>sorted()<\/code>\u548c<code>sort()<\/code>\u65b9\u6cd5\u7684<code>key<\/code>\u53c2\u6570\u5141\u8bb8\u6211\u4eec\u4f20\u9012\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f1a\u4f5c\u7528\u4e8e\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u4ee5\u6b64\u4e3a\u4f9d\u636e\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7ec4\u5305\u542b\u4e00\u4e2a\u6570\u5b57\u548c\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u5e0c\u671b\u6309\u5143\u7ec4\u4e2d\u7684\u6570\u5b57\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [(1, &#39;one&#39;), (3, &#39;three&#39;), (2, &#39;two&#39;)]<\/p>\n<p>sorted_data = sorted(data, key=lambda x: x[0])<\/p>\n<p>print(sorted_data)<\/p>\n<h2><strong>\u8f93\u51fa: [(1, &#39;one&#39;), (2, &#39;two&#39;), (3, &#39;three&#39;)]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5404\u79cd\u6392\u5e8f\u7b97\u6cd5\u7684\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528Python\u5185\u7f6e\u7684\u6392\u5e8f\u65b9\u6cd5\uff0c\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u4e86\u89e3\u6216\u5b9e\u73b0\u5177\u4f53\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5982<strong>\u5192\u6ce1\u6392\u5e8f\u3001\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f<\/strong>\u7b49\u3002\u8fd9\u4e0d\u4ec5\u6709\u52a9\u4e8e\u7406\u89e3\u6392\u5e8f\u7684\u8fc7\u7a0b\uff0c\u8fd8\u53ef\u4ee5\u5728\u67d0\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\u4f18\u5316\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u5192\u6ce1\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u5192\u6ce1\u6392\u5e8f\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u60f3\u662f\u901a\u8fc7\u591a\u6b21\u904d\u5386\u5217\u8868\uff0c\u5c06\u76f8\u90bb\u7684\u4e24\u4e2a\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\u5e76\u4ea4\u6362\uff0c\u4ece\u800c\u5c06\u6700\u5927\u6216\u6700\u5c0f\u7684\u5143\u7d20\u201c\u5192\u6ce1\u201d\u5230\u5217\u8868\u7684\u4e00\u7aef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def bubble_sort(arr):<\/p>\n<p>    n = len(arr)<\/p>\n<p>    for i in range(n):<\/p>\n<p>        for j in range(0, n-i-1):<\/p>\n<p>            if arr[j] &gt; arr[j+1]:<\/p>\n<p>                arr[j], arr[j+1] = arr[j+1], arr[j]<\/p>\n<p>    return arr<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_numbers = bubble_sort(numbers)<\/p>\n<p>print(sorted_numbers)<\/p>\n<h2><strong>\u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5feb\u901f\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u5feb\u901f\u6392\u5e8f\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u60f3\u662f\u9009\u62e9\u4e00\u4e2a\u201c\u57fa\u51c6\u201d\u5143\u7d20\uff0c\u7136\u540e\u5c06\u5217\u8868\u5206\u6210\u4e24\u90e8\u5206\uff0c\u4e00\u90e8\u5206\u6bd4\u57fa\u51c6\u5143\u7d20\u5c0f\uff0c\u53e6\u4e00\u90e8\u5206\u6bd4\u57fa\u51c6\u5143\u7d20\u5927\uff0c\u9012\u5f52\u5730\u5bf9\u8fd9\u4e24\u90e8\u5206\u8fdb\u884c\u6392\u5e8f\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>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_numbers = quick_sort(numbers)<\/p>\n<p>print(sorted_numbers)<\/p>\n<h2><strong>\u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5f52\u5e76\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u5f52\u5e76\u6392\u5e8f\u662f\u4e00\u79cd\u7a33\u5b9a\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u60f3\u662f\u5c06\u5217\u8868\u5206\u6210\u4e24\u90e8\u5206\uff0c\u5206\u522b\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u5c06\u4e24\u90e8\u5206\u5408\u5e76\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_sort(arr):<\/p>\n<p>    if len(arr) &lt;= 1:<\/p>\n<p>        return arr<\/p>\n<p>    mid = len(arr) \/\/ 2<\/p>\n<p>    left = merge_sort(arr[:mid])<\/p>\n<p>    right = merge_sort(arr[mid:])<\/p>\n<p>    return merge(left, right)<\/p>\n<p>def merge(left, right):<\/p>\n<p>    result = []<\/p>\n<p>    i = j = 0<\/p>\n<p>    while i &lt; len(left) and j &lt; len(right):<\/p>\n<p>        if left[i] &lt; right[j]:<\/p>\n<p>            result.append(left[i])<\/p>\n<p>            i += 1<\/p>\n<p>        else:<\/p>\n<p>            result.append(right[j])<\/p>\n<p>            j += 1<\/p>\n<p>    result.extend(left[i:])<\/p>\n<p>    result.extend(right[j:])<\/p>\n<p>    return result<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>sorted_numbers = merge_sort(numbers)<\/p>\n<p>print(sorted_numbers)<\/p>\n<h2><strong>\u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001Python\u4e2d\u7684\u6392\u5e8f\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u6392\u5e8f\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u91cf\u56e0\u7d20\u3002Python\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u548c\u5217\u8868\u7684<code>sort()<\/code>\u65b9\u6cd5\u90fd\u57fa\u4e8eTimsort\u7b97\u6cd5\uff0c\u8fd9\u662f\u4e00\u79cd\u6df7\u5408\u6392\u5e8f\u7b97\u6cd5\uff0c\u7ed3\u5408\u4e86\u63d2\u5165\u6392\u5e8f\u548c\u5f52\u5e76\u6392\u5e8f\u7684\u4f18\u70b9\uff0c\u5177\u6709\u975e\u5e38\u597d\u7684\u5e73\u5747\u548c\u6700\u574f\u60c5\u51b5\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>Timsort\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>Timsort\u7b97\u6cd5\u5728Python\u7684\u5b9e\u73b0\u4e2d\u626e\u6f14\u4e86\u91cd\u8981\u89d2\u8272\u3002\u5b83\u662f\u4e00\u79cd\u6df7\u5408\u7b97\u6cd5\uff0c\u7ed3\u5408\u4e86\u63d2\u5165\u6392\u5e8f\u548c\u5f52\u5e76\u6392\u5e8f\u7684\u4f18\u70b9\uff0c\u65e8\u5728\u4f18\u5316\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6027\u80fd\u3002Timsort\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5728\u6700\u574f\u60c5\u51b5\u4e0b\u4e3aO(n log n)\uff0c\u8fd9\u4f7f\u5f97\u5b83\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u8868\u73b0\u4f18\u5f02\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import random<\/p>\n<h2><strong>\u751f\u6210\u968f\u673a\u6570\u5217\u8868<\/strong><\/h2>\n<p>numbers = [random.randint(1, 10000) for _ in range(10000)]<\/p>\n<h2><strong>\u6d4b\u91cfsorted()\u51fd\u6570\u7684\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>sorted_numbers = sorted(numbers)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;sorted()\u51fd\u6570\u8017\u65f6: {end_time - start_time}\u79d2&quot;)<\/p>\n<h2><strong>\u6d4b\u91cfsort()\u65b9\u6cd5\u7684\u65f6\u95f4<\/strong><\/h2>\n<p>numbers = [random.randint(1, 10000) for _ in range(10000)]<\/p>\n<p>start_time = time.time()<\/p>\n<p>numbers.sort()<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;sort()\u65b9\u6cd5\u8017\u65f6: {end_time - start_time}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6392\u5e8f\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u6392\u5e8f\u5728\u8bb8\u591a\u5b9e\u9645\u5e94\u7528\u4e2d\u90fd\u975e\u5e38\u91cd\u8981\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><h4>\u6392\u5e8f\u5b66\u751f\u6210\u7ee9<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5b66\u751f\u540d\u5b57\u548c\u6210\u7ee9\u7684\u5217\u8868\uff0c\u6211\u4eec\u5e0c\u671b\u6309\u6210\u7ee9\u4ece\u9ad8\u5230\u4f4e\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = [(&quot;Alice&quot;, 88), (&quot;Bob&quot;, 95), (&quot;Charlie&quot;, 78), (&quot;David&quot;, 92)]<\/p>\n<p>sorted_students = sorted(students, key=lambda x: x[1], reverse=True)<\/p>\n<p>print(sorted_students)<\/p>\n<h2><strong>\u8f93\u51fa: [(&#39;Bob&#39;, 95), (&#39;David&#39;, 92), (&#39;Alice&#39;, 88), (&#39;Charlie&#39;, 78)]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6392\u5e8f\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u662fPython\u4e2d\u7684\u4e00\u79cd\u91cd\u8981\u6570\u636e\u7ed3\u6784\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u6839\u636e\u5b57\u5178\u7684\u503c\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">scores = {&quot;Alice&quot;: 88, &quot;Bob&quot;: 95, &quot;Charlie&quot;: 78, &quot;David&quot;: 92}<\/p>\n<p>sorted_scores = sorted(scores.items(), key=lambda x: x[1], reverse=True)<\/p>\n<p>print(sorted_scores)<\/p>\n<h2><strong>\u8f93\u51fa: [(&#39;Bob&#39;, 95), (&#39;David&#39;, 92), (&#39;Alice&#39;, 88), (&#39;Charlie&#39;, 78)]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6392\u5e8f\u5bf9\u8c61\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5bf9\u8c61\u7684\u5217\u8868\uff0c\u6211\u4eec\u5e0c\u671b\u6309\u5bf9\u8c61\u7684\u67d0\u4e2a\u5c5e\u6027\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Student:<\/p>\n<p>    def __init__(self, name, grade):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.grade = grade<\/p>\n<p>students = [Student(&quot;Alice&quot;, 88), Student(&quot;Bob&quot;, 95), Student(&quot;Charlie&quot;, 78), Student(&quot;David&quot;, 92)]<\/p>\n<p>sorted_students = sorted(students, key=lambda x: x.grade, reverse=True)<\/p>\n<p>for student in sorted_students:<\/p>\n<p>    print(f&quot;{student.name}: {student.grade}&quot;)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>Bob: 95<\/strong><\/h2>\n<h2><strong>David: 92<\/strong><\/h2>\n<h2><strong>Alice: 88<\/strong><\/h2>\n<h2><strong>Charlie: 78<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6392\u5e8f\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u80fd\u591f\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u548c\u5217\u8868\u7684<code>sort()<\/code>\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\u6392\u5e8f\u5de5\u5177\uff0c\u5b83\u4eec\u57fa\u4e8e\u9ad8\u6548\u7684Timsort\u7b97\u6cd5\uff0c\u80fd\u591f\u5904\u7406\u5404\u79cd\u6392\u5e8f\u4efb\u52a1\u3002\u6b64\u5916\uff0c\u638c\u63e1\u4e00\u4e9b\u7ecf\u5178\u7684\u6392\u5e8f\u7b97\u6cd5\uff08\u5982\u5192\u6ce1\u6392\u5e8f\u3001\u5feb\u901f\u6392\u5e8f\u548c\u5f52\u5e76\u6392\u5e8f\uff09\u4e5f\u6709\u52a9\u4e8e\u6211\u4eec\u66f4\u6df1\u5165\u5730\u7406\u89e3\u6392\u5e8f\u7684\u539f\u7406\u548c\u4f18\u5316\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u65b9\u6cd5\u548c\u4f18\u5316\u6392\u5e8f\u7b97\u6cd5\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u6548\u7387\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728Python\u4e2d\u8fdb\u884c\u6570\u5b57\u6392\u5e8f\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\u5217\u8868\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sort()<\/code>\u65b9\u6cd5\u5bf9\u5217\u8868\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\u3002\u53ea\u9700\u8c03\u7528\u8be5\u65b9\u6cd5\u5373\u53ef\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [5, 2, 9, 1, 5, 6]\nnumbers.sort()\nprint(numbers)  # \u8f93\u51fa: [1, 2, 5, 5, 6, 9]\n<\/code><\/pre>\n<p>\u5982\u679c\u60f3\u4fdd\u7559\u539f\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u5217\u8868\uff1a  <\/p>\n<pre><code class=\"language-python\">sorted_numbers = sorted(numbers)\nprint(sorted_numbers)  # \u8f93\u51fa: [1, 2, 5, 5, 6, 9]\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u6570\u5b57\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\uff0c\u53ef\u4ee5\u5728\u8c03\u7528<code>sort()<\/code>\u65b9\u6cd5\u65f6\u8bbe\u7f6e<code>reverse=True<\/code>\u53c2\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [5, 2, 9, 1, 5, 6]\nnumbers.sort(reverse=True)\nprint(numbers)  # \u8f93\u51fa: [9, 6, 5, 5, 2, 1]\n<\/code><\/pre>\n<p>\u540c\u6837\uff0c\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u65f6\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u964d\u5e8f\u6392\u5e8f\uff1a  <\/p>\n<pre><code class=\"language-python\">sorted_numbers = sorted(numbers, reverse=True)\nprint(sorted_numbers)  # \u8f93\u51fa: [9, 6, 5, 5, 2, 1]\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5bf9\u5305\u542b\u8d1f\u6570\u7684\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5904\u7406\u5305\u542b\u8d1f\u6570\u7684\u5217\u8868\u65f6\uff0cPython\u7684\u6392\u5e8f\u65b9\u6cd5\u540c\u6837\u9002\u7528\u3002\u65e0\u8bba\u5217\u8868\u4e2d\u5305\u542b\u6b63\u6570\u3001\u8d1f\u6570\u6216\u96f6\uff0c<code>sort()<\/code>\u548c<code>sorted()<\/code>\u51fd\u6570\u90fd\u80fd\u6b63\u786e\u5904\u7406\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [3, -1, 4, -5, 2]\nnumbers.sort()\nprint(numbers)  # \u8f93\u51fa: [-5, -1, 2, 3, 4]\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u4e5f\u4e00\u6837\uff1a  <\/p>\n<pre><code class=\"language-python\">sorted_numbers = sorted(numbers)\nprint(sorted_numbers)  # \u8f93\u51fa: [-5, -1, 2, 3, 4]\n<\/code><\/pre>\n<p>\u56e0\u6b64\uff0c\u65e0\u9700\u62c5\u5fc3\u8d1f\u6570\u5bf9\u6392\u5e8f\u7ed3\u679c\u7684\u5f71\u54cd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5bf9\u6570\u5b57\u8fdb\u884c\u6392\u5e8f\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u7684sorted()\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u7684sort()\u65b9\u6cd5\u3001\u5b9e\u73b0\u81ea [&hellip;]","protected":false},"author":3,"featured_media":1174627,"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\/1174610"}],"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=1174610"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174610\/revisions"}],"predecessor-version":[{"id":1174628,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174610\/revisions\/1174628"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1174627"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1174610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1174610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1174610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}