{"id":1081867,"date":"2025-01-08T12:43:28","date_gmt":"2025-01-08T04:43:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1081867.html"},"modified":"2025-01-08T12:43:31","modified_gmt":"2025-01-08T04:43:31","slug":"python%e4%b8%adlist%e5%a6%82%e4%bd%95%e6%8c%89%e7%85%a7%e6%9f%90%e5%88%97%e6%8e%92%e5%ba%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1081867.html","title":{"rendered":"python\u4e2dlist\u5982\u4f55\u6309\u7167\u67d0\u5217\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183648\/0fad937e-6fb2-47c9-af4f-19df280719c0.webp\" alt=\"python\u4e2dlist\u5982\u4f55\u6309\u7167\u67d0\u5217\u6392\u5e8f\" \/><\/p>\n<p><p> \u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u5217\u8868\u6309\u7167\u67d0\u5217\u8fdb\u884c\u6392\u5e8f\u3002<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528 <code>sort<\/code> \u65b9\u6cd5\u3001<code>sorted<\/code> \u51fd\u6570\u3001<code>itemgetter<\/code> \u6a21\u5757\u3001lambda \u8868\u8fbe\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0c<code>sort<\/code> \u65b9\u6cd5\u548c <code>sorted<\/code> \u51fd\u6570\u662f\u5185\u7f6e\u7684\u6392\u5e8f\u65b9\u6cd5\uff0c\u800c <code>itemgetter<\/code> \u6a21\u5757\u548c lambda \u8868\u8fbe\u5f0f\u662f\u7528\u4e8e\u6307\u5b9a\u6392\u5e8f\u5173\u952e\u5b57\u7684\u5de5\u5177\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 <code>sorted<\/code> \u51fd\u6570\u548c <code>lambda<\/code> \u8868\u8fbe\u5f0f\u6765\u6392\u5e8f<\/strong>\uff1a<code>sorted<\/code> \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u6309\u7167\u6307\u5b9a\u7684\u5173\u952e\u5b57\u6392\u5e8f\u3002\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a lambda \u8868\u8fbe\u5f0f\u4f5c\u4e3a <code>key<\/code> \u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u6309\u5217\u8868\u7684\u54ea\u4e00\u5217\u8fdb\u884c\u6392\u5e8f\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\u4e14\u6613\u4e8e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528 <code>sort<\/code> \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><code>sort<\/code> \u65b9\u6cd5\u7528\u4e8e\u5bf9\u5217\u8868\u8fdb\u884c\u539f\u5730\u6392\u5e8f\uff0c\u6539\u53d8\u5217\u8868\u672c\u8eab\u3002\u5b83\u6709\u4e00\u4e2a <code>key<\/code> \u53c2\u6570\uff0c\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u51fd\u6570\u6765\u6307\u5b9a\u6392\u5e8f\u7684\u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [[1, 2, 3], [4, 1, 6], [3, 5, 2]]<\/p>\n<p>data.sort(key=lambda x: x[1])  # \u6309\u7b2c\u4e8c\u5217\u6392\u5e8f<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>lambda x: x[1]<\/code> \u4f5c\u4e3a <code>key<\/code> \u53c2\u6570\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u6309\u7167\u6bcf\u4e2a\u5b50\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002\u6392\u5e8f\u540e\u7684\u5217\u8868\u5c06\u4f1a\u662f <code>[[4, 1, 6], [1, 2, 3], [3, 5, 2]]<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528 <code>sorted<\/code> \u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>sorted<\/code> \u51fd\u6570\u4e0e <code>sort<\/code> \u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u5b83\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u540e\u7684\u5217\u8868\uff0c\u4e0d\u4f1a\u6539\u53d8\u539f\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [[1, 2, 3], [4, 1, 6], [3, 5, 2]]<\/p>\n<p>sorted_data = sorted(data, key=lambda x: x[1])  # \u6309\u7b2c\u4e8c\u5217\u6392\u5e8f<\/p>\n<p>print(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e0e\u4e0a\u4e00\u4e2a\u57fa\u672c\u76f8\u540c\uff0c\u4e0d\u540c\u7684\u662f <code>sorted<\/code> \u8fd4\u56de\u4e86\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u6240\u4ee5\u539f\u59cb\u7684 <code>data<\/code> \u5217\u8868\u4e0d\u4f1a\u6539\u53d8\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528 <code>itemgetter<\/code> \u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>itemgetter<\/code> \u662f <code>operator<\/code> \u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u83b7\u53d6\u5bf9\u8c61\u7684\u67d0\u4e2a\u5b57\u6bb5\u3002\u5b83\u53ef\u4ee5\u63d0\u9ad8\u6392\u5e8f\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from operator import itemgetter<\/p>\n<p>data = [[1, 2, 3], [4, 1, 6], [3, 5, 2]]<\/p>\n<p>sorted_data = sorted(data, key=itemgetter(1))  # \u6309\u7b2c\u4e8c\u5217\u6392\u5e8f<\/p>\n<p>print(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>itemgetter(1)<\/code> \u6765\u4ee3\u66ff <code>lambda x: x[1]<\/code>\uff0c\u6548\u679c\u662f\u4e00\u6837\u7684\uff0c\u4f46 <code>itemgetter<\/code> \u901a\u5e38\u66f4\u5feb\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u6309\u591a\u5217\u6392\u5e8f<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6309\u591a\u5217\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5148\u6309\u7b2c\u4e8c\u5217\u6392\u5e8f\uff0c\u518d\u6309\u7b2c\u4e00\u5217\u6392\u5e8f\u3002\u8fd9\u65f6\u53ef\u4ee5\u5c06 <code>key<\/code> \u53c2\u6570\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u8fd4\u56de\u591a\u4e2a\u5173\u952e\u5b57\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [[1, 2, 3], [4, 1, 6], [3, 5, 2], [3, 1, 9]]<\/p>\n<p>sorted_data = sorted(data, key=lambda x: (x[1], x[0]))  # \u5148\u6309\u7b2c\u4e8c\u5217\uff0c\u518d\u6309\u7b2c\u4e00\u5217\u6392\u5e8f<\/p>\n<p>print(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5148\u6309\u7b2c\u4e8c\u5217\u6392\u5e8f\uff0c\u518d\u6309\u7b2c\u4e00\u5217\u6392\u5e8f\u3002\u6392\u5e8f\u540e\u7684\u7ed3\u679c\u5c06\u4f1a\u662f <code>[[4, 1, 6], [3, 1, 9], [1, 2, 3], [3, 5, 2]]<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u9006\u5e8f\u6392\u5e8f<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u6309\u7167\u964d\u5e8f\u6392\u5e8f\uff0c\u53ef\u4ee5\u5c06 <code>reverse<\/code> \u53c2\u6570\u8bbe\u7f6e\u4e3a <code>True<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [[1, 2, 3], [4, 1, 6], [3, 5, 2]]<\/p>\n<p>sorted_data = sorted(data, key=lambda x: x[1], reverse=True)  # \u6309\u7b2c\u4e8c\u5217\u964d\u5e8f\u6392\u5e8f<\/p>\n<p>print(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6309\u7167\u7b2c\u4e8c\u5217\u964d\u5e8f\u6392\u5e8f\uff0c\u6392\u5e8f\u540e\u7684\u7ed3\u679c\u5c06\u4f1a\u662f <code>[[3, 5, 2], [1, 2, 3], [4, 1, 6]]<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u590d\u6742\u6570\u636e\u7ed3\u6784\u6392\u5e8f<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ef\u80fd\u4e0d\u662f\u7b80\u5355\u7684\u6570\u503c\uff0c\u800c\u662f\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5b57\u5178\u3002\u8fd9\u65f6\u6211\u4eec\u4f9d\u7136\u53ef\u4ee5\u4f7f\u7528 <code>key<\/code> \u53c2\u6570\u6765\u6307\u5b9a\u6392\u5e8f\u7684\u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}, {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 20}, {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 23}]<\/p>\n<p>sorted_data = sorted(data, key=lambda x: x[&#39;age&#39;])  # \u6309\u5e74\u9f84\u6392\u5e8f<\/p>\n<p>print(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5b57\u5178\u4ee3\u8868\u4e00\u4e2a\u4eba\u7684\u4fe1\u606f\u3002\u6211\u4eec\u4f7f\u7528 <code>lambda x: x[&#39;age&#39;]<\/code> \u4f5c\u4e3a <code>key<\/code> \u53c2\u6570\uff0c\u6309\u5e74\u9f84\u8fdb\u884c\u6392\u5e8f\u3002\u6392\u5e8f\u540e\u7684\u7ed3\u679c\u5c06\u4f1a\u662f <code> [{&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 20}, {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 23}, {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}]<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u5904\u7406\u7a7a\u503c<\/h2>\n<\/p>\n<p><p>\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5217\u8868\u4e2d\u7684\u67d0\u4e9b\u5143\u7d20\u53ef\u80fd\u5305\u542b\u7a7a\u503c\u6216\u7f3a\u5931\u503c\u3002\u6392\u5e8f\u65f6\u9700\u8981\u8003\u8651\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u7a7a\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [[1, None, 3], [4, 1, 6], [3, 5, 2]]<\/p>\n<p>sorted_data = sorted(data, key=lambda x: (x[1] is None, x[1]))  # \u5c06\u7a7a\u503c\u653e\u5728\u6700\u540e<\/p>\n<p>print(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7 <code>(x[1] is None, x[1])<\/code> \u4f5c\u4e3a <code>key<\/code> \u53c2\u6570\uff0c\u5c06\u7a7a\u503c\u653e\u5728\u6392\u5e8f\u7684\u6700\u540e\u3002\u6392\u5e8f\u540e\u7684\u7ed3\u679c\u5c06\u4f1a\u662f <code>[[4, 1, 6], [3, 5, 2], [1, None, 3]]<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u3002\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6bd4\u8f83\u51fd\u6570\uff0c\u4f20\u9012\u7ed9 <code>sorted<\/code> \u51fd\u6570\u7684 <code>cmp<\/code> \u53c2\u6570\uff08Python 2 \u4e2d\u4f7f\u7528\uff09\u6216\u4f7f\u7528 <code>functools.cmp_to_key<\/code>\uff08Python 3 \u4e2d\u4f7f\u7528\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import functools<\/p>\n<p>data = [[1, 2, 3], [4, 1, 6], [3, 5, 2]]<\/p>\n<p>def custom_compare(x, y):<\/p>\n<p>    if x[1] &lt; y[1]:<\/p>\n<p>        return -1<\/p>\n<p>    elif x[1] &gt; y[1]:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return 0<\/p>\n<p>sorted_data = sorted(data, key=functools.cmp_to_key(custom_compare))  # \u4f7f\u7528\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570<\/p>\n<p>print(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6bd4\u8f83\u51fd\u6570 <code>custom_compare<\/code>\uff0c\u5e76\u4f7f\u7528 <code>functools.cmp_to_key<\/code> \u5c06\u5176\u8f6c\u6362\u4e3a <code>key<\/code> \u51fd\u6570\u4f20\u9012\u7ed9 <code>sorted<\/code> \u51fd\u6570\u3002\u6392\u5e8f\u540e\u7684\u7ed3\u679c\u5c06\u4f1a\u662f <code>[[4, 1, 6], [1, 2, 3], [3, 5, 2]]<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u5904\u7406\u6280\u672f<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6392\u5e8f\u5f80\u5f80\u662f\u6570\u636e\u5904\u7406\u5de5\u4f5c\u7684\u4e00\u90e8\u5206\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u6392\u5e8f\u4e0e\u5176\u4ed6\u6570\u636e\u5904\u7406\u6280\u672f\u7ed3\u5408\u4f7f\u7528\uff0c\u4f8b\u5982\u8fc7\u6ee4\u3001\u6620\u5c04\u548c\u805a\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [[1, 2, 3], [4, 1, 6], [3, 5, 2], [5, 3, 8]]<\/p>\n<h2><strong>\u8fc7\u6ee4\u51fa\u7b2c\u4e09\u5217\u5927\u4e8e2\u7684\u5143\u7d20\uff0c\u7136\u540e\u6309\u7b2c\u4e8c\u5217\u6392\u5e8f<\/strong><\/h2>\n<p>filtered_sorted_data = sorted(filter(lambda x: x[2] &gt; 2, data), key=lambda x: x[1])<\/p>\n<p>print(filtered_sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528 <code>filter<\/code> \u51fd\u6570\u8fc7\u6ee4\u51fa\u7b2c\u4e09\u5217\u5927\u4e8e2\u7684\u5143\u7d20\uff0c\u7136\u540e\u4f7f\u7528 <code>sorted<\/code> \u51fd\u6570\u6309\u7b2c\u4e8c\u5217\u6392\u5e8f\u3002\u5904\u7406\u540e\u7684\u7ed3\u679c\u5c06\u4f1a\u662f <code>[[5, 3, 8], [1, 2, 3], [3, 5, 2]]<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u3001\u4f7f\u7528 Pandas \u8fdb\u884c\u6392\u5e8f<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0cPandas \u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Pandas \u7684 <code>sort_values<\/code> \u65b9\u6cd5\u5bf9 DataFrame \u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.DataFrame({<\/p>\n<p>    &#39;A&#39;: [1, 4, 3],<\/p>\n<p>    &#39;B&#39;: [2, 1, 5],<\/p>\n<p>    &#39;C&#39;: [3, 6, 2]<\/p>\n<p>})<\/p>\n<p>sorted_data = data.sort_values(by=&#39;B&#39;)  # \u6309\u5217 B \u6392\u5e8f<\/p>\n<p>print(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u5217\u7684 DataFrame\uff0c\u5e76\u4f7f\u7528 <code>sort_values<\/code> \u65b9\u6cd5\u6309\u5217 <code>B<\/code> \u8fdb\u884c\u6392\u5e8f\u3002\u6392\u5e8f\u540e\u7684\u7ed3\u679c\u5c06\u4f1a\u662f\u4e00\u4e2a\u65b0\u7684 DataFrame\uff0c\u6309\u5217 <code>B<\/code> \u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u5217\u8868\u6309\u7167\u67d0\u5217\u8fdb\u884c\u6392\u5e8f\u3002\u4ece\u7b80\u5355\u7684 <code>sort<\/code> \u65b9\u6cd5\u548c <code>sorted<\/code> \u51fd\u6570\uff0c\u5230\u66f4\u590d\u6742\u7684 <code>itemgetter<\/code> \u6a21\u5757\u548c lambda \u8868\u8fbe\u5f0f\uff0c\u518d\u5230\u5904\u7406\u7a7a\u503c\u3001\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u548c\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u5904\u7406\u6280\u672f\uff0c\u751a\u81f3\u4f7f\u7528 Pandas \u8fdb\u884c\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u5b9e\u73b0\u5404\u79cd\u6392\u5e8f\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5bf9\u5d4c\u5957\u5217\u8868\u6309\u7279\u5b9a\u5217\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u6216\u8005<code>list.sort()<\/code>\u65b9\u6cd5\u5bf9\u5d4c\u5957\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u901a\u8fc7<code>key<\/code>\u53c2\u6570\u6307\u5b9a\u6392\u5e8f\u4f9d\u636e\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b50\u5217\u8868\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5b50\u5217\u8868\u4ee3\u8868\u4e00\u884c\u6570\u636e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5217\u7d22\u5f15\u6765\u8fdb\u884c\u6392\u5e8f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">data = [[1, &#39;Alice&#39;, 25], [2, &#39;Bob&#39;, 22], [3, &#39;Charlie&#39;, 30]]\nsorted_data = sorted(data, key=lambda x: x[2])  # \u6309\u5e74\u9f84\u6392\u5e8f\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u662f\u5426\u53ef\u4ee5\u6309\u591a\u4e2a\u5217\u8fdb\u884c\u6392\u5e8f\uff1f\u5982\u679c\u53ef\u4ee5\uff0c\u8be5\u5982\u4f55\u5b9e\u73b0\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728<code>key<\/code>\u53c2\u6570\u4e2d\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\u6765\u5b9e\u73b0\u6309\u591a\u4e2a\u5217\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5e0c\u671b\u9996\u5148\u6309\u5e74\u9f84\u6392\u5e8f\uff0c\u7136\u540e\u6309\u540d\u5b57\u6392\u5e8f\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">sorted_data = sorted(data, key=lambda x: (x[2], x[1]))  # \u5148\u6309\u5e74\u9f84\uff0c\u518d\u6309\u540d\u5b57\u6392\u5e8f\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u4e86\u6570\u636e\u7684\u591a\u91cd\u6392\u5e8f\u903b\u8f91\u5f97\u4ee5\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u4f7f\u7528Pandas\u5e93\u65f6\uff0c\u5982\u4f55\u5bf9DataFrame\u6309\u5217\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5728\u5904\u7406\u6570\u636e\u65f6\u4f7f\u7528Pandas\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sort_values()<\/code>\u51fd\u6570\u6765\u5bf9DataFrame\u6309\u67d0\u4e00\u5217\u6216\u591a\u5217\u8fdb\u884c\u6392\u5e8f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndf = pd.DataFrame({&#39;ID&#39;: [1, 2, 3], &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;Age&#39;: [25, 22, 30]})\nsorted_df = df.sort_values(by=&#39;Age&#39;)  # \u6309\u5e74\u9f84\u6392\u5e8f\n<\/code><\/pre>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u901a\u8fc7<code>ascending<\/code>\u53c2\u6570\u6307\u5b9a\u5347\u5e8f\u6216\u964d\u5e8f\u6392\u5217\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u7ba1\u7406\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u5217\u8868\u6309\u7167\u67d0\u5217\u8fdb\u884c\u6392\u5e8f\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528 sort \u65b9\u6cd5\u3001sort [&hellip;]","protected":false},"author":3,"featured_media":1081875,"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\/1081867"}],"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=1081867"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081867\/revisions"}],"predecessor-version":[{"id":1081877,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081867\/revisions\/1081877"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1081875"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1081867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1081867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1081867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}