{"id":1149653,"date":"2025-01-13T16:52:03","date_gmt":"2025-01-13T08:52:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149653.html"},"modified":"2025-01-13T16:52:06","modified_gmt":"2025-01-13T08:52:06","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8%e5%87%bd%e6%95%b0%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1149653.html","title":{"rendered":"python\u5982\u4f55\u7528\u51fd\u6570\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25180313\/b52d6d96-d313-4c5c-91cb-0b1da542a66f.webp\" alt=\"python\u5982\u4f55\u7528\u51fd\u6570\u6392\u5e8f\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f<\/strong>\u3001<strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684sorted()\u51fd\u6570\u8fdb\u884c\u6392\u5e8f<\/strong>\u3001<strong>sorted()\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u51fd\u6570<\/strong>\u3001<strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528list.sort()\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528sorted()\u51fd\u6570\u8fdb\u884c\u6392\u5e8f\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528sorted()\u51fd\u6570\u8fdb\u884c\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002<code>sorted()<\/code>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd8\u53ef\u4ee5\u5bf9\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\u3002\u6700\u57fa\u672c\u7684\u7528\u6cd5\u662f\u76f4\u63a5\u4f20\u5165\u4e00\u4e2a\u5217\u8868\uff0c\u8fd4\u56de\u4e00\u4e2a\u6392\u5e8f\u540e\u7684\u65b0\u5217\u8868\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)  # \u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>sorted()<\/code>\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a<code>key<\/code>\u53c2\u6570\uff0c\u8fd9\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7528\u4e8e\u4ece\u6bcf\u4e2a\u5217\u8868\u5143\u7d20\u4e2d\u63d0\u53d6\u4e00\u4e2a\u7528\u4e8e\u6bd4\u8f83\u7684\u952e\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u6309\u7167\u4f60\u5b9a\u4e49\u7684\u89c4\u5219\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u7167\u5b57\u7b26\u4e32\u957f\u5ea6\u6392\u5e8f<\/p>\n<p>words = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;]<\/p>\n<p>sorted_words = sorted(words, key=len)<\/p>\n<p>print(sorted_words)  # \u8f93\u51fa: [&#39;date&#39;, &#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528lambda\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>lambda<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6392\u5e8f\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u7684\u5217\u8868\uff0c\u5e76\u4e14\u4f60\u60f3\u8981\u6309\u7167\u5b57\u5178\u4e2d\u67d0\u4e2a\u952e\u7684\u503c\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = [<\/p>\n<p>    {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 25},<\/p>\n<p>    {&quot;name&quot;: &quot;Jane&quot;, &quot;age&quot;: 22},<\/p>\n<p>    {&quot;name&quot;: &quot;Dave&quot;, &quot;age&quot;: 23}<\/p>\n<p>]<\/p>\n<p>sorted_students = sorted(students, key=lambda student: student[&quot;age&quot;])<\/p>\n<p>print(sorted_students)<\/p>\n<h2><strong>\u8f93\u51fa: [{&#39;name&#39;: &#39;Jane&#39;, &#39;age&#39;: 22}, {&#39;name&#39;: &#39;Dave&#39;, &#39;age&#39;: 23}, {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528list.sort()\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u4e0e<code>sorted()<\/code>\u51fd\u6570\u4e0d\u540c\uff0c<code>list.sort()<\/code>\u65b9\u6cd5\u4f1a\u5bf9\u5217\u8868\u8fdb\u884c\u539f\u5730\u6392\u5e8f\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u4e0d\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u800c\u662f\u76f4\u63a5\u4fee\u6539\u539f\u59cb\u5217\u8868\u3002<code>list.sort()<\/code>\u65b9\u6cd5\u4e5f\u63a5\u53d7\u4e00\u4e2a<code>key<\/code>\u53c2\u6570\uff0c\u7528\u6cd5\u4e0e<code>sorted()<\/code>\u51fd\u6570\u76f8\u540c\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)  # \u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528key\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u540c\u6837\u7684\uff0c<code>list.sort()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u901a\u8fc7<code>key<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = [<\/p>\n<p>    {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 25},<\/p>\n<p>    {&quot;name&quot;: &quot;Jane&quot;, &quot;age&quot;: 22},<\/p>\n<p>    {&quot;name&quot;: &quot;Dave&quot;, &quot;age&quot;: 23}<\/p>\n<p>]<\/p>\n<p>students.sort(key=lambda student: student[&quot;age&quot;])<\/p>\n<p>print(students)<\/p>\n<h2><strong>\u8f93\u51fa: [{&#39;name&#39;: &#39;Jane&#39;, &#39;age&#39;: 22}, {&#39;name&#39;: &#39;Dave&#39;, &#39;age&#39;: 23}, {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u964d\u5e8f\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u8fd8\u662f<code>list.sort()<\/code>\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>reverse<\/code>\u53c2\u6570\u4e3a<code>True<\/code>\u6765\u5b9e\u73b0\u964d\u5e8f\u6392\u5e8f\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, reverse=True)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [9, 6, 5, 5, 2, 1]<\/p>\n<p>students.sort(key=lambda student: student[&quot;age&quot;], reverse=True)<\/p>\n<p>print(students)<\/p>\n<h2><strong>\u8f93\u51fa: [{&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}, {&#39;name&#39;: &#39;Dave&#39;, &#39;age&#39;: 23}, {&#39;name&#39;: &#39;Jane&#39;, &#39;age&#39;: 22}]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u590d\u6742\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u8fdb\u884c\u6392\u5e8f\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u6761\u4ef6\u7ec4\u5408\u5230<code>key<\/code>\u51fd\u6570\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = [<\/p>\n<p>    {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 25, &quot;grade&quot;: 90},<\/p>\n<p>    {&quot;name&quot;: &quot;Jane&quot;, &quot;age&quot;: 22, &quot;grade&quot;: 85},<\/p>\n<p>    {&quot;name&quot;: &quot;Dave&quot;, &quot;age&quot;: 23, &quot;grade&quot;: 90},<\/p>\n<p>    {&quot;name&quot;: &quot;Lucy&quot;, &quot;age&quot;: 22, &quot;grade&quot;: 92}<\/p>\n<p>]<\/p>\n<h2><strong>\u5148\u6309age\u6392\u5e8f\uff0c\u518d\u6309grade\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_students = sorted(students, key=lambda student: (student[&quot;age&quot;], student[&quot;grade&quot;]))<\/p>\n<p>print(sorted_students)<\/p>\n<h2><strong>\u8f93\u51fa: [{&#39;name&#39;: &#39;Jane&#39;, &#39;age&#39;: 22, &#39;grade&#39;: 85}, {&#39;name&#39;: &#39;Lucy&#39;, &#39;age&#39;: 22, &#39;grade&#39;: 92}, {&#39;name&#39;: &#39;Dave&#39;, &#39;age&#39;: 23, &#39;grade&#39;: 90}, {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;grade&#39;: 90}]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7a33\u5b9a\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>Python\u7684\u6392\u5e8f\u7b97\u6cd5\u662f\u7a33\u5b9a\u7684\uff0c\u8fd9\u610f\u5473\u7740\u5982\u679c\u4e24\u4e2a\u5143\u7d20\u7684\u6392\u5e8f\u952e\u76f8\u540c\uff0c\u5b83\u4eec\u5728\u6392\u5e8f\u540e\u7684\u5217\u8868\u4e2d\u7684\u76f8\u5bf9\u987a\u5e8f\u5c06\u4fdd\u6301\u4e0d\u53d8\u3002\u8fd9\u4e2a\u7279\u6027\u5728\u8fdb\u884c\u591a\u7ea7\u6392\u5e8f\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = [<\/p>\n<p>    {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 25, &quot;grade&quot;: 90},<\/p>\n<p>    {&quot;name&quot;: &quot;Jane&quot;, &quot;age&quot;: 22, &quot;grade&quot;: 85},<\/p>\n<p>    {&quot;name&quot;: &quot;Dave&quot;, &quot;age&quot;: 23, &quot;grade&quot;: 90},<\/p>\n<p>    {&quot;name&quot;: &quot;Lucy&quot;, &quot;age&quot;: 22, &quot;grade&quot;: 92}<\/p>\n<p>]<\/p>\n<h2><strong>\u5148\u6309grade\u6392\u5e8f\uff0c\u518d\u6309age\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_students = sorted(students, key=lambda student: student[&quot;grade&quot;])<\/p>\n<p>sorted_students = sorted(sorted_students, key=lambda student: student[&quot;age&quot;])<\/p>\n<p>print(sorted_students)<\/p>\n<h2><strong>\u8f93\u51fa: [{&#39;name&#39;: &#39;Jane&#39;, &#39;age&#39;: 22, &#39;grade&#39;: 85}, {&#39;name&#39;: &#39;Lucy&#39;, &#39;age&#39;: 22, &#39;grade&#39;: 92}, {&#39;name&#39;: &#39;Dave&#39;, &#39;age&#39;: 23, &#39;grade&#39;: 90}, {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;grade&#39;: 90}]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u66f4\u591a\u6392\u5e8f\u793a\u4f8b<\/h3>\n<\/p>\n<p><h4>\u6309\u5bf9\u8c61\u5c5e\u6027\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u5217\u8868\uff0c\u5e76\u4e14\u4f60\u60f3\u8981\u6309\u7167\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, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>students = [<\/p>\n<p>    Student(&quot;John&quot;, 25),<\/p>\n<p>    Student(&quot;Jane&quot;, 22),<\/p>\n<p>    Student(&quot;Dave&quot;, 23)<\/p>\n<p>]<\/p>\n<p>sorted_students = sorted(students, key=lambda student: student.age)<\/p>\n<p>for student in sorted_students:<\/p>\n<p>    print(student.name, student.age)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>Jane 22<\/strong><\/h2>\n<h2><strong>Dave 23<\/strong><\/h2>\n<h2><strong>John 25<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6309\u590d\u6570\u7684\u6a21\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u590d\u6570\u7684\u5217\u8868\uff0c\u5e76\u4e14\u4f60\u60f3\u8981\u6309\u7167\u5b83\u4eec\u7684\u6a21\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>abs()<\/code>\u51fd\u6570\u4f5c\u4e3a<code>key<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">complex_numbers = [3 + 4j, 1 - 1j, 0, 1 + 1j]<\/p>\n<p>sorted_complex_numbers = sorted(complex_numbers, key=abs)<\/p>\n<p>print(sorted_complex_numbers)  # \u8f93\u51fa: [0, (1-1j), (1+1j), (3+4j)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6392\u5e8f\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u6392\u5e8f\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u91cf\u56e0\u7d20\u3002Python\u7684<code>sorted()<\/code>\u51fd\u6570\u548c<code>list.sort()<\/code>\u65b9\u6cd5\u90fd\u4f7f\u7528\u4e86Timsort\u7b97\u6cd5\uff0c\u8fd9\u662f\u4e00\u79cd\u6df7\u5408\u7a33\u5b9a\u6392\u5e8f\u7b97\u6cd5\uff0c\u5177\u6709O(n log n)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>\u5927\u6570\u636e\u96c6\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\uff0c\u6392\u5e8f\u53ef\u80fd\u4f1a\u53d8\u5f97\u975e\u5e38\u8017\u65f6\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u751f\u6210\u5668\u6216\u8005\u5176\u4ed6\u4f18\u5316\u7b56\u7565\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>import time<\/p>\n<p>data = [random.randint(0, 1000000) for _ in range(1000000)]<\/p>\n<p>start_time = time.time()<\/p>\n<p>sorted_data = sorted(data)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;\u6392\u5e8f\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528sorted()\u51fd\u6570\u8fdb\u884c\u6392\u5e8f<\/strong>\u3001<strong>\u4f7f\u7528list.sort()\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f<\/strong>\u3001<strong>\u4f7f\u7528key\u53c2\u6570\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219<\/strong>\u3001<strong>\u4f7f\u7528lambda\u51fd\u6570\u7b80\u5316\u81ea\u5b9a\u4e49\u6392\u5e8f<\/strong>\u3001<strong>\u5b9e\u73b0\u964d\u5e8f\u6392\u5e8f<\/strong>\u3001<strong>\u8fdb\u884c\u591a\u7ea7\u590d\u6742\u6392\u5e8f<\/strong>\u3001<strong>\u5229\u7528\u7a33\u5b9a\u6392\u5e8f\u7279\u6027<\/strong>\u3001<strong>\u6309\u5bf9\u8c61\u5c5e\u6027\u6392\u5e8f<\/strong>\u3001<strong>\u6309\u590d\u6570\u6a21\u6392\u5e8f<\/strong>\u3001<strong>\u8003\u8651\u6392\u5e8f\u6027\u80fd<\/strong>\u3002\u8fd9\u4e9b\u90fd\u662fPython\u4e2d\u5e38\u89c1\u7684\u6392\u5e8f\u65b9\u6cd5\u548c\u6280\u5de7\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u89e3\u51b3\u5404\u79cd\u6392\u5e8f\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>Python\u7684\u6392\u5e8f\u51fd\u6570\u975e\u5e38\u7075\u6d3b\u548c\u5f3a\u5927\uff0c\u53ef\u4ee5\u6ee1\u8db3\u5404\u79cd\u590d\u6742\u7684\u6392\u5e8f\u9700\u6c42\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6570\u503c\u6392\u5e8f\uff0c\u8fd8\u662f\u590d\u6742\u7684\u5bf9\u8c61\u6392\u5e8f\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u548c\u7a33\u5b9a\u6392\u5e8f\u7279\u6027\u8f7b\u677e\u5b9e\u73b0\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u51fd\u6570\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u548c\u5217\u8868\u7684<code>sort()<\/code>\u65b9\u6cd5\u6765\u6392\u5e8f\u5217\u8868\u3002<code>sorted()<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5df2\u6392\u5e8f\u5217\u8868\uff0c\u800c<code>sort()<\/code>\u65b9\u6cd5\u5219\u5728\u539f\u5730\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u51fd\u6570\u7ed9<code>key<\/code>\u53c2\u6570\u6765\u5b9e\u73b0\u7279\u5b9a\u7684\u6392\u5e8f\u903b\u8f91\u3002\u4f8b\u5982\uff0c<code>sorted(my_list, key=lambda x: x[1])<\/code>\u5c06\u6839\u636e\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u7b2c\u4e8c\u4e2a\u503c\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p><strong>Python\u4e2d\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u7684\u4f7f\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u975e\u5e38\u9002\u5408\u5728\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u6392\u5e8f\u65f6\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u7684\u5217\u8868\uff0c\u5e76\u5e0c\u671b\u6839\u636e\u5b57\u5178\u4e2d\u7684\u67d0\u4e2a\u952e\u8fdb\u884c\u6392\u5e8f\uff0c\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u901a\u8fc7\u4f20\u9012\u4e00\u4e2alambda\u51fd\u6570\u6216\u5b9a\u4e49\u4e00\u4e2a\u72ec\u7acb\u7684\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u6392\u5e8f\u7684\u4f9d\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u7684\u51fd\u6570\u5bf9\u590d\u6742\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5bf9\u4e8e\u590d\u6742\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5d4c\u5957\u5217\u8868\u6216\u5b57\u5178\uff0c\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u7684\u5143\u7d20\u5e76\u8fd4\u56de\u7528\u4e8e\u6392\u5e8f\u7684\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u5b66\u751f\u4fe1\u606f\u7684\u5b57\u5178\u5217\u8868\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u6839\u636e\u5b66\u751f\u7684\u6210\u7ee9\u8fdb\u884c\u6392\u5e8f\uff0c\u4ece\u800c\u8f7b\u677e\u5f97\u5230\u6210\u7ee9\u4ece\u9ad8\u5230\u4f4e\u6216\u4ece\u4f4e\u5230\u9ad8\u7684\u987a\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3001Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684sorted()\u51fd\u6570\u8fdb\u884c\u6392\u5e8f\u3001sorte [&hellip;]","protected":false},"author":3,"featured_media":1149659,"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\/1149653"}],"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=1149653"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149653\/revisions"}],"predecessor-version":[{"id":1149660,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149653\/revisions\/1149660"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1149659"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1149653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1149653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1149653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}