{"id":1131577,"date":"2025-01-08T20:49:42","date_gmt":"2025-01-08T12:49:42","guid":{"rendered":""},"modified":"2025-01-08T20:49:47","modified_gmt":"2025-01-08T12:49:47","slug":"python%e5%a6%82%e4%bd%95%e6%9b%bf%e6%8d%a2%e5%88%97%e8%a1%a8%e4%b8%ad%e7%9a%84%e5%ad%97%e7%ac%a6%e5%a4%a7%e5%b0%8f%e5%86%99","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1131577.html","title":{"rendered":"python\u5982\u4f55\u66ff\u6362\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u5927\u5c0f\u5199"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101316\/74dc1801-47a4-4400-9825-4780a958d878.webp\" alt=\"python\u5982\u4f55\u66ff\u6362\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u5927\u5c0f\u5199\" \/><\/p>\n<p><p> <strong>Python \u66ff\u6362\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u5927\u5c0f\u5199\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001map\u51fd\u6570\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u793a\u4f8b\u4ee3\u7801\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u4e14\u9ad8\u6548\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e00\u65b9\u6cd5\uff0c\u5e76\u63a2\u8ba8\u5176\u4ed6\u53ef\u9009\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u80fd\u591f\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5bf9\u5217\u8868\u8fdb\u884c\u64cd\u4f5c\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u66ff\u6362\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u5927\u5c0f\u5199\u662f\u6700\u5e38\u7528\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f8b\u5b50\uff1a\u5c06\u5217\u8868\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199<\/p>\n<p>original_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>upper_list = [item.upper() for item in original_list]<\/p>\n<p>print(upper_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6267\u884c\u901f\u5ea6\u5feb\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u7b80\u5355\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u662f <code>[expression for item in iterable]<\/code>\uff0c\u5176\u4e2d <code>expression<\/code> \u662f\u5bf9\u6bcf\u4e2a <code>item<\/code> \u8fdb\u884c\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u5c06 <code>expression<\/code> \u66ff\u6362\u4e3a\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u5982 <code>upper()<\/code> \u6216 <code>lower()<\/code>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u5b57\u7b26\u5927\u5c0f\u5199\u7684\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528map\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>map()<\/code> \u51fd\u6570\u662f Python \u5185\u7f6e\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u4e8e\u4e00\u4e2a\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e2a\u9879\u3002\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u76f8\u6bd4\uff0c<code>map()<\/code> \u51fd\u6570\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5e94\u7528\u590d\u6742\u51fd\u6570\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f8b\u5b50\uff1a\u5c06\u5217\u8868\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199<\/p>\n<p>original_list = [&#39;APPLE&#39;, &#39;BANANA&#39;, &#39;CHERRY&#39;]<\/p>\n<p>lower_list = list(map(str.lower, original_list))<\/p>\n<p>print(lower_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p><code>map()<\/code> \u51fd\u6570\u7684\u8bed\u6cd5\u662f <code>map(function, iterable)<\/code>\uff0c\u5176\u4e2d <code>function<\/code> \u662f\u5bf9\u6bcf\u4e2a <code>item<\/code> \u8fdb\u884c\u64cd\u4f5c\u7684\u51fd\u6570\u3002\u901a\u8fc7\u5c06 <code>str.lower<\/code> \u6216 <code>str.upper<\/code> \u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u4f20\u9012\u7ed9 <code>map()<\/code>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u5b57\u7b26\u5927\u5c0f\u5199\u7684\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5bf9\u5355\u4e2a\u5b57\u7b26\u4e32\u8fdb\u884c\u64cd\u4f5c\uff0c\u800c\u4e0d\u662f\u6574\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f8b\u5b50\uff1a\u5c06\u5355\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199<\/p>\n<p>original_string = &#39;hello world&#39;<\/p>\n<p>upper_string = original_string.upper()<\/p>\n<p>print(upper_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u65b9\u6cd5\u5982 <code>upper()<\/code> \u548c <code>lower()<\/code> \u53ef\u4ee5\u76f4\u63a5\u4f5c\u7528\u4e8e\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u5217\u8868\uff0c\u4f46\u53ef\u4ee5\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u6216 <code>map()<\/code> \u51fd\u6570\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u81ea\u5b9a\u4e49\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5b9a\u4e49\u81ea\u5df1\u7684\u51fd\u6570\u6765\u5904\u7406\u5b57\u7b26\u5927\u5c0f\u5199\u7684\u66ff\u6362\u3002\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u6700\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f8b\u5b50\uff1a\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u5c06\u5217\u8868\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u4e32\u9996\u5b57\u6bcd\u5927\u5199<\/p>\n<p>def capitalize_first_letter(item):<\/p>\n<p>    return item.capitalize()<\/p>\n<p>original_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>capitalized_list = list(map(capitalize_first_letter, original_list))<\/p>\n<p>print(capitalized_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u5b57\u7b26\u5927\u5c0f\u5199\u8fdb\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u53ea\u5c06\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u5927\u5199\uff0c\u800c\u5176\u4ed6\u5b57\u7b26\u4fdd\u6301\u4e0d\u53d8\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>map()<\/code> \u51fd\u6570\u5c06\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u5e94\u7528\u4e8e\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u5b57\u7b26\u5927\u5c0f\u5199\u7684\u66ff\u6362\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u5904\u7406\u590d\u6742\u903b\u8f91\uff0c\u518d\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216 <code>map()<\/code> \u51fd\u6570\u5e94\u7528\u4e8e\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f8b\u5b50\uff1a\u5c06\u5217\u8868\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u4e32\u9996\u5b57\u6bcd\u5927\u5199\uff0c\u5e76\u5c06\u5176\u4ed6\u5b57\u7b26\u8f6c\u6362\u4e3a\u5c0f\u5199<\/p>\n<p>def title_case(item):<\/p>\n<p>    return item.title()<\/p>\n<p>original_list = [&#39;aPPle&#39;, &#39;bANANA&#39;, &#39;chERRY&#39;]<\/p>\n<p>title_case_list = [title_case(item) for item in original_list]<\/p>\n<p>print(title_case_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u81ea\u5b9a\u4e49\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u5904\u7406\u66f4\u52a0\u590d\u6742\u7684\u5b57\u7b26\u5927\u5c0f\u5199\u66ff\u6362\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a <code>title_case<\/code> \u51fd\u6570\uff0c\u5c06\u5b57\u7b26\u4e32\u7684\u9996\u5b57\u6bcd\u5927\u5199\uff0c\u5e76\u5c06\u5176\u4ed6\u5b57\u7b26\u8f6c\u6362\u4e3a\u5c0f\u5199\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u8fd9\u4e2a\u51fd\u6570\u5e94\u7528\u4e8e\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u9879\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u5d4c\u5957\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5217\u8868\u4e2d\u53ef\u80fd\u5305\u542b\u5d4c\u5957\u5217\u8868\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u5904\u7406\u5b57\u7b26\u5927\u5c0f\u5199\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f8b\u5b50\uff1a\u9012\u5f52\u51fd\u6570\uff0c\u5c06\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199<\/p>\n<p>def upper_case_nested_list(nested_list):<\/p>\n<p>    result = []<\/p>\n<p>    for item in nested_list:<\/p>\n<p>        if isinstance(item, list):<\/p>\n<p>            result.append(upper_case_nested_list(item))<\/p>\n<p>        else:<\/p>\n<p>            result.append(item.upper())<\/p>\n<p>    return result<\/p>\n<p>original_list = [[&#39;apple&#39;, &#39;banana&#39;], [&#39;cherry&#39;, [&#39;date&#39;, &#39;elderberry&#39;]]]<\/p>\n<p>upper_list = upper_case_nested_list(original_list)<\/p>\n<p>print(upper_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u662f\u4e00\u79cd\u5728\u51fd\u6570\u5185\u90e8\u8c03\u7528\u81ea\u8eab\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\uff0c\u53ef\u4ee5\u5904\u7406\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u5927\u5c0f\u5199\u66ff\u6362\u9700\u6c42\u3002\u8be5\u9012\u5f52\u51fd\u6570\u68c0\u67e5\u6bcf\u4e2a\u9879\u662f\u5426\u4e3a\u5217\u8868\uff0c\u5982\u679c\u662f\uff0c\u5219\u9012\u5f52\u8c03\u7528\u81ea\u8eab\uff1b\u5426\u5219\uff0c\u5e94\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5 <code>upper()<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u6027\u80fd\u8003\u8651<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u901a\u5e38\u6bd4 <code>map()<\/code> \u51fd\u6570\u66f4\u5feb\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c<code>map()<\/code> \u51fd\u6570\u7ed3\u5408\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u80fd\u5177\u6709\u66f4\u597d\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6027\u80fd\u6d4b\u8bd5\uff1a\u5904\u7406\u767e\u4e07\u7ea7\u522b\u6570\u636e<\/p>\n<p>import time<\/p>\n<p>large_list = [&#39;apple&#39; * 1000] * 1000000<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>upper_list = [item.upper() for item in large_list]<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;\u5217\u8868\u63a8\u5bfc\u5f0f\u8017\u65f6\uff1a{end_time - start_time}\u79d2&quot;)<\/p>\n<h2><strong>\u4f7f\u7528map\u51fd\u6570<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>upper_list = list(map(str.upper, large_list))<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;map\u51fd\u6570\u8017\u65f6\uff1a{end_time - start_time}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u6bd4\u5217\u8868\u63a8\u5bfc\u5f0f\u548c <code>map()<\/code> \u51fd\u6570\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u53d1\u73b0\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u52a3\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u6027\u80fd\u66f4\u597d\uff0c\u4f46\u5728\u5904\u7406\u590d\u6742\u51fd\u6570\u65f6\uff0c<code>map()<\/code> \u51fd\u6570\u53ef\u80fd\u66f4\u5177\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u66ff\u6362\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u5927\u5c0f\u5199\u662fPython\u4e2d\u7684\u5e38\u89c1\u64cd\u4f5c\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u591a\u79cd\u5b9e\u73b0\u65b9\u6cd5\uff0c\u5305\u62ec\u5217\u8868\u63a8\u5bfc\u5f0f\u3001<code>map()<\/code> \u51fd\u6570\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u901a\u8fc7\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5e94\u5bf9\u5404\u79cd\u590d\u6742\u9700\u6c42\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p><strong>\u6838\u5fc3\u5185\u5bb9\u603b\u7ed3\uff1a<\/strong> \u5217\u8868\u63a8\u5bfc\u5f0f\u3001<code>map()<\/code> \u51fd\u6570\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u3001\u5904\u7406\u5d4c\u5957\u5217\u8868\u3001\u6027\u80fd\u8003\u8651\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u5217\u8868\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u8f6c\u6362\u4e3a\u5927\u5199\u6216\u5c0f\u5199\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u5b57\u7b26\u4e32\u7684<code>upper()<\/code>\u548c<code>lower()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u7b26\u7684\u5927\u5c0f\u5199\u8f6c\u6362\u3002\u6bd4\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5927\u5199\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>new_list = [char.upper() for char in original_list]<\/code>\u3002\u540c\u7406\uff0c\u4f7f\u7528<code>lower()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5c0f\u5199\u3002\u8fd9\u6837\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u6574\u4e2a\u5217\u8868\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u66ff\u6362\u5217\u8868\u4e2d\u7279\u5b9a\u5b57\u7b26\u7684\u5927\u5c0f\u5199\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u66ff\u6362\u7279\u5b9a\u5b57\u7b26\u7684\u5927\u5c0f\u5199\u3002\u6bd4\u5982\uff0c\u5982\u679c\u53ea\u60f3\u5c06\u5217\u8868\u4e2d\u6240\u6709\u7684\u5b57\u6bcd\u201ca\u201d\u8f6c\u6362\u4e3a\u5927\u5199\uff0c\u800c\u5176\u4ed6\u5b57\u7b26\u4fdd\u6301\u4e0d\u53d8\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a<code>new_list = [char.upper() if char == &#39;a&#39; else char for char in original_list]<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u54ea\u4e9b\u5b57\u7b26\u9700\u8981\u88ab\u66ff\u6362\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5217\u8868\u4e2d\u540c\u65f6\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u7684\u5927\u5c0f\u5199\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u6765\u6620\u5c04\u9700\u8981\u66ff\u6362\u7684\u5b57\u7b26\u53ca\u5176\u5bf9\u5e94\u7684\u5927\u5c0f\u5199\u5f62\u5f0f\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u5b57\u5178\u4e2d\u7684\u6620\u5c04\u5173\u7cfb\u8fdb\u884c\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u9700\u8981\u5c06\u201cA\u201d\u548c\u201cb\u201d\u66ff\u6362\u4e3a\u5c0f\u5199\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>new_list = [replace_dict.get(char, char) for char in original_list]<\/code>\uff0c\u5176\u4e2d<code>replace_dict = {&#39;A&#39;: &#39;a&#39;, &#39;b&#39;: &#39;B&#39;}<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u591a\u4e2a\u5b57\u7b26\u7684\u5927\u5c0f\u5199\u66ff\u6362\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u66ff\u6362\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u5927\u5c0f\u5199\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001map\u51fd\u6570\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u672c\u6587\u5c06\u8be6 [&hellip;]","protected":false},"author":3,"featured_media":1131588,"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\/1131577"}],"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=1131577"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131577\/revisions"}],"predecessor-version":[{"id":1131594,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131577\/revisions\/1131594"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1131588"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1131577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1131577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1131577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}