{"id":1161339,"date":"2025-01-13T19:15:17","date_gmt":"2025-01-13T11:15:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1161339.html"},"modified":"2025-01-13T19:15:20","modified_gmt":"2025-01-13T11:15:20","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8sorted%e5%8f%8d%e5%90%91%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1161339.html","title":{"rendered":"Python\u5982\u4f55\u7528sorted\u53cd\u5411\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202605\/469b6f53-649e-44e6-9f59-7289a9020582.webp\" alt=\"Python\u5982\u4f55\u7528sorted\u53cd\u5411\u6392\u5e8f\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u4f7f\u7528sorted\u51fd\u6570\u8fdb\u884c\u53cd\u5411\u6392\u5e8f\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528reverse\u53c2\u6570\u3001\u4f7f\u7528lambda\u51fd\u6570\u8fdb\u884c\u952e\u6392\u5e8f\u3001\u5728\u6392\u5e8f\u524d\u5bf9\u6570\u636e\u8fdb\u884c\u53cd\u8f6c\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528reverse\u53c2\u6570<\/strong>\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528reverse\u53c2\u6570\uff1a<\/p>\n<p>\u5728Python\u4e2d\uff0csorted()\u51fd\u6570\u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u8be5\u51fd\u6570\u7684\u8bed\u6cd5\u662fsorted(iterable, key=None, reverse=False)\u3002\u901a\u8fc7\u5c06reverse\u53c2\u6570\u8bbe\u7f6e\u4e3aTrue\uff0c\u53ef\u4ee5\u5b9e\u73b0\u53cd\u5411\u6392\u5e8f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 3, 2, 5, 4]<\/p>\n<p>sorted_numbers = sorted(numbers, reverse=True)<\/p>\n<p>print(sorted_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a[5, 4, 3, 2, 1]\uff0c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u5c06\u6309\u964d\u5e8f\u6392\u5217\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u4e2d\u4f7f\u7528sorted\u53cd\u5411\u6392\u5e8f\u7684\u4e0d\u540c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528reverse\u53c2\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u7840\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>Python\u7684sorted\u51fd\u6570\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u6309\u5347\u5e8f\u6392\u5217\u7684\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6ereverse\u53c2\u6570\u4e3aTrue\u6765\u5b9e\u73b0\u964d\u5e8f\u6392\u5217\u3002reverse\u53c2\u6570\u662fsorted\u51fd\u6570\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u9ed8\u8ba4\u503c\u4e3aFalse\uff0c\u5f53\u8bbe\u7f6e\u4e3aTrue\u65f6\uff0c\u6392\u5e8f\u7ed3\u679c\u5c06\u6309\u964d\u5e8f\u6392\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>numbers = [1, 3, 2, 5, 4]<\/p>\n<p>sorted_numbers = sorted(numbers, reverse=True)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [5, 4, 3, 2, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f20\u9012\u4e86\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u5217\u8868\u7ed9sorted\u51fd\u6570\uff0c\u5e76\u8bbe\u7f6ereverse=True\uff0c\u7ed3\u679c\u662f\u4e00\u4e2a\u6309\u964d\u5e8f\u6392\u5217\u7684\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4e0ekey\u53c2\u6570\u7ed3\u5408\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0csorted\u51fd\u6570\u901a\u5e38\u4f1a\u7ed3\u5408key\u53c2\u6570\u4e00\u8d77\u4f7f\u7528\u3002key\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f1a\u88ab\u5e94\u7528\u5230iterable\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a\uff0c\u5e76\u6839\u636e\u8fd4\u56de\u503c\u8fdb\u884c\u6392\u5e8f\u3002key\u53c2\u6570\u548creverse\u53c2\u6570\u53ef\u4ee5\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6392\u5e8f\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>students = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 20},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 23}<\/p>\n<p>]<\/p>\n<h2><strong>\u6309\u5e74\u9f84\u964d\u5e8f\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_students = sorted(students, key=lambda x: x[&#39;age&#39;], reverse=True)<\/p>\n<p>print(sorted_students)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u7684\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5b57\u5178\u4ee3\u8868\u4e00\u4e2a\u5b66\u751f\u7684\u4fe1\u606f\u3002\u901a\u8fc7\u4f20\u9012lambda\u51fd\u6570\u7ed9key\u53c2\u6570\uff0c\u5e76\u8bbe\u7f6ereverse=True\uff0c\u5b9e\u73b0\u4e86\u6309\u5e74\u9f84\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528lambda\u51fd\u6570\u8fdb\u884c\u952e\u6392\u5e8f<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u7840\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u6839\u636e\u67d0\u4e2a\u7279\u5b9a\u7684\u5c5e\u6027\u6216\u8ba1\u7b97\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002\u8fd9\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7key\u53c2\u6570\u4f20\u9012\u4e00\u4e2alambda\u51fd\u6570\u6765\u5b9e\u73b0\u3002lambda\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u6392\u5e8f\u7684\u952e\u503c\uff0c\u7136\u540esorted\u51fd\u6570\u6839\u636e\u8fd9\u4e9b\u952e\u503c\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>words = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;]<\/p>\n<h2><strong>\u6309\u5355\u8bcd\u957f\u5ea6\u964d\u5e8f\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_words = sorted(words, key=lambda x: len(x), reverse=True)<\/p>\n<p>print(sorted_words)  # \u8f93\u51fa: [&#39;banana&#39;, &#39;cherry&#39;, &#39;apple&#39;, &#39;date&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bf9\u5355\u8bcd\u5217\u8868\u8fdb\u884c\u4e86\u6309\u957f\u5ea6\u964d\u5e8f\u6392\u5e8f\uff0c\u901a\u8fc7\u4f20\u9012lambda\u51fd\u6570\u7ed9key\u53c2\u6570\uff0clambda\u51fd\u6570\u8fd4\u56de\u5355\u8bcd\u7684\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ed3\u5408\u590d\u6742\u6570\u636e\u7ed3\u6784\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u4f8b\u5982\u5d4c\u5957\u7684\u5217\u8868\u6216\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528lambda\u51fd\u6570\u5bf9\u7279\u5b9a\u5c5e\u6027\u8fdb\u884c\u6392\u5e8f\u3002\u901a\u8fc7\u7075\u6d3b\u5e94\u7528lambda\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u79cd\u6392\u5e8f\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>products = [<\/p>\n<p>    {&#39;name&#39;: &#39;laptop&#39;, &#39;price&#39;: 1000, &#39;rating&#39;: 4.5},<\/p>\n<p>    {&#39;name&#39;: &#39;phone&#39;, &#39;price&#39;: 500, &#39;rating&#39;: 4.7},<\/p>\n<p>    {&#39;name&#39;: &#39;tablet&#39;, &#39;price&#39;: 300, &#39;rating&#39;: 4.3}<\/p>\n<p>]<\/p>\n<h2><strong>\u6309\u4ef7\u683c\u964d\u5e8f\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_products = sorted(products, key=lambda x: x[&#39;price&#39;], reverse=True)<\/p>\n<p>print(sorted_products)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bf9\u4ea7\u54c1\u5217\u8868\u8fdb\u884c\u4e86\u6309\u4ef7\u683c\u964d\u5e8f\u6392\u5e8f\uff0c\u901a\u8fc7\u4f20\u9012lambda\u51fd\u6570\u7ed9key\u53c2\u6570\uff0clambda\u51fd\u6570\u8fd4\u56de\u4ea7\u54c1\u7684\u4ef7\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5728\u6392\u5e8f\u524d\u5bf9\u6570\u636e\u8fdb\u884c\u53cd\u8f6c<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u7840\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528reverse\u53c2\u6570\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5728\u6392\u5e8f\u524d\u5bf9\u6570\u636e\u8fdb\u884c\u53cd\u8f6c\u6765\u5b9e\u73b0\u53cd\u5411\u6392\u5e8f\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\uff0c\u4f8b\u5982\u5bf9\u5df2\u7ecf\u6392\u5e8f\u7684\u5217\u8868\u8fdb\u884c\u53cd\u8f6c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u5148\u6392\u5e8f\u540e\u53cd\u8f6c<\/strong><\/h2>\n<p>numbers_sorted = sorted(numbers)<\/p>\n<p>numbers_reversed = numbers_sorted[::-1]<\/p>\n<p>print(numbers_reversed)  # \u8f93\u51fa: [5, 4, 3, 2, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bf9\u5217\u8868\u8fdb\u884c\u4e86\u5347\u5e8f\u6392\u5e8f\uff0c\u7136\u540e\u901a\u8fc7\u5207\u7247\u64cd\u4f5c[::-1]\u5bf9\u6392\u5e8f\u7ed3\u679c\u8fdb\u884c\u4e86\u53cd\u8f6c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ed3\u5408\u5176\u4ed6\u64cd\u4f5c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5728\u6392\u5e8f\u524d\u5bf9\u6570\u636e\u8fdb\u884c\u4e00\u4e9b\u5176\u4ed6\u64cd\u4f5c\uff0c\u4f8b\u5982\u8fc7\u6ee4\u3001\u8f6c\u6362\u7b49\u3002\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6392\u5e8f\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u8fc7\u6ee4\u51fa\u5076\u6570\u5e76\u6309\u964d\u5e8f\u6392\u5e8f<\/strong><\/h2>\n<p>even_numbers = [x for x in numbers if x % 2 == 0]<\/p>\n<p>sorted_even_numbers = sorted(even_numbers, reverse=True)<\/p>\n<p>print(sorted_even_numbers)  # \u8f93\u51fa: [4, 2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u8fc7\u6ee4\u51fa\u5076\u6570\uff0c\u7136\u540e\u5bf9\u8fc7\u6ee4\u7ed3\u679c\u8fdb\u884c\u4e86\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001sorted\u51fd\u6570\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u591a\u91cd\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u591a\u91cd\u6392\u5e8f\uff0c\u4f8b\u5982\u5148\u6309\u4e00\u4e2a\u5c5e\u6027\u6392\u5e8f\uff0c\u518d\u6309\u53e6\u4e00\u4e2a\u5c5e\u6027\u6392\u5e8f\u3002\u8fd9\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ed3\u5408key\u53c2\u6570\u548ctuple\u6765\u5b9e\u73b0\u591a\u91cd\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>students = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;grade&#39;: &#39;B&#39;},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 20, &#39;grade&#39;: &#39;A&#39;},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 23, &#39;grade&#39;: &#39;C&#39;}<\/p>\n<p>]<\/p>\n<h2><strong>\u5148\u6309\u6210\u7ee9\u5347\u5e8f\u6392\u5e8f\uff0c\u518d\u6309\u5e74\u9f84\u964d\u5e8f\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_students = sorted(students, key=lambda x: (x[&#39;grade&#39;], -x[&#39;age&#39;]))<\/p>\n<p>print(sorted_students)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u6309\u6210\u7ee9\u5347\u5e8f\u6392\u5e8f\uff0c\u7136\u540e\u6309\u5e74\u9f84\u964d\u5e8f\u6392\u5e8f\uff0c\u901a\u8fc7\u4f20\u9012tuple\u7ed9key\u53c2\u6570\uff0c\u5b9e\u73b0\u4e86\u591a\u91cd\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528lambda\u51fd\u6570\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9key\u53c2\u6570\u3002\u8fd9\u6837\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6392\u5e8f\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>def custom_sort(student):<\/p>\n<p>    return student[&#39;grade&#39;], -student[&#39;age&#39;]<\/p>\n<p>students = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;grade&#39;: &#39;B&#39;},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 20, &#39;grade&#39;: &#39;A&#39;},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 23, &#39;grade&#39;: &#39;C&#39;}<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/strong><\/h2>\n<p>sorted_students = sorted(students, key=custom_sort)<\/p>\n<p>print(sorted_students)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570custom_sort\uff0c\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2atuple\uff0c\u7528\u4e8e\u5b9e\u73b0\u591a\u91cd\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001sorted\u51fd\u6570\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u5904\u7406\u4e0e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u4e0e\u5206\u6790\u4e2d\uff0csorted\u51fd\u6570\u7ecf\u5e38\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u5206\u6790\u548c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u4ee5\u627e\u5230\u6700\u5927\u503c\u3001\u6700\u5c0f\u503c\u6216\u4e2d\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>data = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u627e\u5230\u4e2d\u4f4d\u6570<\/strong><\/h2>\n<p>sorted_data = sorted(data)<\/p>\n<p>median = sorted_data[len(sorted_data) \/\/ 2]<\/p>\n<p>print(median)  # \u8f93\u51fa: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bf9\u6570\u636e\u8fdb\u884c\u4e86\u6392\u5e8f\uff0c\u7136\u540e\u627e\u5230\u4e86\u4e2d\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6587\u672c\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6587\u672c\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0csorted\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5bf9\u5355\u8bcd\u3001\u53e5\u5b50\u6216\u6bb5\u843d\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee5\u4fbf\u4e8e\u5206\u6790\u6216\u751f\u6210\u62a5\u544a\u3002\u4f8b\u5982\uff0c\u5bf9\u5355\u8bcd\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>text = &quot;the quick brown fox jumps over the lazy dog&quot;<\/p>\n<p>words = text.split()<\/p>\n<h2><strong>\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_words = sorted(words)<\/p>\n<p>print(sorted_words)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bf9\u6587\u672c\u4e2d\u7684\u5355\u8bcd\u8fdb\u884c\u4e86\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001sorted\u51fd\u6570\u4e0e\u5176\u4ed6\u6392\u5e8f\u51fd\u6570\u7684\u6bd4\u8f83<\/h3>\n<\/p>\n<p><h4>1\u3001sorted\u4e0esort\u7684\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u9664\u4e86sorted\u51fd\u6570\u5916\uff0c\u8fd8\u6709\u4e00\u4e2alist\u7684sort\u65b9\u6cd5\u3002\u4e24\u8005\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\uff0csorted\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u800csort\u65b9\u6cd5\u662f\u5728\u539f\u5217\u8868\u4e0a\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>numbers = [3, 1, 2, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528sorted\u51fd\u6570<\/strong><\/h2>\n<p>sorted_numbers = sorted(numbers)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [3, 1, 2, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528sort\u65b9\u6cd5<\/strong><\/h2>\n<p>numbers.sort()<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86sorted\u51fd\u6570\u548csort\u65b9\u6cd5\u7684\u4f7f\u7528\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001sorted\u4e0e\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\u7684\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>Python\u7684sorted\u51fd\u6570\u5e95\u5c42\u5b9e\u73b0\u4e86Timsort\u7b97\u6cd5\uff0c\u8fd9\u662f\u4e00\u79cd\u7ed3\u5408\u4e86\u5f52\u5e76\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u7684\u6df7\u5408\u7b97\u6cd5\uff0c\u5177\u6709\u7a33\u5b9a\u6027\u548c\u9ad8\u6548\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0csorted\u51fd\u6570\u901a\u5e38\u6bd4\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u7b97\u6cd5\u66f4\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u7ecf\u8fc7\u4e86\u5145\u5206\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import time<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5927\u5217\u8868<\/strong><\/h2>\n<p>large_list = list(range(1000000, 0, -1))<\/p>\n<h2><strong>\u4f7f\u7528sorted\u51fd\u6570\u6392\u5e8f<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>sorted_list = sorted(large_list)<\/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<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u7b97\u6cd5\uff08\u5feb\u901f\u6392\u5e8f\uff09\u6392\u5e8f<\/strong><\/h2>\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<p>start_time = time.time()<\/p>\n<p>sorted_list_custom = quicksort(large_list)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u7b97\u6cd5\u6392\u5e8f\u7684\u65f6\u95f4:&quot;, end_time - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bf9\u6bd4\u4e86sorted\u51fd\u6570\u548c\u81ea\u5b9a\u4e49\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u7684\u6027\u80fd\u3002\u7ed3\u679c\u8868\u660e\uff0csorted\u51fd\u6570\u7684\u6027\u80fd\u901a\u5e38\u4f18\u4e8e\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001sorted\u51fd\u6570\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>1\u3001\u5408\u7406\u9009\u62e9key\u548creverse\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528sorted\u51fd\u6570\u65f6\uff0c\u5408\u7406\u9009\u62e9key\u548creverse\u53c2\u6570\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6392\u5e8f\u9700\u6c42\uff0c\u76f4\u63a5\u4f7f\u7528reverse\u53c2\u6570\u5373\u53ef\uff1b\u5bf9\u4e8e\u590d\u6742\u7684\u6392\u5e8f\u9700\u6c42\uff0c\u53ef\u4ee5\u7ed3\u5408lambda\u51fd\u6570\u6216\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>employees = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;salary&#39;: 50000, &#39;age&#39;: 25},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;salary&#39;: 60000, &#39;age&#39;: 30},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;salary&#39;: 70000, &#39;age&#39;: 35}<\/p>\n<p>]<\/p>\n<h2><strong>\u6309\u85aa\u6c34\u5347\u5e8f\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_employees = sorted(employees, key=lambda x: x[&#39;salary&#39;])<\/p>\n<p>print(sorted_employees)<\/p>\n<h2><strong>\u6309\u5e74\u9f84\u964d\u5e8f\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_employees = sorted(employees, key=lambda x: x[&#39;age&#39;], reverse=True)<\/p>\n<p>print(sorted_employees)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5408\u7406\u9009\u62e9key\u548creverse\u53c2\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6392\u5e8f\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6392\u5e8f\u64cd\u4f5c\u53ef\u80fd\u4f1a\u6d88\u8017\u5927\u91cf\u7684\u8ba1\u7b97\u8d44\u6e90\u3002\u56e0\u6b64\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6392\u5e8f\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u6570\u636e\u8fc7\u6ee4\u540e\u518d\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee5\u51cf\u5c11\u6392\u5e8f\u7684\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]<\/p>\n<h2><strong>\u8fc7\u6ee4\u51fa\u5927\u4e8e50\u7684\u6570\u636e\uff0c\u5e76\u6309\u964d\u5e8f\u6392\u5e8f<\/strong><\/h2>\n<p>filtered_data = [x for x in data if x &gt; 50]<\/p>\n<p>sorted_filtered_data = sorted(filtered_data, reverse=True)<\/p>\n<p>print(sorted_filtered_data)  # \u8f93\u51fa: [100, 90, 80, 70, 60]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u6570\u636e\u8fc7\u6ee4\u540e\u518d\u8fdb\u884c\u6392\u5e8f\uff0c\u907f\u514d\u4e86\u5bf9\u6574\u4e2a\u6570\u636e\u96c6\u8fdb\u884c\u6392\u5e8f\u7684\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001sorted\u51fd\u6570\u7684\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<p><h4>1\u3001\u6392\u5e8f\u7ed3\u679c\u4e0d\u7b26\u5408\u9884\u671f<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528sorted\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6392\u5e8f\u7ed3\u679c\u4e0d\u7b26\u5408\u9884\u671f\u7684\u95ee\u9898\u3002\u901a\u5e38\uff0c\u8fd9\u662f\u7531\u4e8ekey\u53c2\u6570\u6216reverse\u53c2\u6570\u8bbe\u7f6e\u4e0d\u5f53\u5f15\u8d77\u7684\u3002\u68c0\u67e5\u548c\u8c03\u6574\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u89e3\u51b3\u5927\u591a\u6570\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>data = [&#39;apple&#39;, &#39;Banana&#39;, &#39;cherry&#39;, &#39;date&#39;]<\/p>\n<h2><strong>\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\uff0c\u5ffd\u7565\u5927\u5c0f\u5199<\/strong><\/h2>\n<p>sorted_data = sorted(data, key=lambda x: x.lower())<\/p>\n<p>print(sorted_data)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;Banana&#39;, &#39;cherry&#39;, &#39;date&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5c06\u5355\u8bcd\u8f6c\u4e3a\u5c0f\u5199\u540e\u518d\u8fdb\u884c\u6392\u5e8f\uff0c\u89e3\u51b3\u4e86\u5ffd\u7565\u5927\u5c0f\u5199\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u5305\u542bNone\u7684\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5728\u6392\u5e8f\u5305\u542bNone\u7684\u5217\u8868\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230TypeError\u9519\u8bef\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u901a\u8fc7key\u53c2\u6570\u8fdb\u884c\u7279\u6b8a\u5904\u7406\uff0c\u4f8b\u5982\u5c06None\u503c\u66ff\u6362\u4e3a\u67d0\u4e2a\u7279\u5b9a\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>data = [10, None, 30, None, 50]<\/p>\n<h2><strong>\u5c06None\u503c\u66ff\u6362\u4e3a-1\u8fdb\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_data = sorted(data, key=lambda x: (x is None, x))<\/p>\n<p>print(sorted_data)  # \u8f93\u51fa: [10, 30, 50, None, None]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5c06None\u503c\u66ff\u6362\u4e3a-1\u8fdb\u884c\u6392\u5e8f\uff0c\u89e3\u51b3\u4e86\u5305\u542bNone\u7684\u5217\u8868\u6392\u5e8f\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001sorted\u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u5f00\u9500\u3002sorted\u51fd\u6570\u652f\u6301\u751f\u6210\u5668\u8868\u8fbe\u5f0f\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728\u6392\u5e8f\u524d\u5148\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u751f\u6210\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>data = range(1000000, 0, -1)<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u8fdb\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_data = sorted((x for x in data))<\/p>\n<p>print(sorted_data[:10])  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u51cf\u5c11\u4e86\u5185\u5b58\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u590d\u6742\u6392\u5e8f\u9700\u6c42\u4e2d\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u5c06\u590d\u6742\u7684\u8ba1\u7b97\u7ed3\u679c\u7f13\u5b58\u5230\u5b57\u5178\u4e2d\uff0c\u7136\u540e\u5728\u6392\u5e8f\u65f6\u76f4\u63a5\u4f7f\u7528\u7f13\u5b58\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>data = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;]<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u4e2a\u5355\u8bcd\u7684\u957f\u5ea6\uff0c\u5e76\u7f13\u5b58\u7ed3\u679c<\/strong><\/h2>\n<p>length_cache = {word: len(word) for word in data}<\/p>\n<h2><strong>\u4f7f\u7528\u7f13\u5b58\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_data = sorted(data, key=lambda x: length_cache[x], reverse=True)<\/p>\n<p>print(sorted_data)  # \u8f93\u51fa: [&#39;banana&#39;, &#39;cherry&#39;, &#39;apple&#39;, &#39;date&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5c06\u8ba1\u7b97\u7ed3\u679c\u7f13\u5b58\u5230\u5b57\u5178\u4e2d\uff0c\u907f\u514d\u4e86\u91cd\u590d\u8ba1\u7b97\uff0c\u63d0\u9ad8\u4e86\u6392\u5e8f\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u7684sorted\u51fd\u6570\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u6392\u5e8f\u5de5\u5177\uff0c\u901a\u8fc7\u5408\u7406\u4f7f\u7528reverse\u53c2\u6570\u3001key\u53c2\u6570\u548clambda\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u79cd\u6392\u5e8f\u9700\u6c42\u3002\u7ed3\u5408\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6700\u4f73\u5b9e\u8df5\u548c\u6027\u80fd\u4f18\u5316\u65b9\u6cd5\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6ce8\u610f\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6392\u5e8f\u64cd\u4f5c\u548c\u91cd\u590d\u8ba1\u7b97\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u6027\u80fd\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u8bfb\u8005\u80fd\u591f\u66f4\u597d\u5730\u638c\u63e1\u548c\u5e94\u7528Python\u7684sorted\u51fd\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528sorted\u51fd\u6570\u8fdb\u884c\u53cd\u5411\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0csorted\u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\u3002\u5982\u679c\u9700\u8981\u5b9e\u73b0\u53cd\u5411\u6392\u5e8f\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u53c2\u6570<code>reverse=True<\/code>\u6765\u5b8c\u6210\u3002\u4f8b\u5982\uff0c<code>sorted(my_list, reverse=True)<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\u7684\u65b0\u5217\u8868\u3002<\/p>\n<p><strong>sorted\u51fd\u6570\u4e0esort\u65b9\u6cd5\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>sorted\u51fd\u6570\u548clist\u7684sort\u65b9\u6cd5\u90fd\u53ef\u4ee5\u7528\u6765\u6392\u5e8f\uff0c\u4f46\u662f\u6709\u4e00\u4e9b\u5173\u952e\u533a\u522b\u3002sorted\u51fd\u6570\u53ef\u4ee5\u5bf9\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u800csort\u65b9\u6cd5\u662flist\u5bf9\u8c61\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4f1a\u76f4\u63a5\u5728\u539f\u5217\u8868\u4e0a\u8fdb\u884c\u6392\u5e8f\uff0c\u4e0d\u4f1a\u8fd4\u56de\u65b0\u5217\u8868\u3002\u56e0\u6b64\uff0c\u5982\u679c\u9700\u8981\u4fdd\u7559\u539f\u59cb\u5217\u8868\uff0c\u4f7f\u7528sorted\u51fd\u6570\u662f\u66f4\u5408\u9002\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5bf9\u5b57\u5178\u7684\u503c\u8fdb\u884c\u53cd\u5411\u6392\u5e8f\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5bf9\u5b57\u5178\u7684\u503c\u8fdb\u884c\u53cd\u5411\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528sorted\u51fd\u6570\u7ed3\u5408\u5b57\u5178\u7684items\u65b9\u6cd5\u3002\u901a\u8fc7\u8bbe\u7f6e<code>key=lambda item: item[1]<\/code>\u548c<code>reverse=True<\/code>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u5b57\u5178\u503c\u7684\u53cd\u5411\u6392\u5e8f\u3002\u4f8b\u5982\uff1a<code>sorted(my_dict.items(), key=lambda item: item[1], reverse=True)<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u9879\u7684\u5217\u8868\uff0c\u6309\u7167\u503c\u4ece\u5927\u5230\u5c0f\u6392\u5217\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u4f7f\u7528sorted\u51fd\u6570\u8fdb\u884c\u53cd\u5411\u6392\u5e8f\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528reverse\u53c2\u6570\u3001\u4f7f\u7528lambda\u51fd\u6570\u8fdb\u884c [&hellip;]","protected":false},"author":3,"featured_media":1161343,"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\/1161339"}],"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=1161339"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161339\/revisions"}],"predecessor-version":[{"id":1161344,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161339\/revisions\/1161344"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1161343"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1161339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1161339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1161339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}