{"id":1118582,"date":"2025-01-08T18:36:45","date_gmt":"2025-01-08T10:36:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1118582.html"},"modified":"2025-01-08T18:36:47","modified_gmt":"2025-01-08T10:36:47","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%adlist%e9%87%8c%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1118582.html","title":{"rendered":"python\u5982\u4f55\u5224\u65adlist\u91cc\u4e3a\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082024\/acb8277c-e992-401e-9985-9debfc3c08bc.webp\" alt=\"python\u5982\u4f55\u5224\u65adlist\u91cc\u4e3a\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5224\u65adlist\u91cc\u662f\u5426\u5305\u542b\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\u3001\u4f7f\u7528\u5185\u7f6e\u7684all()\u6216any()\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\u662f\u6700\u76f4\u89c2\u7684\u65b9\u5f0f\uff0c\u80fd\u591f\u9010\u4e2a\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u5c55\u5f00\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u57fa\u7840\u7684\u65b9\u6cd5\u662f\u4f7f\u7528for\u5faa\u73af\u904d\u5386list\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u4f7f\u7528\u5185\u7f6e\u51fd\u6570isinstance()\u6765\u5224\u65ad\u5143\u7d20\u662f\u5426\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns_string(lst):<\/p>\n<p>    for item in lst:<\/p>\n<p>        if isinstance(item, str):<\/p>\n<p>            return True<\/p>\n<p>    return False<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>sample_list = [1, 2, &#39;hello&#39;, 4.5]<\/p>\n<p>print(contains_string(sample_list))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570contains_string()\u901a\u8fc7\u904d\u5386\u4f20\u5165\u7684list\uff08lst\uff09\uff0c\u68c0\u67e5\u5176\u4e2d\u662f\u5426\u6709\u4efb\u4f55\u5143\u7d20\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u5982\u679c\u627e\u5230\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u5c31\u8fd4\u56deTrue\uff1b\u5982\u679c\u904d\u5386\u5b8c\u6240\u6709\u5143\u7d20\u90fd\u6ca1\u6709\u627e\u5230\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u5219\u8fd4\u56deFalse\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u4f18\u70b9\u5728\u4e8e\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u7c7b\u578b\u7684list\uff0c\u5305\u62ec\u6df7\u5408\u7c7b\u578b\u7684list\u3002\u5176\u7f3a\u70b9\u662f\u5f53list\u975e\u5e38\u5927\u65f6\uff0c\u6548\u7387\u53ef\u80fd\u8f83\u4f4e\uff0c\u56e0\u4e3a\u9700\u8981\u904d\u5386\u6240\u6709\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u7684all()\u6216any()\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e24\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5185\u7f6e\u51fd\u6570all()\u548cany()\uff0c\u53ef\u4ee5\u7528\u6765\u7b80\u5316\u5bf9list\u4e2d\u5143\u7d20\u7684\u5224\u65ad\u3002all()\u51fd\u6570\u7528\u4e8e\u68c0\u67e5list\u4e2d\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\uff0c\u800cany()\u51fd\u6570\u7528\u4e8e\u68c0\u67e5list\u4e2d\u662f\u5426\u5b58\u5728\u81f3\u5c11\u4e00\u4e2a\u5143\u7d20\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528any()\u51fd\u6570\u6765\u5224\u65adlist\u4e2d\u662f\u5426\u5305\u542b\u5b57\u7b26\u4e32\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def contains_string(lst):<\/p>\n<p>    return any(isinstance(item, str) for item in lst)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>sample_list = [1, 2, &#39;hello&#39;, 4.5]<\/p>\n<p>print(contains_string(sample_list))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5217\u8868\u63a8\u5bfc\u5f0f\u548cany()\u51fd\u6570\u6765\u68c0\u67e5list\u4e2d\u662f\u5426\u6709\u4efb\u4f55\u5143\u7d20\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u8fd9\u6837\u7684\u65b9\u6cd5\u6bd4\u4f7f\u7528for\u5faa\u73af\u66f4\u7b80\u6d01\uff0c\u66f4\u7b26\u5408Pythonic\u7684\u7f16\u7a0b\u98ce\u683c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u539flist\u4e2d\u6240\u6709\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u5143\u7d20\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ea\u9700\u8981\u68c0\u67e5\u8fd9\u4e2a\u65b0\u5217\u8868\u662f\u5426\u4e3a\u7a7a\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def contains_string(lst):<\/p>\n<p>    string_elements = [item for item in lst if isinstance(item, str)]<\/p>\n<p>    return len(string_elements) &gt; 0<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>sample_list = [1, 2, &#39;hello&#39;, 4.5]<\/p>\n<p>print(contains_string(sample_list))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684list\uff08string_elements\uff09\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u539flist\u4e2d\u6240\u6709\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u5143\u7d20\u3002\u7136\u540e\uff0c\u6211\u4eec\u68c0\u67e5\u8fd9\u4e2a\u65b0list\u7684\u957f\u5ea6\u662f\u5426\u5927\u4e8e0\u6765\u5224\u65ad\u539flist\u4e2d\u662f\u5426\u5305\u542b\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u65b9\u6cd5\u4f18\u70b9\u5728\u4e8e\u4ee3\u7801\u7b80\u6d01\uff0c\u53ef\u8bfb\u6027\u5f3a\uff0c\u9002\u7528\u4e8e\u751f\u6210\u65b0\u7684list\u4ee5\u4f9b\u540e\u7eed\u64cd\u4f5c\u3002\u5176\u7f3a\u70b9\u5728\u4e8e\u9700\u8981\u989d\u5916\u7684\u5185\u5b58\u6765\u5b58\u50a8\u65b0list\uff0c\u53ef\u80fd\u5728\u5904\u7406\u975e\u5e38\u5927\u7684list\u65f6\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u6765\u5b9e\u73b0\u529f\u80fd\u3002\u5728\u5224\u65adlist\u4e2d\u662f\u5426\u5305\u542b\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u7efc\u5408\u4f7f\u7528for\u5faa\u73af\u3001\u5185\u7f6e\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u66f4\u52a0\u9ad8\u6548\u3001\u7b80\u6d01\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def contains_string(lst):<\/p>\n<p>    return any(isinstance(item, str) for item in lst)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>sample_list = [1, 2, &#39;hello&#39;, 4.5]<\/p>\n<p>print(contains_string(sample_list))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u5185\u7f6e\u7684any()\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4fdd\u8bc1\u4ee3\u7801\u7b80\u6d01\u7684\u540c\u65f6\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5224\u65adlist\u4e2d\u662f\u5426\u5305\u542b\u5b57\u7b26\u4e32\u7684\u9700\u6c42\u53ef\u80fd\u51fa\u73b0\u5728\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u5904\u7406\u3001\u7528\u6237\u8f93\u5165\u9a8c\u8bc1\u7b49\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u65f6\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u683c\u5f0f\u6b63\u786e\uff0c\u5305\u62ec\u68c0\u67e5list\u4e2d\u662f\u5426\u5305\u542b\u9884\u671f\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u573a\u666f\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def validate_user_input(data):<\/p>\n<p>    if not isinstance(data, list):<\/p>\n<p>        raise ValueError(&quot;Input data must be a list.&quot;)<\/p>\n<p>    if contains_string(data):<\/p>\n<p>        return &quot;Valid input: contains string.&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;Invalid input: no string found.&quot;<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>user_input = [1, 2, &#39;hello&#39;, 4.5]<\/p>\n<p>print(validate_user_input(user_input))  # \u8f93\u51fa: Valid input: contains string.<\/p>\n<p>user_input = [1, 2, 3, 4.5]<\/p>\n<p>print(validate_user_input(user_input))  # \u8f93\u51fa: Invalid input: no string found.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570validate_user_input()\u7528\u4e8e\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u662f\u5426\u4e3alist\uff0c\u5e76\u68c0\u67e5list\u4e2d\u662f\u5426\u5305\u542b\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u5143\u7d20\u3002\u5982\u679c\u8f93\u5165\u6570\u636e\u4e0d\u662flist\uff0c\u629b\u51faValueError\u5f02\u5e38\uff1b\u5982\u679clist\u4e2d\u5305\u542b\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u6709\u6548\u8f93\u5165\u7684\u6d88\u606f\uff1b\u5426\u5219\uff0c\u8fd4\u56de\u65e0\u6548\u8f93\u5165\u7684\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5224\u65adlist\u4e2d\u662f\u5426\u5305\u542b\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u4e14\u91cd\u8981\u7684\u9700\u6c42\u3002\u7ed3\u5408\u4f7f\u7528for\u5faa\u73af\u3001\u5185\u7f6e\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7f16\u5199\u51fa\u66f4\u52a0\u9ad8\u6548\u3001\u7b80\u6d01\u548c\u53ef\u8bfb\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5224\u65adlist\u91cc\u662f\u5426\u5305\u542b\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\u3001\u4f7f\u7528\u5185\u7f6e\u7684all()\u6216any()\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u66f4\u52a0\u9ad8\u6548\u3001\u7b80\u6d01\u548c\u53ef\u8bfb\u7684\u4ee3\u7801\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5224\u65adlist\u4e2d\u662f\u5426\u5305\u542b\u5b57\u7b26\u4e32\u7684\u9700\u6c42\u53ef\u80fd\u51fa\u73b0\u5728\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u5904\u7406\u3001\u7528\u6237\u8f93\u5165\u9a8c\u8bc1\u7b49\u573a\u666f\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u5217\u8868\u4e2d\u7684\u5143\u7d20\u7c7b\u578b\uff1f<\/strong><br \/>\u8981\u68c0\u67e5Python\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u5426\u4e3a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>isinstance()<\/code>\u51fd\u6570\u3002\u904d\u5386\u5217\u8868\u5e76\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;apple&#39;, 42, &#39;banana&#39;, 3.14]\nstring_elements = [item for item in my_list if isinstance(item, str)]\nprint(string_elements)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u6709\u6548\u5730\u7b5b\u9009\u51fa\u6240\u6709\u5b57\u7b26\u4e32\u5143\u7d20\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8fc7\u6ee4\u51fa\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>isinstance()<\/code>\u51fd\u6570\u6765\u8fc7\u6ee4\u51fa\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;apple&#39;, 42, &#39;banana&#39;, None]\nfiltered_strings = [item for item in my_list if isinstance(item, str)]\nprint(filtered_strings)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u7a7a\u5b57\u7b26\u4e32\uff0c\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5728\u68c0\u67e5\u5217\u8868\u5143\u7d20\u65f6\uff0c\u53ef\u4ee5\u540c\u65f6\u5224\u65ad\u5143\u7d20\u662f\u5426\u4e3a\u5b57\u7b26\u4e32\u5e76\u4e14\u4e0d\u4e3a\u7a7a\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;apple&#39;, &#39;&#39;, &#39;banana&#39;, None]\nnon_empty_strings = [item for item in my_list if isinstance(item, str) and item]\nprint(non_empty_strings)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u53ea\u83b7\u53d6\u975e\u7a7a\u7684\u5b57\u7b26\u4e32\u5143\u7d20\uff0c\u63d0\u9ad8\u6570\u636e\u7684\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5224\u65adlist\u91cc\u662f\u5426\u5305\u542b\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\u3001\u4f7f\u7528\u5185\u7f6e\u7684all( [&hellip;]","protected":false},"author":3,"featured_media":1118589,"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\/1118582"}],"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=1118582"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118582\/revisions"}],"predecessor-version":[{"id":1118590,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118582\/revisions\/1118590"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1118589"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1118582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1118582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1118582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}