{"id":1108531,"date":"2025-01-08T17:01:09","date_gmt":"2025-01-08T09:01:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108531.html"},"modified":"2025-01-08T17:01:11","modified_gmt":"2025-01-08T09:01:11","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e8%8b%b1%e6%96%87%e5%ad%97%e6%af%8d%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108531.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u82f1\u6587\u5b57\u6bcd\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072137\/5f345d69-0377-496b-afca-a5dc894b97da.webp\" alt=\"python\u5982\u4f55\u7ed9\u82f1\u6587\u5b57\u6bcd\u6392\u5e8f\" \/><\/p>\n<p><p> <strong>\u7ed9\u82f1\u6587\u5b57\u6bcd\u6392\u5e8f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Python\u5185\u7f6e\u7684sorted\u51fd\u6570\u3001list\u7684sort\u65b9\u6cd5\u3001\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u4e14\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u5185\u7f6e\u7684sorted\u51fd\u6570\u3002\u4e0b\u9762\u6211\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528sorted\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u7684sorted\u51fd\u6570\u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u6240\u6709\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u540e\u7684\u5217\u8868\u3002\u8be5\u51fd\u6570\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u6765\u6307\u5b9a\u6392\u5e8f\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>letters = [&#39;b&#39;, &#39;d&#39;, &#39;a&#39;, &#39;c&#39;, &#39;e&#39;]<\/p>\n<p>sorted_letters = sorted(letters)<\/p>\n<p>print(sorted_letters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>sorted<\/code>\u51fd\u6570\u5bf9\u5217\u8868<code>letters<\/code>\u8fdb\u884c\u4e86\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u540e\u7684\u5217\u8868<code>sorted_letters<\/code>\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>sorted<\/code>\u51fd\u6570\u6309\u7167\u5b57\u6bcd\u7684ASCII\u7801\u8fdb\u884c\u6392\u5e8f\uff0c\u5373\u5b57\u6bcd\u4ece\u5c0f\u5230\u5927\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u8bf4\u660e\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>sorted\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5<\/strong>\uff1a<code>sorted(iterable, key=None, reverse=False)<\/code>\uff0c\u5176\u4e2d<code>iterable<\/code>\u662f\u4f60\u8981\u6392\u5e8f\u7684\u5bf9\u8c61\uff0c<code>key<\/code>\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u6765\u63d0\u53d6\u6392\u5e8f\u7684\u5173\u952e\u5b57\uff0c<code>reverse<\/code>\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u9ed8\u8ba4\u4e3aFalse\uff0c\u8868\u793a\u5347\u5e8f\u6392\u5e8f\uff1b\u5982\u679c\u4e3aTrue\uff0c\u5219\u8868\u793a\u964d\u5e8f\u6392\u5e8f\u3002<\/li>\n<li><strong>key\u53c2\u6570\u7684\u4f7f\u7528<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6309\u7167\u81ea\u5b9a\u4e49\u89c4\u5219\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>key<\/code>\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u6309\u7167\u5b57\u6bcd\u7684\u957f\u5ea6\u8fdb\u884c\u6392\u5e8f\uff0c\u6216\u8005\u5ffd\u7565\u5927\u5c0f\u5199\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u6309\u7167\u5b57\u6bcd\u7684\u957f\u5ea6\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>letters = [&#39;beta&#39;, &#39;alpha&#39;, &#39;gamma&#39;, &#39;delta&#39;]<\/p>\n<p>sorted_letters = sorted(letters, key=len)<\/p>\n<p>print(sorted_letters)<\/p>\n<h2><strong>\u5ffd\u7565\u5927\u5c0f\u5199\u8fdb\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>letters = [&#39;Beta&#39;, &#39;alpha&#39;, &#39;Gamma&#39;, &#39;delta&#39;]<\/p>\n<p>sorted_letters = sorted(letters, key=str.lower)<\/p>\n<p>print(sorted_letters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528list\u7684sort\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>list\u7684sort\u65b9\u6cd5\u662f\u4e00\u4e2a\u5c31\u5730\u6392\u5e8f\u65b9\u6cd5\uff0c\u5b83\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u5217\u8868\uff0c\u800c\u4e0d\u662f\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>letters = [&#39;b&#39;, &#39;d&#39;, &#39;a&#39;, &#39;c&#39;, &#39;e&#39;]<\/p>\n<p>letters.sort()<\/p>\n<p>print(letters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>letters.sort()<\/code>\u5bf9\u5217\u8868<code>letters<\/code>\u8fdb\u884c\u4e86\u6392\u5e8f\uff0c\u5e76\u76f4\u63a5\u4fee\u6539\u4e86\u539f\u5217\u8868\u3002\u4e0e<code>sorted<\/code>\u51fd\u6570\u7c7b\u4f3c\uff0c<code>sort<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u6765\u6307\u5b9a\u6392\u5e8f\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u8bf4\u660e\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>sort\u65b9\u6cd5\u7684\u57fa\u672c\u7528\u6cd5<\/strong>\uff1a<code>list.sort(key=None, reverse=False)<\/code>\uff0c\u4e0e<code>sorted<\/code>\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u4f3c\u3002<\/li>\n<li><strong>key\u53c2\u6570\u7684\u4f7f\u7528<\/strong>\uff1a\u4e0e<code>sorted<\/code>\u51fd\u6570\u7684<code>key<\/code>\u53c2\u6570\u76f8\u540c\uff0c\u53ef\u4ee5\u7528\u4e8e\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u6309\u7167\u5b57\u6bcd\u7684\u957f\u5ea6\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>letters = [&#39;beta&#39;, &#39;alpha&#39;, &#39;gamma&#39;, &#39;delta&#39;]<\/p>\n<p>letters.sort(key=len)<\/p>\n<p>print(letters)<\/p>\n<h2><strong>\u5ffd\u7565\u5927\u5c0f\u5199\u8fdb\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>letters = [&#39;Beta&#39;, &#39;alpha&#39;, &#39;Gamma&#39;, &#39;delta&#39;]<\/p>\n<p>letters.sort(key=str.lower)<\/p>\n<p>print(letters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u7684\u6392\u5e8f\u9700\u6c42\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5b9a\u4e49\u81ea\u5df1\u7684\u6392\u5e8f\u89c4\u5219\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570<code>sorted<\/code>\u6216\u8005<code>list<\/code>\u7684<code>sort<\/code>\u65b9\u6cd5\u7684<code>key<\/code>\u53c2\u6570\u6765\u4f20\u9012\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>def custom_sort(letter):<\/p>\n<p>    return ord(letter)<\/p>\n<p>letters = [&#39;b&#39;, &#39;d&#39;, &#39;a&#39;, &#39;c&#39;, &#39;e&#39;]<\/p>\n<p>sorted_letters = sorted(letters, key=custom_sort)<\/p>\n<p>print(sorted_letters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<code>custom_sort<\/code>\uff0c\u5b83\u8fd4\u56de\u5b57\u6bcd\u7684ASCII\u7801\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u8be5\u51fd\u6570\u4f20\u9012\u7ed9<code>sorted<\/code>\u51fd\u6570\u7684<code>key<\/code>\u53c2\u6570\uff0c\u8fdb\u884c\u81ea\u5b9a\u4e49\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u8bf4\u660e\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u7684\u5b9a\u4e49<\/strong>\uff1a\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u5e94\u8be5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u7528\u4e8e\u6392\u5e8f\u7684\u503c\u3002<\/li>\n<li><strong>\u4f20\u9012\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/strong>\uff1a\u5c06\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u4f20\u9012\u7ed9<code>sorted<\/code>\u51fd\u6570\u6216\u8005<code>list<\/code>\u7684<code>sort<\/code>\u65b9\u6cd5\u7684<code>key<\/code>\u53c2\u6570\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u6309\u7167\u5b57\u6bcd\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>def custom_sort(word):<\/p>\n<p>    return word[-1]<\/p>\n<p>words = [&#39;beta&#39;, &#39;alpha&#39;, &#39;gamma&#39;, &#39;delta&#39;]<\/p>\n<p>sorted_words = sorted(words, key=custom_sort)<\/p>\n<p>print(sorted_words)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528lambda\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002lambda\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u533f\u540d\u51fd\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u7b80\u5355\u7684\u6392\u5e8f\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>letters = [&#39;b&#39;, &#39;d&#39;, &#39;a&#39;, &#39;c&#39;, &#39;e&#39;]<\/p>\n<p>sorted_letters = sorted(letters, key=lambda x: ord(x))<\/p>\n<p>print(sorted_letters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86lambda\u8868\u8fbe\u5f0f<code>lambda x: ord(x)<\/code>\u6765\u4ee3\u66ff\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u8bf4\u660e\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>lambda\u8868\u8fbe\u5f0f\u7684\u5b9a\u4e49<\/strong>\uff1a<code>lambda<\/code>\u5173\u952e\u5b57\u540e\u9762\u8ddf\u53c2\u6570\u5217\u8868\u548c\u5192\u53f7\uff0c\u5192\u53f7\u540e\u9762\u662f\u8868\u8fbe\u5f0f\u3002lambda\u8868\u8fbe\u5f0f\u7684\u8fd4\u56de\u503c\u662f\u8868\u8fbe\u5f0f\u7684\u8ba1\u7b97\u7ed3\u679c\u3002<\/li>\n<li><strong>\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u8fdb\u884c\u6392\u5e8f<\/strong>\uff1a\u5c06lambda\u8868\u8fbe\u5f0f\u4f20\u9012\u7ed9<code>sorted<\/code>\u51fd\u6570\u6216\u8005<code>list<\/code>\u7684<code>sort<\/code>\u65b9\u6cd5\u7684<code>key<\/code>\u53c2\u6570\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u6309\u7167\u5b57\u6bcd\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>words = [&#39;beta&#39;, &#39;alpha&#39;, &#39;gamma&#39;, &#39;delta&#39;]<\/p>\n<p>sorted_words = sorted(words, key=lambda x: x[-1])<\/p>\n<p>print(sorted_words)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u82f1\u6587\u5b57\u6bcd\u8fdb\u884c\u6392\u5e8f\u3002<strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570<code>sorted<\/code>\u548c<code>list<\/code>\u7684<code>sort<\/code>\u65b9\u6cd5<\/strong>\uff0c\u5b83\u4eec\u7b80\u5355\u6613\u7528\u4e14\u529f\u80fd\u5f3a\u5927\u3002\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u6392\u5e8f\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u6216\u8005lambda\u8868\u8fbe\u5f0f\u3002\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p><strong>\u6838\u5fc3\u603b\u7ed3\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528sorted\u51fd\u6570<\/strong>\uff1a\u7b80\u5355\u6613\u7528\uff0c\u8fd4\u56de\u65b0\u7684\u6392\u5e8f\u540e\u7684\u5217\u8868\u3002<\/li>\n<li><strong>\u4f7f\u7528list\u7684sort\u65b9\u6cd5<\/strong>\uff1a\u5c31\u5730\u6392\u5e8f\uff0c\u4fee\u6539\u539f\u5217\u8868\u3002<\/li>\n<li><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/strong>\uff1a\u9002\u7528\u4e8e\u590d\u6742\u7684\u6392\u5e8f\u9700\u6c42\u3002<\/li>\n<li><strong>\u4f7f\u7528lambda\u8868\u8fbe\u5f0f<\/strong>\uff1a\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6392\u5e8f\u89c4\u5219\u3002<\/li>\n<\/ol>\n<p><p>\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u4e86\u89e3\u5176\u539f\u7406\u548c\u4f7f\u7528\u573a\u666f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u4f60\u80fd\u591f\u66f4\u597d\u5730\u638c\u63e1Python\u4e2d\u5bf9\u82f1\u6587\u5b57\u6bcd\u6392\u5e8f\u7684\u65b9\u6cd5\uff0c\u5e76\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u5b57\u6bcd\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u6216<code>sort()<\/code>\u65b9\u6cd5\u6765\u5bf9\u5b57\u6bcd\u8fdb\u884c\u6392\u5e8f\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\u4f1a\u5bf9\u539f\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-python\">letters = [&#39;d&#39;, &#39;a&#39;, &#39;c&#39;, &#39;b&#39;]\nsorted_letters = sorted(letters)  # \u8fd4\u56de [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]\n<\/code><\/pre>\n<p>\u6216\u8005\u5982\u679c\u662f\u5217\u8868\uff1a<\/p>\n<pre><code class=\"language-python\">letters.sort()  # \u539f\u5217\u8868\u88ab\u6392\u5e8f\u4e3a [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u6bcd\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u60f3\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u6bcd\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u5148\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u4f7f\u7528<code>sorted()<\/code>\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u518d\u5c06\u5176\u5408\u5e76\u56de\u5b57\u7b26\u4e32\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">string = &quot;hello&quot;\nsorted_string = &#39;&#39;.join(sorted(string))  # \u8fd4\u56de &#39;ehllo&#39;\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5bf9\u5b57\u7b26\u4e32\u4e2d\u5b57\u6bcd\u7684\u6392\u5e8f\u3002<\/p>\n<p><strong>\u5728\u6392\u5e8f\u65f6\u5982\u4f55\u5904\u7406\u5927\u5c0f\u5199\u5b57\u6bcd\uff1f<\/strong><br \/>\u5728\u5bf9\u5b57\u6bcd\u8fdb\u884c\u6392\u5e8f\u65f6\uff0cPython\u9ed8\u8ba4\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\u3002\u5927\u5199\u5b57\u6bcd\u4f1a\u6392\u5728\u5c0f\u5199\u5b57\u6bcd\u4e4b\u524d\u3002\u5982\u679c\u60a8\u5e0c\u671b\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff0c\u53ef\u4ee5\u4f7f\u7528<code>key<\/code>\u53c2\u6570\u6765\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">letters = [&#39;d&#39;, &#39;A&#39;, &#39;c&#39;, &#39;B&#39;]\nsorted_letters = sorted(letters, key=str.lower)  # \u8fd4\u56de [&#39;A&#39;, &#39;B&#39;, &#39;c&#39;, &#39;d&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u6309\u7167\u5b57\u6bcd\u7684\u5b57\u6bcd\u987a\u5e8f\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u4e0d\u8003\u8651\u5b83\u4eec\u7684\u5927\u5c0f\u5199\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7ed9\u82f1\u6587\u5b57\u6bcd\u6392\u5e8f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Python\u5185\u7f6e\u7684sorted\u51fd\u6570\u3001list\u7684sort\u65b9\u6cd5\u3001\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1108536,"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\/1108531"}],"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=1108531"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108531\/revisions"}],"predecessor-version":[{"id":1108537,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108531\/revisions\/1108537"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108536"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}