{"id":1109995,"date":"2025-01-08T17:15:30","date_gmt":"2025-01-08T09:15:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1109995.html"},"modified":"2025-01-08T17:15:32","modified_gmt":"2025-01-08T09:15:32","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%88%97%e8%a1%a8%e4%b8%ad%e7%9a%84%e7%a9%ba%e6%a0%bc%e5%88%a0%e9%99%a4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1109995.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5217\u8868\u4e2d\u7684\u7a7a\u683c\u5220\u9664"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073033\/8864a4f7-6103-48f0-b6fd-d47535683af9.webp\" alt=\"python\u5982\u4f55\u5c06\u5217\u8868\u4e2d\u7684\u7a7a\u683c\u5220\u9664\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u5217\u8868\u4e2d\u7684\u7a7a\u683c\u5220\u9664<\/strong>\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter\u51fd\u6570\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002<strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u6cd5<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u9488\u5bf9\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u5b83\u7ed3\u5408\u4e86for\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u5185\u5b9e\u73b0\u5220\u9664\u7a7a\u683c\u7684\u529f\u80fd\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u5217\u8868 <code>my_list<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u5176\u4e2d\u7684\u7a7a\u683c\u5220\u9664\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39; &#39;, &#39;b&#39;, &#39;c&#39;, &#39; &#39;, &#39;d&#39;]<\/p>\n<p>cleaned_list = [item for item in my_list if item != &#39; &#39;]<\/p>\n<p>print(cleaned_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u4e86 <code>my_list<\/code> \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u4e14\u53ea\u4fdd\u7559\u4e0d\u7b49\u4e8e\u7a7a\u683c\u7684\u5143\u7d20\uff0c\u6700\u7ec8\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868 <code>cleaned_list<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001filter\u51fd\u6570<\/p>\n<p><code>filter()<\/code> \u51fd\u6570\u7528\u4e8e\u6784\u5efa\u4e00\u4e2a\u65b0\u7684\u8fed\u4ee3\u5668\uff0c\u7b5b\u9009\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408 <code>lambda<\/code> \u8868\u8fbe\u5f0f\u4f7f\u7528 <code>filter()<\/code> \u51fd\u6570\u6765\u5220\u9664\u5217\u8868\u4e2d\u7684\u7a7a\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39; &#39;, &#39;b&#39;, &#39;c&#39;, &#39; &#39;, &#39;d&#39;]<\/p>\n<p>cleaned_list = list(filter(lambda x: x != &#39; &#39;, my_list))<\/p>\n<p>print(cleaned_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>filter()<\/code> \u51fd\u6570\u7b5b\u9009\u51fa <code>my_list<\/code> \u4e2d\u6240\u6709\u4e0d\u7b49\u4e8e\u7a7a\u683c\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u7ed3\u679c\u8f6c\u6362\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868 <code>cleaned_list<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u5339\u914d\u548c\u66ff\u6362\u7279\u5b9a\u7684\u5b57\u7b26\u6a21\u5f0f\u3002\u867d\u7136\u5728\u5220\u9664\u5217\u8868\u4e2d\u7684\u7a7a\u683c\u65f6\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u80fd\u4e0d\u662f\u6700\u7b80\u6d01\u7684\u65b9\u6cd5\uff0c\u4f46\u5b83\u5728\u5904\u7406\u590d\u6742\u6587\u672c\u65f6\u975e\u5e38\u6709\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>my_list = [&#39;a&#39;, &#39; &#39;, &#39;b&#39;, &#39;c&#39;, &#39; &#39;, &#39;d&#39;]<\/p>\n<p>pattern = re.compile(r&#39;\\s&#39;)<\/p>\n<p>cleaned_list = [item for item in my_list if not pattern.match(item)]<\/p>\n<p>print(cleaned_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>re.compile(r&#39;\\s&#39;)<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u5339\u914d\u7a7a\u767d\u5b57\u7b26\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408 <code>pattern.match(item)<\/code> \u7b5b\u9009\u51fa\u6240\u6709\u4e0d\u5339\u914d\u7a7a\u767d\u5b57\u7b26\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5<\/p>\n<p>\u5982\u679c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>strip()<\/code> \u65b9\u6cd5\u5220\u9664\u7a7a\u683c\u3002<code>strip()<\/code> \u65b9\u6cd5\u4f1a\u5220\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\uff0c\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39; &#39;, &#39;b&#39;, &#39;c&#39;, &#39; &#39;, &#39;d&#39;]<\/p>\n<p>cleaned_list = [item.strip() for item in my_list if item.strip() != &#39;&#39;]<\/p>\n<p>print(cleaned_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>strip()<\/code> \u65b9\u6cd5\u5220\u9664\u4e86\u6bcf\u4e2a\u5143\u7d20\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7b5b\u9009\u51fa\u4e0d\u4e3a\u7a7a\u5b57\u7b26\u4e32\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7ed3\u5408\u6761\u4ef6\u8868\u8fbe\u5f0f\u548cfor\u5faa\u73af<\/p>\n<p>\u9664\u4e86\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4f20\u7edf\u7684for\u5faa\u73af\u7ed3\u5408\u6761\u4ef6\u8868\u8fbe\u5f0f\u5220\u9664\u5217\u8868\u4e2d\u7684\u7a7a\u683c\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4e0d\u5982\u5217\u8868\u63a8\u5bfc\u5f0f\u7b80\u6d01\uff0c\u4f46\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u8c03\u8bd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39; &#39;, &#39;b&#39;, &#39;c&#39;, &#39; &#39;, &#39;d&#39;]<\/p>\n<p>cleaned_list = []<\/p>\n<p>for item in my_list:<\/p>\n<p>    if item != &#39; &#39;:<\/p>\n<p>        cleaned_list.append(item)<\/p>\n<p>print(cleaned_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cfor\u5faa\u73af\u904d\u5386\u4e86 <code>my_list<\/code> \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u6761\u4ef6\u8868\u8fbe\u5f0f <code>if item != &#39; &#39;<\/code> \u7b5b\u9009\u51fa\u4e0d\u7b49\u4e8e\u7a7a\u683c\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u65b0\u7684\u5217\u8868 <code>cleaned_list<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u7ed3\u5408enumerate\u51fd\u6570<\/p>\n<p>\u6709\u65f6\u9700\u8981\u4fdd\u7559\u539f\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>enumerate()<\/code> \u51fd\u6570\u904d\u5386\u5217\u8868\uff0c\u8bb0\u5f55\u6bcf\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u5e76\u5220\u9664\u7a7a\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39; &#39;, &#39;b&#39;, &#39;c&#39;, &#39; &#39;, &#39;d&#39;]<\/p>\n<p>indices_to_remove = [index for index, item in enumerate(my_list) if item == &#39; &#39;]<\/p>\n<p>for index in sorted(indices_to_remove, reverse=True):<\/p>\n<p>    del my_list[index]<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>enumerate()<\/code> \u51fd\u6570\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b\u7d22\u5f15\u548c\u503c\u7684\u5143\u7ec4\u5217\u8868\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7b5b\u9009\u51fa\u7b49\u4e8e\u7a7a\u683c\u7684\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u5e76\u5728for\u5faa\u73af\u4e2d\u6309\u7d22\u5f15\u5220\u9664\u8fd9\u4e9b\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528while\u5faa\u73af\u7ed3\u5408remove\u65b9\u6cd5<\/p>\n<p><code>remove()<\/code> \u65b9\u6cd5\u7528\u4e8e\u4ece\u5217\u8868\u4e2d\u5220\u9664\u6307\u5b9a\u7684\u5143\u7d20\u3002\u5982\u679c\u5217\u8868\u4e2d\u53ef\u80fd\u5305\u542b\u591a\u4e2a\u7a7a\u683c\uff0c\u53ef\u4ee5\u7ed3\u5408 <code>while<\/code> \u5faa\u73af\u4f7f\u7528 <code>remove()<\/code> \u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39; &#39;, &#39;b&#39;, &#39;c&#39;, &#39; &#39;, &#39;d&#39;]<\/p>\n<p>while &#39; &#39; in my_list:<\/p>\n<p>    my_list.remove(&#39; &#39;)<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>while<\/code> \u5faa\u73af\u6301\u7eed\u8fd0\u884c\uff0c\u76f4\u5230\u5217\u8868\u4e2d\u4e0d\u518d\u5305\u542b\u7a7a\u683c\uff0c<code>remove()<\/code> \u65b9\u6cd5\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u5220\u9664\u4e00\u4e2a\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u4f7f\u7528itertools\u6a21\u5757<\/p>\n<p><code>itertools<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\u5de5\u5177\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>itertools.filterfalse()<\/code> \u51fd\u6570\u5220\u9664\u5217\u8868\u4e2d\u7684\u7a7a\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>my_list = [&#39;a&#39;, &#39; &#39;, &#39;b&#39;, &#39;c&#39;, &#39; &#39;, &#39;d&#39;]<\/p>\n<p>cleaned_list = list(itertools.filterfalse(lambda x: x == &#39; &#39;, my_list))<\/p>\n<p>print(cleaned_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>itertools.filterfalse()<\/code> \u51fd\u6570\u7b5b\u9009\u51fa <code>my_list<\/code> \u4e2d\u6240\u6709\u4e0d\u7b49\u4e8e\u7a7a\u683c\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u7ed3\u679c\u8f6c\u6362\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868 <code>cleaned_list<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u7ed3\u5408map\u51fd\u6570\u548c\u6761\u4ef6\u8868\u8fbe\u5f0f<\/p>\n<p><code>map()<\/code> \u51fd\u6570\u7528\u4e8e\u5c06\u6307\u5b9a\u51fd\u6570\u5e94\u7528\u4e8e\u8fed\u4ee3\u5668\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u8868\u8fbe\u5f0f\u548c <code>map()<\/code> \u51fd\u6570\u5220\u9664\u5217\u8868\u4e2d\u7684\u7a7a\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39; &#39;, &#39;b&#39;, &#39;c&#39;, &#39; &#39;, &#39;d&#39;]<\/p>\n<p>cleaned_list = list(filter(None, map(lambda x: x if x != &#39; &#39; else None, my_list)))<\/p>\n<p>print(cleaned_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>map()<\/code> \u51fd\u6570\u5e94\u7528\u4e86\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u5c06\u4e0d\u7b49\u4e8e\u7a7a\u683c\u7684\u5143\u7d20\u4fdd\u7559\uff0c\u5c06\u7b49\u4e8e\u7a7a\u683c\u7684\u5143\u7d20\u66ff\u6362\u4e3a <code>None<\/code>\uff0c<code>filter(None, ...)<\/code> \u51fd\u6570\u8fc7\u6ee4\u6389\u6240\u6709 <code>None<\/code> \u503c\uff0c\u751f\u6210\u65b0\u7684\u5217\u8868 <code>cleaned_list<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<p>\u5728Python\u4e2d\uff0c\u5220\u9664\u5217\u8868\u4e2d\u7684\u7a7a\u683c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u4f20\u7edffor\u5faa\u73af\u3001enumerate\u51fd\u6570\u3001while\u5faa\u73af\u7ed3\u5408remove\u65b9\u6cd5\u3001itertools\u6a21\u5757\u548cmap\u51fd\u6570\u7ed3\u5408\u6761\u4ef6\u8868\u8fbe\u5f0f\u7b49\u3002<strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u6700\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5<\/strong>\uff0c\u63a8\u8350\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5220\u9664\u5217\u8868\u4e2d\u7684\u7a7a\u683c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664\u5217\u8868\u4e2d\u7684\u7a7a\u767d\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u8f7b\u677e\u5220\u9664\u5217\u8868\u4e2d\u7684\u7a7a\u767d\u5143\u7d20\u3002\u901a\u8fc7\u904d\u5386\u5217\u8868\u5e76\u4ec5\u4fdd\u7559\u975e\u7a7a\u767d\u5143\u7d20\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39;&#39;, &#39;b&#39;, &#39; &#39;, &#39;c&#39;]\ncleaned_list = [item for item in my_list if item.strip()]\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5305\u542b\u4e86\u539f\u5217\u8868\u4e2d\u6240\u6709\u975e\u7a7a\u767d\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u4f7f\u7528\u8fc7\u6ee4\u5668\u51fd\u6570\u600e\u6837\u5220\u9664\u5217\u8868\u4e2d\u7684\u7a7a\u767d\uff1f<\/strong><br \/>\u9664\u4e86\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>filter()<\/code>\u51fd\u6570\u6765\u5220\u9664\u5217\u8868\u4e2d\u7684\u7a7a\u767d\u5143\u7d20\u3002\u7ed3\u5408<code>str.strip()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u76f8\u540c\u7684\u6548\u679c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39;&#39;, &#39;b&#39;, &#39; &#39;, &#39;c&#39;]\ncleaned_list = list(filter(lambda item: item.strip(), my_list))\n<\/code><\/pre>\n<p><code>cleaned_list<\/code>\u5c06\u5305\u542b\u6240\u6709\u975e\u7a7a\u767d\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5220\u9664\u5217\u8868\u4e2d\u7684\u7a7a\u767d\u5143\u7d20\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c<code>filter()<\/code>\u51fd\u6570\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u624b\u52a8\u6784\u5efa\u65b0\u7684\u5217\u8868\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39;&#39;, &#39;b&#39;, &#39; &#39;, &#39;c&#39;]\ncleaned_list = []\nfor item in my_list:\n    if item.strip():\n        cleaned_list.append(item)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u66f4\u4e3a\u57fa\u7840\uff0c\u4f46\u540c\u6837\u6709\u6548\uff0c\u5c24\u5176\u9002\u5408\u521d\u5b66\u8005\u7406\u89e3\u5217\u8868\u64cd\u4f5c\u7684\u6d41\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u5217\u8868\u4e2d\u7684\u7a7a\u683c\u5220\u9664\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter\u51fd\u6570\u548c\u6b63\u5219\u8868\u8fbe [&hellip;]","protected":false},"author":3,"featured_media":1110001,"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\/1109995"}],"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=1109995"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109995\/revisions"}],"predecessor-version":[{"id":1110002,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109995\/revisions\/1110002"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1110001"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1109995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1109995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1109995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}