{"id":1111568,"date":"2025-01-08T17:30:32","date_gmt":"2025-01-08T09:30:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1111568.html"},"modified":"2025-01-08T17:30:34","modified_gmt":"2025-01-08T09:30:34","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%87%aa%e5%ae%9a%e4%b9%89%e6%8e%92%e5%ba%8f%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1111568.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u81ea\u5b9a\u4e49\u6392\u5e8f\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073951\/beb5edbe-3aec-4345-9e57-aa5e352ddc71.webp\" alt=\"python\u4e2d\u5982\u4f55\u81ea\u5b9a\u4e49\u6392\u5e8f\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u81ea\u5b9a\u4e49\u6392\u5e8f\u5217\u8868\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u7684<code>sort()<\/code>\u65b9\u6cd5\u3001\u901a\u8fc7\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u51fd\u6570\u3001\u5229\u7528<code>key<\/code>\u53c2\u6570\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u6392\u5e8f\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u51fd\u6570<code>sorted()<\/code>\u53ef\u4ee5\u5bf9\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u8981\u8fdb\u884c\u81ea\u5b9a\u4e49\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>key<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u6839\u636e\u5b57\u7b26\u4e32\u957f\u5ea6\u6392\u5e8f<\/p>\n<p>data = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;]<\/p>\n<p>sorted_data = sorted(data, key=len)<\/p>\n<p>print(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>sorted()<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a<code>key<\/code>\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u4ece\u6bcf\u4e2a\u5143\u7d20\u4e2d\u63d0\u53d6\u6392\u5e8f\u952e\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>len<\/code>\u51fd\u6570\u88ab\u7528\u4f5c<code>key<\/code>\uff0c\u56e0\u6b64\u5217\u8868\u5c06\u6839\u636e\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u7684<code>sort()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4e0e<code>sorted()<\/code>\u51fd\u6570\u7c7b\u4f3c\uff0c\u5217\u8868\u7684<code>sort()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u4f46\u5b83\u662f\u5c31\u5730\u6392\u5e8f\uff0c\u4e0d\u4f1a\u8fd4\u56de\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u6839\u636e\u5b57\u7b26\u4e32\u957f\u5ea6\u6392\u5e8f<\/p>\n<p>data = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;]<\/p>\n<p>data.sort(key=len)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>sort()<\/code>\u65b9\u6cd5\u7684\u7528\u6cd5\u4e0e<code>sorted()<\/code>\u51fd\u6570\u7c7b\u4f3c\uff0c\u540c\u6837\u63a5\u53d7\u4e00\u4e2a<code>key<\/code>\u53c2\u6570\u3002\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e<code>sort()<\/code>\u65b9\u6cd5\u4f1a\u4fee\u6539\u539f\u5217\u8868\uff0c\u800c<code>sorted()<\/code>\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python 2\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u51fd\u6570\u6765\u6392\u5e8f\u5217\u8868\uff0c\u4f46\u5728Python 3\u4e2d\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5df2\u88ab\u5e9f\u5f03\uff0c\u63a8\u8350\u4f7f\u7528<code>key<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u6839\u636e\u5b57\u7b26\u4e32\u957f\u5ea6\u6392\u5e8f\uff08Python 2\uff09<\/p>\n<p>data = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;]<\/p>\n<p>data.sort(cmp=lambda x, y: cmp(len(x), len(y)))<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python 3\u4e2d\uff0c<code>cmp<\/code>\u53c2\u6570\u88ab\u79fb\u9664\uff0c\u5efa\u8bae\u4f7f\u7528<code>key<\/code>\u53c2\u6570\u6765\u8fdb\u884c\u81ea\u5b9a\u4e49\u6392\u5e8f\u3002\u5bf9\u4e8e\u8001\u7248\u672c\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528<code>functools.cmp_to_key<\/code>\u5c06\u6bd4\u8f83\u51fd\u6570\u8f6c\u6362\u4e3a\u952e\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5229\u7528<code>key<\/code>\u53c2\u6570\u8fdb\u884c\u590d\u6742\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd9\u65f6\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e00\u4e2a\u63d0\u53d6\u6392\u5e8f\u952e\u7684\u51fd\u6570\uff0c\u6765\u5b9e\u73b0\u590d\u6742\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u9996\u5148\u6839\u636e\u5b57\u7b26\u4e32\u957f\u5ea6\u6392\u5e8f\uff0c\u5982\u679c\u957f\u5ea6\u76f8\u540c\u5219\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f<\/p>\n<p>data = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;, &quot;fig&quot;, &quot;grape&quot;]<\/p>\n<p>sorted_data = sorted(data, key=lambda s: (len(s), s))<\/p>\n<p>print(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>key<\/code>\u53c2\u6570\u4f7f\u7528\u4e00\u4e2alambda\u51fd\u6570\uff0c\u63d0\u53d6\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5143\u7d20\u7684\u5143\u7ec4\u4f5c\u4e3a\u6392\u5e8f\u952e\u3002\u9996\u5148\uff0c\u6839\u636e\u5b57\u7b26\u4e32\u957f\u5ea6\u6392\u5e8f\uff1b\u5982\u679c\u957f\u5ea6\u76f8\u540c\uff0c\u5219\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u4efb\u610f\u590d\u6742\u7684\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u81ea\u5b9a\u4e49\u5bf9\u8c61\u548c\u5c5e\u6027\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5bf9\u5305\u542b\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>key<\/code>\u53c2\u6570\u7ed3\u5408\u5bf9\u8c61\u7684\u5c5e\u6027\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u6839\u636e\u5bf9\u8c61\u7684\u5c5e\u6027\u6392\u5e8f<\/p>\n<p>class Fruit:<\/p>\n<p>    def __init__(self, name, price):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.price = price<\/p>\n<p>fruits = [Fruit(&quot;apple&quot;, 10), Fruit(&quot;banana&quot;, 5), Fruit(&quot;cherry&quot;, 20)]<\/p>\n<p>sorted_fruits = sorted(fruits, key=lambda fruit: fruit.price)<\/p>\n<p>for fruit in sorted_fruits:<\/p>\n<p>    print(fruit.name, fruit.price)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Fruit<\/code>\u7c7b\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u591a\u4e2a<code>Fruit<\/code>\u5bf9\u8c61\u7684\u5217\u8868\u3002\u901a\u8fc7<code>sorted()<\/code>\u51fd\u6570\u548c<code>key<\/code>\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e<code>Fruit<\/code>\u5bf9\u8c61\u7684<code>price<\/code>\u5c5e\u6027\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u5408\u64cd\u4f5c\u7b26\u6a21\u5757\u8fdb\u884c\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>Python\u7684<code>operator<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5e38\u7528\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u7b80\u5316\u81ea\u5b9a\u4e49\u6392\u5e8f\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u6839\u636e\u5bf9\u8c61\u7684\u5c5e\u6027\u6392\u5e8f<\/p>\n<p>from operator import attrgetter<\/p>\n<p>class Fruit:<\/p>\n<p>    def __init__(self, name, price):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.price = price<\/p>\n<p>fruits = [Fruit(&quot;apple&quot;, 10), Fruit(&quot;banana&quot;, 5), Fruit(&quot;cherry&quot;, 20)]<\/p>\n<p>sorted_fruits = sorted(fruits, key=attrgetter(&#39;price&#39;))<\/p>\n<p>for fruit in sorted_fruits:<\/p>\n<p>    print(fruit.name, fruit.price)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>operator.attrgetter<\/code>\u4ece\u6bcf\u4e2a<code>Fruit<\/code>\u5bf9\u8c61\u4e2d\u63d0\u53d6<code>price<\/code>\u5c5e\u6027\uff0c\u4f5c\u4e3a\u6392\u5e8f\u952e\u3002\u8fd9\u6837\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7ed3\u5408<code>reverse<\/code>\u53c2\u6570\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u8fd8\u662f<code>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\"># \u793a\u4f8b\uff1a\u6839\u636e\u5b57\u7b26\u4e32\u957f\u5ea6\u964d\u5e8f\u6392\u5e8f<\/p>\n<p>data = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;]<\/p>\n<p>sorted_data = sorted(data, key=len, reverse=True)<\/p>\n<p>print(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u5c06<code>reverse<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>True<\/code>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6309\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u964d\u5e8f\u6392\u5e8f\u3002<code>reverse<\/code>\u53c2\u6570\u5728\u9700\u8981\u964d\u5e8f\u6392\u5e8f\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u7ed3\u5408\u590d\u5408\u952e\u8fdb\u884c\u591a\u6761\u4ef6\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u4e14\u6bcf\u4e2a\u6761\u4ef6\u7684\u91cd\u8981\u6027\u4e0d\u540c\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u590d\u5408\u952e\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u6392\u5e8f<\/p>\n<p>data = [(&quot;apple&quot;, 10), (&quot;banana&quot;, 5), (&quot;cherry&quot;, 20), (&quot;date&quot;, 10)]<\/p>\n<p>sorted_data = sorted(data, key=lambda x: (x[1], x[0]))<\/p>\n<p>print(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5bf9\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u9996\u5148\u6839\u636e\u5143\u7ec4\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff08\u6570\u5b57\uff09\u6392\u5e8f\uff0c\u5982\u679c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u76f8\u540c\uff0c\u5219\u6839\u636e\u7b2c\u4e00\u4e2a\u5143\u7d20\uff08\u5b57\u7b26\u4e32\uff09\u6392\u5e8f\u3002\u901a\u8fc7\u4f7f\u7528\u590d\u5408\u952e\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u6761\u4ef6\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u7ed3\u5408<code>functools<\/code>\u6a21\u5757\u8fdb\u884c\u81ea\u5b9a\u4e49\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5b9a\u4e49\u66f4\u590d\u6742\u7684\u6392\u5e8f\u903b\u8f91\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>functools.cmp_to_key<\/code>\u5c06\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u51fd\u6570\u8f6c\u6362\u4e3a\u952e\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u4f7f\u7528\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>from functools import cmp_to_key<\/p>\n<p>data = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;]<\/p>\n<p>def compare_length_then_alphabetically(a, b):<\/p>\n<p>    if len(a) == len(b):<\/p>\n<p>        return (a &gt; b) - (a &lt; b)<\/p>\n<p>    return len(a) - len(b)<\/p>\n<p>sorted_data = sorted(data, key=cmp_to_key(compare_length_then_alphabetically))<\/p>\n<p>print(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6bd4\u8f83\u51fd\u6570<code>compare_length_then_alphabetically<\/code>\uff0c\u9996\u5148\u6bd4\u8f83\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5982\u679c\u957f\u5ea6\u76f8\u540c\uff0c\u5219\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\u3002\u901a\u8fc7<code>functools.cmp_to_key<\/code>\uff0c\u6211\u4eec\u5c06\u8fd9\u4e2a\u6bd4\u8f83\u51fd\u6570\u8f6c\u6362\u4e3a\u952e\u51fd\u6570\uff0c\u7528\u4e8e<code>sorted()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u5916\u90e8\u5e93\u8fdb\u884c\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5916\u90e8\u5e93\u6765\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6392\u5e8f\u3002\u4f8b\u5982\uff0c<code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6392\u5e8f\u529f\u80fd\uff0c\u53ef\u4ee5\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u548c\u591a\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u4f7f\u7528numpy\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>import numpy as np<\/p>\n<p>data = np.array([(&quot;apple&quot;, 10), (&quot;banana&quot;, 5), (&quot;cherry&quot;, 20), (&quot;date&quot;, 10)],<\/p>\n<p>                dtype=[(&#39;name&#39;, &#39;U10&#39;), (&#39;price&#39;, &#39;i4&#39;)])<\/p>\n<p>sorted_data = np.sort(data, order=[&#39;price&#39;, &#39;name&#39;])<\/p>\n<p>print(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>numpy<\/code>\u5e93\u5bf9\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u901a\u8fc7\u6307\u5b9a\u6392\u5e8f\u987a\u5e8f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u6761\u4ef6\u6392\u5e8f\u3002<code>numpy<\/code>\u5e93\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u975e\u5e38\u9ad8\u6548\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u5404\u79cd\u81ea\u5b9a\u4e49\u6392\u5e8f\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5355\u6761\u4ef6\u6392\u5e8f\uff0c\u8fd8\u662f\u590d\u6742\u7684\u591a\u6761\u4ef6\u6392\u5e8f\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u3002\u901a\u8fc7\u7075\u6d3b\u4f7f\u7528<code>key<\/code>\u53c2\u6570\u3001\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570\u4ee5\u53ca\u5916\u90e8\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u6ee1\u8db3\u5404\u79cd\u6392\u5e8f\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u4e4b\uff0c\u5728Python\u4e2d\u81ea\u5b9a\u4e49\u6392\u5e8f\u5217\u8868\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u3001\u5217\u8868\u7684<code>sort()<\/code>\u65b9\u6cd5\u3001\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u51fd\u6570\u3001\u5229\u7528<code>key<\/code>\u53c2\u6570\u3001\u7ed3\u5408\u64cd\u4f5c\u7b26\u6a21\u5757\u3001\u7ed3\u5408<code>reverse<\/code>\u53c2\u6570\u8fdb\u884c\u964d\u5e8f\u6392\u5e8f\u3001\u7ed3\u5408\u590d\u5408\u952e\u8fdb\u884c\u591a\u6761\u4ef6\u6392\u5e8f\u3001\u4f7f\u7528<code>functools<\/code>\u6a21\u5757\u8fdb\u884c\u81ea\u5b9a\u4e49\u6392\u5e8f\u4ee5\u53ca\u4f7f\u7528\u5916\u90e8\u5e93\u8fdb\u884c\u6392\u5e8f\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u7684\u6392\u5e8f\u9700\u6c42\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u81ea\u5b9a\u4e49\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\u6765\u8fdb\u884c\u81ea\u5b9a\u4e49\u6392\u5e8f\u3002\u901a\u8fc7\u63d0\u4f9b\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u51fd\u6570\uff08\u901a\u5e38\u79f0\u4e3a\u201ckey\u201d\u51fd\u6570\uff09\uff0c\u60a8\u53ef\u4ee5\u6307\u5b9a\u6392\u5e8f\u7684\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u5217\u8868\u4e2d\u5bf9\u8c61\u7684\u67d0\u4e2a\u5c5e\u6027\u6216\u7279\u5b9a\u7684\u8ba1\u7b97\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7684\u81ea\u5b9a\u4e49\u6392\u5e8f\u793a\u4f8b\u5417\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5b57\u5178\u4ee3\u8868\u4e00\u4e2a\u5b66\u751f\uff0c\u5305\u542b\u59d3\u540d\u548c\u6210\u7ee9\u3002\u60a8\u53ef\u4ee5\u6309\u6210\u7ee9\u4ece\u9ad8\u5230\u4f4e\u6392\u5e8f\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-python\">students = [{&#39;name&#39;: &#39;Alice&#39;, &#39;score&#39;: 85}, {&#39;name&#39;: &#39;Bob&#39;, &#39;score&#39;: 90}, {&#39;name&#39;: &#39;Charlie&#39;, &#39;score&#39;: 80}]\nsorted_students = sorted(students, key=lambda x: x[&#39;score&#39;], reverse=True)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>key=lambda x: x[&#39;score&#39;]<\/code>\u6307\u5b9a\u4e86\u6392\u5e8f\u7684\u4f9d\u636e\uff0c\u800c<code>reverse=True<\/code>\u4f7f\u5f97\u6392\u5e8f\u7ed3\u679c\u4e3a\u964d\u5e8f\u3002<\/p>\n<p><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u65f6\u6709\u4ec0\u4e48\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u65f6\uff0c\u786e\u4fdd\u6240\u4f7f\u7528\u7684\u201ckey\u201d\u51fd\u6570\u80fd\u591f\u5904\u7406\u6240\u6709\u53ef\u80fd\u7684\u8f93\u5165\u7c7b\u578b\u3002\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u4e0d\u53ef\u6bd4\u8f83\u7684\u5143\u7d20\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u6b64\u5916\uff0c\u8003\u8651\u5230\u6027\u80fd\uff0c\u5c3d\u91cf\u7b80\u5316\u201ckey\u201d\u51fd\u6570\u7684\u903b\u8f91\uff0c\u4ee5\u63d0\u9ad8\u6392\u5e8f\u6548\u7387\u3002<\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u6392\u5e8f\u662f\u5426\u652f\u6301\u591a\u91cd\u6392\u5e8f\u6761\u4ef6\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u201ckey\u201d\u51fd\u6570\u4e2d\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\u6765\u5b9e\u73b0\u591a\u91cd\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u5e0c\u671b\u6309\u6210\u7ee9\u6392\u5e8f\uff0c\u4f46\u5728\u6210\u7ee9\u76f8\u540c\u7684\u60c5\u51b5\u4e0b\u6309\u540d\u5b57\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-python\">sorted_students = sorted(students, key=lambda x: (x[&#39;score&#39;], x[&#39;name&#39;]))\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5143\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u6210\u7ee9\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u662f\u540d\u5b57\uff0cPython\u4f1a\u4f9d\u6b21\u6bd4\u8f83\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u81ea\u5b9a\u4e49\u6392\u5e8f\u5217\u8868\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528sorted()\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u7684sort()\u65b9\u6cd5\u3001\u901a\u8fc7\u5b9a\u4e49\u81ea\u5b9a\u4e49 [&hellip;]","protected":false},"author":3,"featured_media":1111578,"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\/1111568"}],"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=1111568"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111568\/revisions"}],"predecessor-version":[{"id":1111581,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111568\/revisions\/1111581"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1111578"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1111568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1111568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1111568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}