{"id":1161145,"date":"2025-01-13T19:13:06","date_gmt":"2025-01-13T11:13:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1161145.html"},"modified":"2025-01-13T19:13:09","modified_gmt":"2025-01-13T11:13:09","slug":"python3%e5%a6%82%e4%bd%95%e6%8e%92%e5%88%97list","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1161145.html","title":{"rendered":"python3\u5982\u4f55\u6392\u5217list"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202459\/62eb40c0-03e9-4692-a023-e05a6c393d0f.webp\" alt=\"python3\u5982\u4f55\u6392\u5217list\" \/><\/p>\n<p><p> \u5728Python 3\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684<code>sort()<\/code>\u65b9\u6cd5\u3001<code>sorted()<\/code>\u51fd\u6570\u3001<code>reverse()<\/code>\u65b9\u6cd5\u7b49\u3002<strong>\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6309\u7167\u6307\u5b9a\u7684\u987a\u5e8f\u6392\u5217\u5217\u8868\u5143\u7d20\uff0c\u6bd4\u5982\u5347\u5e8f\u3001\u964d\u5e8f\u3001\u6309\u7279\u5b9a\u5173\u952e\u5b57\u6392\u5e8f<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5b83\u4eec\u7684\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>sort()<\/code>\u65b9\u6cd5\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u5217\u8868\uff0c\u6309\u7167\u6307\u5b9a\u7684\u987a\u5e8f\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b83\u4f1a\u6309\u7167\u5347\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>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>1.1\u3001\u964d\u5e8f\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5c06<code>reverse<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>True<\/code>\uff0c\u6765\u5b9e\u73b0\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>numbers.sort(reverse=True)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [9, 6, 5, 5, 2, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2\u3001\u6309\u5173\u952e\u5b57\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>key<\/code>\u53c2\u6570\uff0c\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u89c4\u5219\u3002\u4f8b\u5982\uff0c\u6309\u5b57\u7b26\u4e32\u957f\u5ea6\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>strings = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;]<\/p>\n<p>strings.sort(key=len)<\/p>\n<p>print(strings)  # \u8f93\u51fa: [&#39;date&#39;, &#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>sorted()<\/code>\u51fd\u6570\u4e0e<code>sort()<\/code>\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u5217\u8868\uff0c\u800c\u4e0d\u4f1a\u4fee\u6539\u539f\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>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>print(numbers)  # \u539f\u5217\u8868\u672a\u4fee\u6539: [5, 2, 9, 1, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.1\u3001\u964d\u5e8f\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06<code>reverse<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>True<\/code>\uff0c\u5b9e\u73b0\u964d\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>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><\/code><\/pre>\n<\/p>\n<p><h4>2.2\u3001\u6309\u5173\u952e\u5b57\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>key<\/code>\u53c2\u6570\uff0c\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u89c4\u5219\u3002\u4f8b\u5982\uff0c\u6309\u5b57\u7b26\u4e32\u957f\u5ea6\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>strings = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;]<\/p>\n<p>sorted_strings = sorted(strings, key=len)<\/p>\n<p>print(sorted_strings)  # \u8f93\u51fa: [&#39;date&#39;, &#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>reverse()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>reverse()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53cd\u8f6c\uff0c\u800c\u4e0d\u662f\u6392\u5e8f\u3002\u5982\u679c\u4f60\u9700\u8981\u7b80\u5355\u5730\u53cd\u8f6c\u5217\u8868\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>numbers.reverse()<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [6, 5, 1, 9, 2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6392\u5e8f\u81ea\u5b9a\u4e49\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0\u5bf9\u8c61\u7684<code>__lt__<\/code>\u65b9\u6cd5\uff08\u7528\u4e8e\u5c0f\u4e8e\u8fd0\u7b97\u7b26<code>&lt;<\/code>\uff09\uff0c\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u3002\u6216\u8005\uff0c\u901a\u8fc7<code>key<\/code>\u53c2\u6570\u4f20\u9012\u4e00\u4e2a\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u5b9e\u73b0<code>__lt__<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def __lt__(self, other):<\/p>\n<p>        return self.age &lt; other.age<\/p>\n<p>people = [Person(&quot;Alice&quot;, 30), Person(&quot;Bob&quot;, 25), Person(&quot;Charlie&quot;, 35)]<\/p>\n<p>people.sort()<\/p>\n<p>for person in people:<\/p>\n<p>    print(f&quot;{person.name}: {person.age}&quot;)<\/p>\n<h2><strong>\u8f93\u51fa: Bob: 25, Alice: 30, Charlie: 35<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2\u3001\u4f7f\u7528<code>key<\/code>\u53c2\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>people = [Person(&quot;Alice&quot;, 30), Person(&quot;Bob&quot;, 25), Person(&quot;Charlie&quot;, 35)]<\/p>\n<p>people.sort(key=lambda person: person.age)<\/p>\n<p>for person in people:<\/p>\n<p>    print(f&quot;{person.name}: {person.age}&quot;)<\/p>\n<h2><strong>\u8f93\u51fa: Bob: 25, Alice: 30, Charlie: 35<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6392\u5e8f\u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u5d4c\u5957\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u6309\u6bcf\u4e2a\u5b50\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>nested_list = [[1, 2], [3, 1], [5, 3]]<\/p>\n<p>nested_list.sort(key=lambda x: x[1])<\/p>\n<p>print(nested_list)  # \u8f93\u51fa: [[3, 1], [1, 2], [5, 3]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6392\u5e8f\u7a33\u5b9a\u6027<\/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\u76f8\u7b49\uff0c\u5b83\u4eec\u7684\u76f8\u5bf9\u987a\u5e8f\u5728\u6392\u5e8f\u540e\u4ecd\u7136\u4fdd\u6301\u4e0d\u53d8\u3002\u8fd9\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>data = [(&#39;apple&#39;, 2), (&#39;banana&#39;, 3), (&#39;cherry&#39;, 1), (&#39;banana&#39;, 1)]<\/p>\n<p>data.sort(key=lambda x: x[1])<\/p>\n<p>print(data)  # \u8f93\u51fa: [(&#39;cherry&#39;, 1), (&#39;banana&#39;, 1), (&#39;apple&#39;, 2), (&#39;banana&#39;, 3)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6027\u80fd\u8003\u8651<\/h3>\n<\/p>\n<p><p>Python \u7684\u5185\u7f6e\u6392\u5e8f\u7b97\u6cd5\u662f Timsort\uff0c\u5177\u6709 O(n log n) \u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\uff0c\u8fd9\u5df2\u7ecf\u8db3\u591f\u9ad8\u6548\u3002\u4f46\u662f\uff0c\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u8003\u8651\u4f18\u5316\u6392\u5e8f\u7b97\u6cd5\u6216\u8005\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u6392\u5e8f\u7684\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>8.1\u3001\u4f7f\u7528<code>heapq<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>heapq<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5806\u961f\u5217\u7b97\u6cd5\uff0c\u53ef\u7528\u4e8e\u5b9e\u73b0\u4f18\u5148\u961f\u5217\u3002\u867d\u7136\u5b83\u4e0d\u662f\u76f4\u63a5\u7684\u6392\u5e8f\u65b9\u6cd5\uff0c\u4f46\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u6700\u5c0f\u6216\u6700\u5927\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import heapq<\/p>\n<p>numbers = [5, 2, 9, 1, 5, 6]<\/p>\n<p>heapq.heapify(numbers)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 5, 5, 6, 9]<\/p>\n<p>print(heapq.heappop(numbers))  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>8.2\u3001\u4f7f\u7528<code>numpy<\/code>\u5e93<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u503c\u6570\u636e\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u6392\u5e8f\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import numpy as np<\/p>\n<p>numbers = np.array([5, 2, 9, 1, 5, 6])<\/p>\n<p>sorted_numbers = np.sort(numbers)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [1 2 5 5 6 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python 3\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002<strong>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd<\/strong>\u3002\u4e3b\u8981\u7684\u65b9\u6cd5\u5305\u62ec<code>sort()<\/code>\u65b9\u6cd5\u3001<code>sorted()<\/code>\u51fd\u6570\u3001<code>reverse()<\/code>\u65b9\u6cd5\u7b49\u3002\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u5bf9\u8c61\u548c\u590d\u6742\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528<code>key<\/code>\u53c2\u6570\u6216\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u65b9\u6cd5\u3002\u5bf9\u4e8e\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>heapq<\/code>\u6a21\u5757\u6216<code>numpy<\/code>\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u8fd9\u4e9b\u65b9\u6cd5\u7684\u719f\u7ec3\u638c\u63e1\uff0c\u53ef\u4ee5\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u6392\u5e8f\u9700\u6c42\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u8fd0\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python3\u4e2d\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sort()<\/code>\u65b9\u6cd5\u6216<code>sorted()<\/code>\u51fd\u6570\u6765\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002<code>sort()<\/code>\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u5217\u8868\uff0c\u800c<code>sorted()<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u5217\u8868\u3002\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5\u65f6\uff0c\u60a8\u53ef\u4ee5\u8c03\u7528<code>list.sort()<\/code>\uff0c\u4f8b\u5982\uff1a<code>my_list.sort()<\/code>. \u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u65f6\uff0c\u60a8\u53ef\u4ee5\u50cf\u8fd9\u6837\uff1a<code>new_list = sorted(my_list)<\/code>\u3002\u4e24\u8005\u90fd\u652f\u6301<code>reverse<\/code>\u53c2\u6570\u6765\u63a7\u5236\u6392\u5e8f\u987a\u5e8f\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\uff1f<\/strong><br \/>Python3\u5141\u8bb8\u60a8\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a<code>key<\/code>\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u3002\u60a8\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f1a\u5728\u6392\u5e8f\u65f6\u7528\u4e8e\u8ba1\u7b97\u6bcf\u4e2a\u5143\u7d20\u7684\u6392\u5e8f\u5173\u952e\u5b57\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\uff0c\u5e76\u5e0c\u671b\u6309\u5143\u7ec4\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6392\u5e8f\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>my_list.sort(key=lambda x: x[1])<\/code>\u3002\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97\u60a8\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u590d\u6742\u7684\u6392\u5e8f\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u5bf9\u5305\u542b\u5bf9\u8c61\u7684\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5bf9\u8c61\u7684\u5c5e\u6027\u4f5c\u4e3a\u6392\u5e8f\u4f9d\u636e\u3002\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u7c7b<code>Person<\/code>\uff0c\u5b83\u6709\u4e00\u4e2a\u5c5e\u6027<code>age<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u65b9\u6cd5\u5bf9<code>Person<\/code>\u5bf9\u8c61\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff1a<code>people.sort(key=lambda person: person.age)<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u8ba9\u60a8\u53ef\u4ee5\u6839\u636e\u5bf9\u8c61\u7684\u4efb\u610f\u5c5e\u6027\u8fdb\u884c\u6392\u5e8f\uff0c\u6781\u5927\u5730\u589e\u5f3a\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python 3\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684sort()\u65b9\u6cd5\u3001sorted()\u51fd\u6570\u3001r [&hellip;]","protected":false},"author":3,"featured_media":1161153,"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\/1161145"}],"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=1161145"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161145\/revisions"}],"predecessor-version":[{"id":1161159,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161145\/revisions\/1161159"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1161153"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1161145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1161145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1161145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}