{"id":1112856,"date":"2025-01-08T17:42:25","date_gmt":"2025-01-08T09:42:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1112856.html"},"modified":"2025-01-08T17:42:27","modified_gmt":"2025-01-08T09:42:27","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%ad%9b%e9%80%89%e5%87%ba%e4%b8%ba%e7%a9%ba%e7%9a%84%e5%ba%8f%e5%88%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1112856.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074722\/9d6827fc-b610-4840-9013-c3495cb1c369.webp\" alt=\"python\u4e2d\u5982\u4f55\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u51fd\u6570\u7b49\u3002<strong>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001filter\u51fd\u6570\u548cpandas\u5e93\u7b49\u65b9\u6cd5\u6765\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217<\/strong>\uff0c\u5176\u4e2d\uff0c\u5217\u8868\u89e3\u6790\u65b9\u6cd5\u6700\u4e3a\u7b80\u6d01\u9ad8\u6548\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u4e00\u884c\u4ee3\u7801\u5185\u5bf9\u5217\u8868\u8fdb\u884c\u8fc7\u6ee4\u548c\u7b5b\u9009\u3002\u4e0b\u9762\u662f\u5177\u4f53\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>sequences = [[], [1, 2, 3], [], [4, 5], [], [6]]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217<\/strong><\/h2>\n<p>empty_sequences = [seq for seq in sequences if not seq]<\/p>\n<p>print(empty_sequences)  # \u8f93\u51fa: [[], [], []]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>empty_sequences<\/code>\u5c06\u5305\u542b\u6240\u6709\u4e3a\u7a7a\u7684\u5b50\u5217\u8868\u3002\u5217\u8868\u89e3\u6790\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u4e14\u6613\u4e8e\u9605\u8bfb\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528filter\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>filter<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u4ece\u5e8f\u5217\u4e2d\u8fc7\u6ee4\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002\u4f7f\u7528<code>filter<\/code>\u51fd\u6570\u7b5b\u9009\u4e3a\u7a7a\u7684\u5e8f\u5217\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>sequences = [[], [1, 2, 3], [], [4, 5], [], [6]]<\/p>\n<h2><strong>\u4f7f\u7528filter\u51fd\u6570\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217<\/strong><\/h2>\n<p>empty_sequences = list(filter(lambda x: not x, sequences))<\/p>\n<p>print(empty_sequences)  # \u8f93\u51fa: [[], [], []]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>filter<\/code>\u51fd\u6570\u7684\u4f18\u70b9\u662f\u5b83\u53ef\u4ee5\u4e0e\u5176\u4ed6\u51fd\u6570\u7ec4\u5408\u4f7f\u7528\uff0c\u4ee3\u7801\u66f4\u5177\u901a\u7528\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6b63\u5728\u5904\u7406\u7684\u662f\u4e00\u4e2a\u6570\u636e\u6846\uff08DataFrame\uff09\uff0c<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5<code>pandas<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528<code>pandas<\/code>\u5e93\u8fdb\u884c\u7b5b\u9009\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;col1&#39;: [[], [1, 2, 3], [], [4, 5], [], [6]]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217<\/strong><\/h2>\n<p>empty_sequences_df = df[df[&#39;col1&#39;].apply(lambda x: len(x) == 0)]<\/p>\n<p>print(empty_sequences_df)  # \u8f93\u51fa: \u7a7a\u5e8f\u5217\u7684\u884c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u7684\u4f18\u70b9\u662f\u5b83\u53ef\u4ee5\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p><code>NumPy<\/code>\u5e93\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u591a\u7ef4\u6570\u7ec4\u64cd\u4f5c\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5<code>NumPy<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528<code>NumPy<\/code>\u5e93\u8fdb\u884c\u7b5b\u9009\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u7ec4<\/strong><\/h2>\n<p>sequences = np.array([[], [1, 2, 3], [], [4, 5], [], [6]], dtype=object)<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217<\/strong><\/h2>\n<p>empty_sequences = np.array([seq for seq in sequences if len(seq) == 0])<\/p>\n<p>print(empty_sequences)  # \u8f93\u51fa: [[], [], []]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>NumPy<\/code>\u5e93\u7684\u4f18\u70b9\u662f\u5b83\u5728\u5904\u7406\u5927\u578b\u6570\u7ec4\u548c\u77e9\u9635\u65f6\u6027\u80fd\u4f18\u8d8a\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u89e3\u6790\u548c<code>pandas<\/code>\u5e93\u7ed3\u5408\u5904\u7406\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;col1&#39;: [[], [1, 2, 3], [], [4, 5], [], [6]], &#39;col2&#39;: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217<\/strong><\/h2>\n<p>empty_sequences_df = df[df[&#39;col1&#39;].apply(lambda x: len(x) == 0)]<\/p>\n<h2><strong>\u5904\u7406\u7a7a\u5e8f\u5217\u6570\u636e<\/strong><\/h2>\n<p>processed_data = empty_sequences_df[&#39;col2&#39;].tolist()<\/p>\n<p>print(processed_data)  # \u8f93\u51fa: [&#39;a&#39;, &#39;c&#39;, &#39;e&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u5217\u8868\u89e3\u6790\u548c<code>pandas<\/code>\u5e93\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u5d4c\u5957\u5e8f\u5217<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5e8f\u5217\u53ef\u80fd\u662f\u5d4c\u5957\u7684\uff0c\u9700\u8981\u9012\u5f52\u5730\u8fdb\u884c\u7b5b\u9009\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5904\u7406\u5d4c\u5957\u5e8f\u5217\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def filter_empty_sequences(seq):<\/p>\n<p>    if isinstance(seq, list):<\/p>\n<p>        return [filter_empty_sequences(subseq) for subseq in seq if subseq]<\/p>\n<p>    return seq<\/p>\n<h2><strong>\u793a\u4f8b\u5d4c\u5957\u5217\u8868<\/strong><\/h2>\n<p>nested_sequences = [[], [1, 2, []], [], [4, [5, []]], [], [6, []]]<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u5d4c\u5957\u5e8f\u5217\u4e2d\u7684\u7a7a\u5e8f\u5217<\/strong><\/h2>\n<p>filtered_nested_sequences = filter_empty_sequences(nested_sequences)<\/p>\n<p>print(filtered_nested_sequences)  # \u8f93\u51fa: [[1, 2], [4, [5]], [6]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>filter_empty_sequences<\/code>\u51fd\u6570\u9012\u5f52\u5730\u7b5b\u9009\u51fa\u5d4c\u5957\u5e8f\u5217\u4e2d\u7684\u7a7a\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217\uff0c\u5305\u62ec\u5217\u8868\u89e3\u6790\u3001<code>filter<\/code>\u51fd\u6570\u3001<code>pandas<\/code>\u5e93\u548c<code>NumPy<\/code>\u5e93\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u7b80\u5355\u573a\u666f\uff0c\u5217\u8868\u89e3\u6790\u65b9\u6cd5\u662f\u6700\u4e3a\u7b80\u6d01\u9ad8\u6548\u7684\u9009\u62e9\uff1b\u5bf9\u4e8e\u590d\u6742\u6570\u636e\u5904\u7406\u4efb\u52a1\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>pandas<\/code>\u6216<code>NumPy<\/code>\u5e93\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u9ad8\u6548\u5730\u7b5b\u9009\u548c\u5904\u7406\u6570\u636e\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u5e8f\u5217\u662f\u5426\u4e3a\u7a7a\uff1f<\/strong><br \/>\u5224\u65ad\u4e00\u4e2a\u5e8f\u5217\u662f\u5426\u4e3a\u7a7a\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u3002\u5bf9\u4e8e\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\u5e8f\u5217\u7c7b\u578b\uff0c\u5982\u679c\u5e8f\u5217\u4e3a\u7a7a\uff0c\u6761\u4ef6\u5224\u65ad\u4f1a\u8fd4\u56deFalse\u3002\u4f8b\u5982\uff0c<code>if not my_list:<\/code>\u53ef\u4ee5\u5224\u65ad<code>my_list<\/code>\u662f\u5426\u4e3a\u7a7a\uff0c\u82e5\u4e3a\u7a7a\u5219\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8fc7\u6ee4\u51fd\u6570\u6765\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217\u3002\u6bd4\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5e8f\u5217\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u63d0\u53d6\u51fa\u6240\u6709\u4e3a\u7a7a\u7684\u5e8f\u5217\uff0c\u5982\uff1a<code>empty_sequences = [s for s in sequences if not s]<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u5feb\u901f\u83b7\u5f97\u6240\u6709\u7a7a\u5e8f\u5217\u7684\u96c6\u5408\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5904\u7406\u7a7a\u5e8f\u5217\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u7a7a\u5e8f\u5217\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u6216\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u6570\u636e\u5904\u7406\u7684\u521d\u59cb\u9636\u6bb5\uff0c\u5bf9\u5e8f\u5217\u8fdb\u884c\u6e05\u7406\uff0c\u79fb\u9664\u6216\u66ff\u6362\u7a7a\u5e8f\u5217\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>filter(None, sequences)<\/code>\u53ef\u4ee5\u6709\u6548\u5730\u79fb\u9664\u6240\u6709\u7a7a\u5e8f\u5217\u3002\u540c\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u8bb0\u5f55\u7a7a\u5e8f\u5217\u7684\u4e2a\u6570\uff0c\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\u6570\u636e\u8d28\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7b5b\u9009\u51fa\u4e3a\u7a7a\u7684\u5e8f\u5217\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u51fd\u6570\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217 [&hellip;]","protected":false},"author":3,"featured_media":1112865,"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\/1112856"}],"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=1112856"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112856\/revisions"}],"predecessor-version":[{"id":1112867,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112856\/revisions\/1112867"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1112865"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1112856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1112856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1112856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}