{"id":178909,"date":"2024-05-08T20:01:34","date_gmt":"2024-05-08T12:01:34","guid":{"rendered":""},"modified":"2024-05-08T20:02:12","modified_gmt":"2024-05-08T12:02:12","slug":"python-%e7%a8%8b%e5%ba%8f%e4%b8%ad%ef%bc%8ckeystr-lower-%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/178909.html","title":{"rendered":"Python \u7a0b\u5e8f\u4e2d\uff0ckey=str.lower \u662f\u4ec0\u4e48\u610f\u601d"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27063534\/9567c5e2-f5e9-444c-a994-d89642a35dc9.webp\" alt=\"Python \u7a0b\u5e8f\u4e2d\uff0ckey=str.lower \u662f\u4ec0\u4e48\u610f\u601d\" \/><\/p>\n<p><p><strong>Python \u7a0b\u5e8f\u4e2d\uff0c<code>key=str.lower<\/code> \u662f\u4e00\u79cd\u7528\u4e8e\u6307\u5b9a\u6392\u5e8f\u6216\u6bd4\u8f83\u8fc7\u7a0b\u4e2d\u7684\u952e\u51fd\u6570\uff0c\u5176\u4f5c\u7528\u662f\u5c06\u6240\u6709\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199\u6765\u8fdb\u884c\u6bd4\u8f83<\/strong>\uff0c\u8fd9\u6837\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u3002\u901a\u5e38\uff0c\u6b64\u79cd\u7528\u6cd5\u51fa\u73b0\u5728\u6392\u5e8f\u51fd\u6570\uff08\u5982 <code>sorted()<\/code> \u6216\u5217\u8868\u7684 <code>sort()<\/code> \u65b9\u6cd5\uff09\u6216\u8005\u662f\u5728\u6570\u636e\u7ed3\u6784\u7684\u67e5\u627e\u548c\u6bd4\u8f83\u64cd\u4f5c\u4e2d\u3002\u5b83\u901a\u8fc7\u628a <code>str.lower<\/code> \u51fd\u6570\u4f5c\u4e3a <code>key<\/code> \u53c2\u6570\u503c\u4f20\u9012\uff0c\u786e\u4fdd\u5728\u6392\u5e8f\u6216\u6bd4\u8f83\u65f6\uff0c\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\u5199\u662f\u4e0d\u88ab\u8003\u8651\u7684\u3002<\/p>\n<\/p>\n<p><p>\u4e3e\u4e2a\u793a\u4f8b\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u9700\u8981\u6309\u7167\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\uff0c\u4f46\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff0c\u5219\u53ef\u4ee5\u5c06 <code>str.lower<\/code> \u4f5c\u4e3a\u952e\u51fd\u6570\u63d0\u4f9b\u7ed9\u6392\u5e8f\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">words = [&#039;banana&#039;, &#039;Apple&#039;, &#039;cherry&#039;]<\/p>\n<p>words.sort(key=str.lower)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>words.sort(key=str.lower)<\/code> \u4e4b\u540e\u7684\u5217\u8868\u4f1a\u88ab\u6392\u5e8f\u4e3a <code>[&#039;Apple&#039;, &#039;banana&#039;, &#039;cherry&#039;]<\/code>\uff0c\u5373\u4f7f &quot;Apple&quot; \u7684\u9996\u5b57\u6bcd\u662f\u5927\u5199\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001KEY \u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406<\/h3>\n<\/p>\n<p><p><strong>\u5f53\u5728\u6392\u5e8f\u6216\u5176\u4ed6\u64cd\u4f5c\u4e2d\u6307\u5b9a\u4e86\u952e\u51fd\u6570\u540e\uff0cPython \u4f1a\u5728\u6bcf\u6b21\u6bd4\u8f83\u5143\u7d20\u4e4b\u524d\u9996\u5148\u8c03\u7528\u8be5\u51fd\u6570<\/strong>\u3002\u952e\u51fd\u6570\u63a5\u6536\u76ee\u6807\u5bf9\u8c61\u4f5c\u4e3a\u8f93\u5165\uff08\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u4e3a\u5b57\u7b26\u4e32\uff09\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u7528\u4e8e\u6bd4\u8f83\u7684\u503c\u3002\u5f53\u4f7f\u7528 <code>str.lower<\/code> \u65f6\uff0c\u5b83\u4f1a\u63a5\u6536\u5b57\u7b26\u4e32\uff0c\u5e76\u8fd4\u56de\u8be5\u5b57\u7b26\u4e32\u7684\u5168\u5c0f\u5199\u7248\u672c\u3002\u8fd9\u6837\uff0c\u5728\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u65f6\uff0c\u5b9e\u9645\u4e0a\u6bd4\u8f83\u7684\u662f\u5b83\u4eec\u7684\u5c0f\u5199\u7248\u672c\uff0c\u4ece\u800c\u5b9e\u73b0\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u6392\u5e8f\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>\u952e\u51fd\u6570\u7684\u6982\u5ff5\u4e0d\u4ec5\u9002\u7528\u4e8e\u6392\u5e8f\uff0c\u8fd8\u5e94\u7528\u4e8e\u591a\u79cd\u6709\u6bd4\u8f83\u903b\u8f91\u7684\u573a\u666f<\/strong>\uff0c\u5982\u6700\u5927\u6700\u5c0f\u503c\u67e5\u627e\u3001\u6570\u636e\u5206\u7ec4\u3001\u53bb\u91cd\u7b49\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u5bf9\u5217\u8868\u8fdb\u884c\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u6392\u5e8f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">names = [&#039;Alice&#039;, &#039;bob&#039;, &#039;Cathy&#039;, &#039;david&#039;]<\/p>\n<p>sorted_names = sorted(names, key=str.lower)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5bfb\u627e\u6700\u5927\u6216\u6700\u5c0f\u5143\u7d20<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">names = [&#039;Alice&#039;, &#039;bob&#039;, &#039;Cathy&#039;, &#039;david&#039;]<\/p>\n<p>max_name = max(names, key=str.lower)<\/p>\n<p>min_name = min(names, key=str.lower)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u952e\u51fd\u6570\u7684\u8fdb\u9636\u5e94\u7528<\/h3>\n<\/p>\n<p><p><strong>\u9664\u4e86\u7b80\u5355\u7684\u5927\u5c0f\u5199\u8f6c\u6362\uff0c<code>key<\/code> \u51fd\u6570\u53ef\u4ee5\u66f4\u52a0\u590d\u6742\u548c\u5f3a\u5927\uff0c\u5b83\u80fd\u7ed3\u5408\u591a\u79cdpython\u5185\u5efa\u51fd\u6570\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u66f4\u7279\u5b9a\u7684\u9700\u6c42<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>\u7ed3\u5408 <code>len<\/code> \u51fd\u6570\u8fdb\u884c\u957f\u5ea6\u6392\u5e8f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">words = [&#039;banana&#039;, &#039;pie&#039;, &#039;Washington&#039;, &#039;apple&#039;]<\/p>\n<p>words.sort(key=len)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u590d\u6742\u6392\u5e8f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_sort(string):<\/p>\n<p>    return string[-1], string.lower()<\/p>\n<p>words = [&#039;banana&#039;, &#039;pie&#039;, &#039;Washington&#039;, &#039;apple&#039;]<\/p>\n<p>words.sort(key=custom_sort)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5904 <code>custom_sort<\/code> \u51fd\u6570\u5229\u7528\u5143\u7ec4\u8fd4\u56de\u4e24\u4e2a\u503c\uff0c\u9996\u5148\u6839\u636e\u5355\u8bcd\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u6bcd\u8fdb\u884c\u6392\u5e8f\uff0c\u5982\u679c\u6700\u540e\u4e00\u4e2a\u5b57\u6bcd\u76f8\u540c\uff0c\u5219\u6309\u7167\u5168\u5c0f\u5199\u7684\u5355\u8bcd\u987a\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u952e\u51fd\u6570\u5728\u6570\u636e\u5904\u7406\u4e2d\u7684\u4ef7\u503c<\/h3>\n<\/p>\n<p><p>\u952e\u51fd\u6570\u5728\u6570\u636e\u5904\u7406\u4e2d\u5c24\u4e3a\u91cd\u8981\uff0c\u5b83<strong>\u5141\u8bb8\u5904\u7406\u590d\u6742\u6570\u636e\u6216\u7ed3\u6784\u5316\u6570\u636e\u7684\u5b9a\u5236\u6392\u5e8f\u548c\u6bd4\u8f83\uff0c\u4ece\u800c\u6ee1\u8db3\u6570\u636e\u5206\u6790\u7684\u9700\u6c42<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>\u5904\u7406\u5305\u542b\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u5217\u8868<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">data = [&#039;banana&#039;, 10, &#039;pie&#039;, 0, &#039;Washington&#039;, 50, &#039;apple&#039;]<\/p>\n<p>data.sort(key=lambda x: str(x).lower())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 <code>lambda<\/code> \u51fd\u6570\u5c06\u6240\u6709\u5143\u7d20\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u5e76\u8fdb\u884c\u5c0f\u5199\u5316\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h4>\u7ed3\u5408\u6570\u636e\u7ed3\u6784\u7684\u6392\u5e8f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">students = [<\/p>\n<p>    {&#039;name&#039;: &#039;Alice&#039;, &#039;grade&#039;: 90},<\/p>\n<p>    {&#039;name&#039;: &#039;bob&#039;, &#039;grade&#039;: 85},<\/p>\n<p>    {&#039;name&#039;: &#039;Cathy&#039;, &#039;grade&#039;: 92},<\/p>\n<p>    {&#039;name&#039;: &#039;David&#039;, &#039;grade&#039;: 88}<\/p>\n<p>]<\/p>\n<p>students.sort(key=lambda x: x[&#039;name&#039;].lower())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u952e\u51fd\u6570\u4f7f\u5f97\u53ef\u4ee5\u5bf9\u5b57\u5178\u5217\u8868\u6309\u7167 <code>name<\/code> \u952e\u5bf9\u5e94\u503c\u7684\u5c0f\u5199\u5f62\u5f0f\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1: \u5982\u4f55\u5728Python\u7a0b\u5e8f\u4e2d\u4f7f\u7528key=str.lower\uff1f<\/strong> <\/p>\n<p><strong>A:<\/strong> \u5728Python\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>key=str.lower<\/code>\u6765\u5bf9\u96c6\u5408\u6216\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u8fd9\u91cc\u7684<code>str.lower<\/code>\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u5c06\u5143\u7d20\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\u5f62\u5f0f\uff0c\u7136\u540e\u6839\u636e\u5b57\u6bcd\u987a\u5e8f\u8fdb\u884c\u6392\u5e8f\u3002\u5728\u6392\u5e8f\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u8fdb\u884c\u6bd4\u8f83\uff0c\u4ece\u800c\u5b9e\u73b0\u6309\u7167\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>Q2: \u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528key=str.lower\u6765\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><\/p>\n<p><strong>A:<\/strong> \u6709\u65f6\uff0c\u5728\u6392\u5e8f\u5217\u8868\u6216\u96c6\u5408\u65f6\uff0c\u6211\u4eec\u5e0c\u671b\u5ffd\u7565\u5143\u7d20\u7684\u5927\u5c0f\u5199\uff0c\u53ea\u5173\u6ce8\u5b83\u4eec\u7684\u5b57\u6bcd\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u6309\u7167\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\uff0c\u4e0d\u8003\u8651\u5927\u5c0f\u5199\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528<code>key=str.lower<\/code>\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u5143\u7d20\u8f6c\u6362\u4e3a\u5c0f\u5199\u5f62\u5f0f\uff0c\u7136\u540e\u6309\u7167\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\u3002<\/p>\n<p><strong>Q3: \u9664\u4e86str.lower\uff0c\u8fd8\u6709\u5176\u4ed6\u7684key\u53c2\u6570\u53ef\u4f9b\u4f7f\u7528\u5417\uff1f<\/strong><\/p>\n<p><strong>A:<\/strong> \u662f\u7684\uff0c\u9664\u4e86<code>str.lower<\/code>\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u5f88\u591a\u53ef\u7528\u4e8ekey\u53c2\u6570\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff0c<code>key=str.upper<\/code>\u53ef\u4ee5\u5c06\u5143\u7d20\u8f6c\u6362\u4e3a\u5927\u5199\u5f62\u5f0f\u540e\u8fdb\u884c\u6392\u5e8f\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\u4f5c\u4e3akey\u53c2\u6570\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u6765\u5904\u7406\u5143\u7d20\u3002\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u5143\u7d20\u7684\u4e0d\u540c\u7279\u6027\u6765\u8fdb\u884c\u6392\u5e8f\uff0c\u4ece\u800c\u5b9e\u73b0\u7075\u6d3b\u7684\u6392\u5e8f\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u7a0b\u5e8f\u4e2d\uff0ckey=str.lower \u662f\u4e00\u79cd\u7528\u4e8e\u6307\u5b9a\u6392\u5e8f\u6216\u6bd4\u8f83\u8fc7\u7a0b\u4e2d\u7684\u952e\u51fd\u6570\uff0c\u5176\u4f5c\u7528\u662f\u5c06\u6240\u6709\u7684\u5b57\u7b26 [&hellip;]","protected":false},"author":3,"featured_media":178966,"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\/178909"}],"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=178909"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/178909\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/178966"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=178909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=178909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=178909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}