{"id":1184792,"date":"2025-01-15T19:30:52","date_gmt":"2025-01-15T11:30:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1184792.html"},"modified":"2025-01-15T19:30:56","modified_gmt":"2025-01-15T11:30:56","slug":"python%e5%88%97%e8%a1%a8%e4%b8%ad%e6%9c%89%e5%88%97%e8%a1%a8%e5%a6%82%e4%bd%95%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1184792.html","title":{"rendered":"python\u5217\u8868\u4e2d\u6709\u5217\u8868\u5982\u4f55\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25134121\/ea1b80c7-60ca-44a8-a7da-35dda5dd1d43.webp\" alt=\"python\u5217\u8868\u4e2d\u6709\u5217\u8868\u5982\u4f55\u6392\u5e8f\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u6216\u5217\u8868\u7684<code>sort()<\/code>\u65b9\u6cd5\u5bf9\u5305\u542b\u5217\u8868\u7684\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u8981\u5bf9\u5305\u542b\u5217\u8868\u7684\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u9700\u8981\u6307\u5b9a\u6392\u5e8f\u7684\u5173\u952e\u5b57\uff08key\uff09\u51fd\u6570\u3002\u5173\u952e\u5b57\u51fd\u6570\u7528\u4e8e\u63d0\u53d6\u6bcf\u4e2a\u5b50\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\uff0c\u4ee5\u6b64\u4f5c\u4e3a\u6392\u5e8f\u7684\u4f9d\u636e\u3002\u5e38\u89c1\u7684\u6392\u5e8f\u4f9d\u636e\u5305\u62ec\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3001\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7b49\u8fdb\u884c\u6392\u5e8f\u3002<strong>\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5\u3001\u6307\u5b9a<code>key<\/code>\u5173\u952e\u5b57\u51fd\u6570<\/strong>\u7b49\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u79cd\u65b9\u6cd5\u6765\u6392\u5e8f\u5305\u542b\u5217\u8868\u7684\u5217\u8868\uff0c\u5e76\u8be6\u7ec6\u5c55\u5f00\u5176\u4e2d\u7684\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u8fdb\u884c\u6392\u5e8f\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>sorted()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5df2\u6392\u5e8f\u7684\u5217\u8868\uff0c\u800c\u4e0d\u4f1a\u6539\u53d8\u539f\u6765\u7684\u5217\u8868\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a<code>key<\/code>\u53c2\u6570\u6765\u786e\u5b9a\u6392\u5e8f\u7684\u4f9d\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>nested_list = [[3, 2, 5], [1, 4, 4], [2, 6, 3]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=lambda x: x[0])<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[1, 4, 4], [2, 6, 3], [3, 2, 5]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=lambda x: x[1])<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[3, 2, 5], [1, 4, 4], [2, 6, 3]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=lambda x: x[2])<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[2, 6, 3], [1, 4, 4], [3, 2, 5]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u7684<code>sort()<\/code>\u65b9\u6cd5\u4f1a\u5bf9\u539f\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de<code>None<\/code>\u3002\u540c\u6837\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a<code>key<\/code>\u53c2\u6570\u6765\u786e\u5b9a\u6392\u5e8f\u7684\u4f9d\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>nested_list = [[3, 2, 5], [1, 4, 4], [2, 6, 3]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>nested_list.sort(key=lambda x: x[0])<\/p>\n<p>print(nested_list)  # \u8f93\u51fa: [[1, 4, 4], [2, 6, 3], [3, 2, 5]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>nested_list.sort(key=lambda x: x[1])<\/p>\n<p>print(nested_list)  # \u8f93\u51fa: [[3, 2, 5], [1, 4, 4], [2, 6, 3]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>nested_list.sort(key=lambda x: x[2])<\/p>\n<p>print(nested_list)  # \u8f93\u51fa: [[2, 6, 3], [1, 4, 4], [3, 2, 5]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6307\u5b9a<code>key<\/code>\u5173\u952e\u5b57\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u6392\u5e8f\u5305\u542b\u5217\u8868\u7684\u5217\u8868\u65f6\uff0c<code>key<\/code>\u53c2\u6570\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u53c2\u6570\uff0c\u5b83\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f1a\u88ab\u5e94\u7528\u4e8e\u6bcf\u4e00\u4e2a\u5b50\u5217\u8868\uff0c\u5e76\u8fd4\u56de\u7528\u4e8e\u6392\u5e8f\u7684\u4f9d\u636e\u3002\u53ef\u4ee5\u901a\u8fc7<code>lambda<\/code>\u51fd\u6570\u6216\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u6307\u5b9a<code>key<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528<code>lambda<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>lambda<\/code>\u51fd\u6570\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u533f\u540d\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9a\u4e49\u7b80\u5355\u7684\u6392\u5e8f\u4f9d\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>nested_list = [[3, 2, 5], [1, 4, 4], [2, 6, 3]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=lambda x: x[0])<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[1, 4, 4], [2, 6, 3], [3, 2, 5]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=lambda x: x[1])<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[3, 2, 5], [1, 4, 4], [2, 6, 3]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=lambda x: x[2])<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[2, 6, 3], [1, 4, 4], [3, 2, 5]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u8fd4\u56de\u7528\u4e8e\u6392\u5e8f\u7684\u4f9d\u636e\uff0c\u7136\u540e\u5c06\u8be5\u51fd\u6570\u4f20\u9012\u7ed9<code>key<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>nested_list = [[3, 2, 5], [1, 4, 4], [2, 6, 3]]<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u8fd4\u56de\u5b50\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>def sort_by_first_element(sublist):<\/p>\n<p>    return sublist[0]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=sort_by_first_element)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[1, 4, 4], [2, 6, 3], [3, 2, 5]]<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u8fd4\u56de\u5b50\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>def sort_by_second_element(sublist):<\/p>\n<p>    return sublist[1]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=sort_by_second_element)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[3, 2, 5], [1, 4, 4], [2, 6, 3]]<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u8fd4\u56de\u5b50\u5217\u8868\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>def sort_by_third_element(sublist):<\/p>\n<p>    return sublist[2]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=sort_by_third_element)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[2, 6, 3], [1, 4, 4], [3, 2, 5]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u591a\u7ea7\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6309\u591a\u4e2a\u7ea7\u522b\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5b50\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u76f8\u540c\uff0c\u53ef\u4ee5\u7ee7\u7eed\u6309\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6392\u5e8f\u3002\u53ef\u4ee5\u901a\u8fc7<code>key<\/code>\u53c2\u6570\u6307\u5b9a\u4e00\u4e2a\u5143\u7ec4\u4f5c\u4e3a\u6392\u5e8f\u4f9d\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>nested_list = [[3, 2, 5], [1, 4, 4], [2, 6, 3], [3, 1, 2], [1, 4, 2]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u548c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=lambda x: (x[0], x[1]))<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[1, 4, 2], [1, 4, 4], [2, 6, 3], [3, 1, 2], [3, 2, 5]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u9006\u5e8f\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>reverse<\/code>\u53c2\u6570\u4e3a<code>True<\/code>\u6765\u8fdb\u884c\u9006\u5e8f\u6392\u5e8f\uff08\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>nested_list = [[3, 2, 5], [1, 4, 4], [2, 6, 3]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u9006\u5e8f\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=lambda x: x[0], reverse=True)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[3, 2, 5], [2, 6, 3], [1, 4, 4]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u9006\u5e8f\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=lambda x: x[1], reverse=True)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[2, 6, 3], [1, 4, 4], [3, 2, 5]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20\u9006\u5e8f\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=lambda x: x[2], reverse=True)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[3, 2, 5], [1, 4, 4], [2, 6, 3]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5904\u7406\u5d4c\u5957\u66f4\u6df1\u7684\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u5217\u8868\u5d4c\u5957\u5c42\u6b21\u66f4\u6df1\uff0c\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u6216\u66f4\u590d\u6742\u7684<code>key<\/code>\u51fd\u6570\u6765\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u4e09\u5c42\u5d4c\u5957\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u6309\u6700\u5185\u5c42\u7684\u67d0\u4e2a\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>nested_list = [[[3, 2, 5], [1, 4, 4]], [[2, 6, 3], [3, 1, 2]]]<\/p>\n<h2><strong>\u6309\u6700\u5185\u5c42\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>def sort_by_inner_element(sublist):<\/p>\n<p>    return sublist[0][0]<\/p>\n<p>sorted_list = sorted(nested_list, key=sort_by_inner_element)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[[2, 6, 3], [3, 1, 2]], [[3, 2, 5], [1, 4, 4]]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5b50\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5b50\u5217\u8868\u7684\u957f\u5ea6\u4e0d\u4e00\u81f4\uff0c\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u5b50\u5217\u8868\u7684\u957f\u5ea6\u6765\u5904\u7406\u7f3a\u5931\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u6765\u5904\u7406\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>nested_list = [[3, 2], [1, 4, 4], [2, 6, 3]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20\u6392\u5e8f\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u89c6\u4e3a0<\/strong><\/h2>\n<p>def sort_by_third_element(sublist):<\/p>\n<p>    try:<\/p>\n<p>        return sublist[2]<\/p>\n<p>    except IndexError:<\/p>\n<p>        return 0<\/p>\n<p>sorted_list = sorted(nested_list, key=sort_by_third_element)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[3, 2], [2, 6, 3], [1, 4, 4]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5904\u7406\u5b57\u7b26\u4e32\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5b50\u5217\u8868\u4e2d\u5305\u542b\u5b57\u7b26\u4e32\u5143\u7d20\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>nested_list = [[&#39;apple&#39;, &#39;banana&#39;], [&#39;cherry&#39;, &#39;date&#39;], [&#39;elderberry&#39;, &#39;fig&#39;]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=lambda x: x[0])<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[&#39;apple&#39;, &#39;banana&#39;], [&#39;cherry&#39;, &#39;date&#39;], [&#39;elderberry&#39;, &#39;fig&#39;]]<\/p>\n<h2><strong>\u6309\u5b50\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=lambda x: x[1])<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[&#39;apple&#39;, &#39;banana&#39;], [&#39;cherry&#39;, &#39;date&#39;], [&#39;elderberry&#39;, &#39;fig&#39;]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7efc\u5408\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u7ed3\u5408\u4ee5\u4e0a\u5404\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684\u6392\u5e8f\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u5143\u7d20\u7684\u5d4c\u5957\u5217\u8868\uff0c\u53ef\u4ee5\u6309\u591a\u4e2a\u7ea7\u522b\u548c\u4e0d\u540c\u7c7b\u578b\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>nested_list = [[3, &#39;banana&#39;, 5], [1, &#39;apple&#39;, 4], [2, &#39;cherry&#39;, 3], [3, &#39;apple&#39;, 2]]<\/p>\n<h2><strong>\u6309\u7b2c\u4e00\u4e2a\u5143\u7d20\u548c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=lambda x: (x[0], x[1]))<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[1, &#39;apple&#39;, 4], [2, &#39;cherry&#39;, 3], [3, &#39;apple&#39;, 2], [3, &#39;banana&#39;, 5]]<\/p>\n<h2><strong>\u6309\u7b2c\u4e09\u4e2a\u5143\u7d20\u9006\u5e8f\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_list = sorted(nested_list, key=lambda x: x[2], reverse=True)<\/p>\n<p>print(sorted_list)  # \u8f93\u51fa: [[3, &#39;banana&#39;, 5], [1, &#39;apple&#39;, 4], [2, &#39;cherry&#39;, 3], [3, &#39;apple&#39;, 2]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u5bf9\u5305\u542b\u5217\u8868\u7684\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u6ee1\u8db3\u5404\u79cd\u6392\u5e8f\u9700\u6c42\u3002\u65e0\u8bba\u662f\u6309\u5355\u4e2a\u5143\u7d20\u3001\u591a\u7ea7\u6392\u5e8f\u3001\u9006\u5e8f\u6392\u5e8f\uff0c\u8fd8\u662f\u5904\u7406\u5d4c\u5957\u66f4\u6df1\u7684\u5217\u8868\u3001\u4e0d\u540c\u957f\u5ea6\u7684\u5b50\u5217\u8868\u548c\u5b57\u7b26\u4e32\u5143\u7d20\uff0c\u90fd\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5bf9\u5305\u542b\u5b50\u5217\u8868\u7684Python\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u6216\u5217\u8868\u7684<code>sort()<\/code>\u65b9\u6cd5\u5bf9\u5305\u542b\u5b50\u5217\u8868\u7684\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u9700\u8981\u6307\u5b9a\u4e00\u4e2a<code>key<\/code>\u53c2\u6570\uff0c\u901a\u5e38\u53ef\u4ee5\u9009\u62e9\u5b50\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u7d22\u5f15\u6765\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>data = [[1, 3], [2, 2], [3, 1]]<\/code>\uff0c\u5e76\u5e0c\u671b\u6839\u636e\u6bcf\u4e2a\u5b50\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">sorted_data = sorted(data, key=lambda x: x[1])\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>sorted_data<\/code>\u5c06\u4f1a\u662f<code>[[3, 1], [2, 2], [1, 3]]<\/code>\uff0c\u6309\u7167\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u5927\u5c0f\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u5417\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u6392\u5e8f\u903b\u8f91\u3002\u6bd4\u5982\uff0c\u5982\u679c\u5e0c\u671b\u9996\u5148\u6309\u7167\u5b50\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u6392\u5e8f\uff0c\u82e5\u76f8\u540c\u518d\u6309\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6392\u5e8f\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">sorted_data = sorted(data, key=lambda x: (x[0], x[1]))\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u6392\u5e8f\u5c06\u4f1a\u4f18\u5148\u8003\u8651\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u4f5c\u4e3a\u6b21\u8981\u6761\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u4e0d\u540c\u957f\u5ea6\u5b50\u5217\u8868\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5b50\u5217\u8868\u65f6\uff0c\u6392\u5e8f\u53ef\u80fd\u4f1a\u53d8\u5f97\u590d\u6742\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u9009\u62e9\u4e00\u4e2a\u9ed8\u8ba4\u503c\u6765\u586b\u5145\u8f83\u77ed\u7684\u5b50\u5217\u8868\u4ee5\u4fdd\u8bc1\u6392\u5e8f\u7684\u7edf\u4e00\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>itertools.zip_longest<\/code>\u6765\u5bf9\u9f50\u5b50\u5217\u8868\u3002\u5982\u679c\u67d0\u4e9b\u5b50\u5217\u8868\u7f3a\u5c11\u5143\u7d20\uff0c\u53ef\u4ee5\u9009\u62e9\u7528<code>None<\/code>\u6216\u5176\u4ed6\u5360\u4f4d\u7b26\u586b\u5145\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u56e0\u4e3a\u957f\u5ea6\u4e0d\u540c\u800c\u5bfc\u81f4\u6392\u5e8f\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684sorted()\u51fd\u6570\u6216\u5217\u8868\u7684sort()\u65b9\u6cd5\u5bf9\u5305\u542b\u5217\u8868\u7684\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u8981\u5bf9 [&hellip;]","protected":false},"author":3,"featured_media":1184799,"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\/1184792"}],"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=1184792"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184792\/revisions"}],"predecessor-version":[{"id":1184801,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184792\/revisions\/1184801"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1184799"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1184792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1184792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1184792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}