{"id":1133002,"date":"2025-01-08T21:02:26","date_gmt":"2025-01-08T13:02:26","guid":{"rendered":""},"modified":"2025-01-08T21:02:29","modified_gmt":"2025-01-08T13:02:29","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e5%88%97%e8%a1%a8%e7%9a%84%e5%88%97%e6%8f%90%e5%8f%96%e5%87%ba%e6%9d%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1133002.html","title":{"rendered":"python\u5982\u4f55\u628a\u5217\u8868\u7684\u5217\u63d0\u53d6\u51fa\u6765"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102351\/d42c20f5-858d-42a8-bf1f-7ca46d60e70e.webp\" alt=\"python\u5982\u4f55\u628a\u5217\u8868\u7684\u5217\u63d0\u53d6\u51fa\u6765\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8981\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u63d0\u53d6\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\u5217\u8868\u63a8\u5bfc\u3001<code>zip()<\/code>\u51fd\u6570\u3001NumPy\u5e93\u7b49\u3002<\/strong> <strong>\u5176\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u548c<code>zip()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\uff0c\u6613\u4e8e\u7406\u89e3\u4e14\u4e0d\u9700\u8981\u989d\u5916\u7684\u5e93\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5217\u8868\u63a8\u5bfc<\/strong>\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5217\u8868\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u539f\u59cb\u5217\u8868\u7684\u6bcf\u4e00\u884c\uff0c\u63d0\u53d6\u51fa\u4f60\u9700\u8981\u7684\u5217\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff08\u5373\u5217\u8868\u7684\u5217\u8868\uff09\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u6765\u63d0\u53d6\u7279\u5b9a\u7684\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u5217\u8868\u63a8\u5bfc<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5217\u8868\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u539f\u59cb\u5217\u8868\u7684\u6bcf\u4e00\u884c\uff0c\u63d0\u53d6\u51fa\u4f60\u9700\u8981\u7684\u5217\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff08\u5373\u5217\u8868\u7684\u5217\u8868\uff09\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u6765\u63d0\u53d6\u7279\u5b9a\u7684\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>data = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c\u4e8c\u5217<\/strong><\/h2>\n<p>column = [row[1] for row in data]<\/p>\n<p>print(column)  # \u8f93\u51fa: [2, 5, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>zip()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u540c\u65f6\u904d\u5386\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u5bf9\u5e94\u5143\u7d20\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\u3002\u4f60\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\u6765\u63d0\u53d6\u5217\u8868\u7684\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>data = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528 zip() \u63d0\u53d6\u5217<\/strong><\/h2>\n<p>columns = list(zip(*data))<\/p>\n<p>print(columns)  # \u8f93\u51fa: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c\u4e8c\u5217<\/strong><\/h2>\n<p>second_column = columns[1]<\/p>\n<p>print(second_column)  # \u8f93\u51fa: (2, 5, 8)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u6570\u7ec4\u64cd\u4f5c\u7684\u51fd\u6570\u3002\u4f7f\u7528NumPy\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u63d0\u53d6\u5217\u8868\u7684\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a NumPy \u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array(data)<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c\u4e8c\u5217<\/strong><\/h2>\n<p>second_column = array[:, 1]<\/p>\n<p>print(second_column)  # \u8f93\u51fa: [2 5 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e00\u3001\u5217\u8868\u63a8\u5bfc<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5c24\u5176\u5728\u5904\u7406\u7b80\u5355\u7684\u6570\u636e\u63d0\u53d6\u548c\u8f6c\u6362\u4efb\u52a1\u65f6\u3002\u5b83\u4e0d\u4ec5\u8bed\u6cd5\u7b80\u6d01\uff0c\u800c\u4e14\u6267\u884c\u901f\u5ea6\u901a\u5e38\u4e5f\u8f83\u5feb\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\u4e2d\u63d0\u53d6\u51fa\u67d0\u4e00\u5217\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u82e5\u5e72\u5b50\u5217\u8868\u7684\u4e8c\u7ef4\u5217\u8868\uff0c\u6bcf\u4e2a\u5b50\u5217\u8868\u8868\u793a\u4e00\u884c\u6570\u636e\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u6765\u63d0\u53d6\u67d0\u4e00\u5217\u7684\u6240\u6709\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>data = [<\/p>\n<p>    [1, &#39;a&#39;, 3.5],<\/p>\n<p>    [2, &#39;b&#39;, 4.5],<\/p>\n<p>    [3, &#39;c&#39;, 5.5]<\/p>\n<p>]<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c\u4e8c\u5217\uff08\u7d22\u5f15\u4e3a1\uff09<\/strong><\/h2>\n<p>second_column = [row[1] for row in data]<\/p>\n<p>print(second_column)  # \u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u975e\u5e38\u76f4\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002\u4f60\u53ea\u9700\u904d\u5386\u6bcf\u4e00\u884c\uff0c\u63d0\u53d6\u51fa\u4f60\u611f\u5174\u8da3\u7684\u90a3\u4e00\u5217\u7684\u6570\u636e\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h3>\u5904\u7406\u4e0d\u89c4\u5219\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u4e0d\u89c4\u5219\u7684\u5217\u8868\uff0c\u5373\u67d0\u4e9b\u5b50\u5217\u8868\u7684\u957f\u5ea6\u4e0d\u4e00\u81f4\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7d22\u5f15\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u4f60\u9700\u8981\u6dfb\u52a0\u4e00\u4e9b\u989d\u5916\u7684\u903b\u8f91\u6765\u5904\u7406\u8fd9\u4e9b\u4e0d\u89c4\u5219\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868\uff08\u5305\u542b\u4e0d\u89c4\u5219\u5b50\u5217\u8868\uff09<\/p>\n<p>data = [<\/p>\n<p>    [1, &#39;a&#39;, 3.5],<\/p>\n<p>    [2, &#39;b&#39;],<\/p>\n<p>    [3, &#39;c&#39;, 5.5]<\/p>\n<p>]<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c\u4e8c\u5217\uff08\u7d22\u5f15\u4e3a1\uff09\uff0c\u5e76\u5904\u7406\u4e0d\u89c4\u5219\u60c5\u51b5<\/strong><\/h2>\n<p>second_column = [row[1] for row in data if len(row) &gt; 1]<\/p>\n<p>print(second_column)  # \u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u6dfb\u52a0<code>if len(row) &gt; 1<\/code>\u8fd9\u4e2a\u6761\u4ef6\uff0c\u786e\u4fdd\u53ea\u6709\u90a3\u4e9b\u957f\u5ea6\u5927\u4e8e1\u7684\u5b50\u5217\u8868\u624d\u4f1a\u88ab\u5904\u7406\uff0c\u4ece\u800c\u907f\u514d\u4e86\u7d22\u5f15\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>zip()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u7684\u5bf9\u5e94\u5143\u7d20\u6253\u5305\u6210\u4e00\u4e2a\u4e2a\u5143\u7ec4\uff0c\u4ece\u800c\u5b9e\u73b0\u5e76\u884c\u8fed\u4ee3\u3002\u5229\u7528\u8fd9\u4e00\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u63d0\u53d6\u4e8c\u7ef4\u5217\u8868\u7684\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u4e8c\u7ef4\u5217\u8868\u89e3\u5305\u4f20\u9012\u7ed9<code>zip()<\/code>\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u5c06\u5176\u6309\u5217\u8fdb\u884c\u91cd\u65b0\u7ec4\u7ec7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>data = [<\/p>\n<p>    [1, &#39;a&#39;, 3.5],<\/p>\n<p>    [2, &#39;b&#39;, 4.5],<\/p>\n<p>    [3, &#39;c&#39;, 5.5]<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528 zip() \u63d0\u53d6\u5217<\/strong><\/h2>\n<p>columns = list(zip(*data))<\/p>\n<p>print(columns)  # \u8f93\u51fa: [(1, 2, 3), (&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (3.5, 4.5, 5.5)]<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c\u4e8c\u5217<\/strong><\/h2>\n<p>second_column = columns[1]<\/p>\n<p>print(second_column)  # \u8f93\u51fa: (&#39;a&#39;, &#39;b&#39;, &#39;c&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5904\u7406\u4e0d\u89c4\u5219\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u4e0e\u5217\u8868\u63a8\u5bfc\u7c7b\u4f3c\uff0c<code>zip()<\/code>\u51fd\u6570\u5728\u5904\u7406\u4e0d\u89c4\u5219\u5217\u8868\u65f6\u4e5f\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u903b\u8f91\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>zip()<\/code>\u4f1a\u5ffd\u7565\u6389\u957f\u5ea6\u4e0d\u4e00\u81f4\u7684\u5b50\u5217\u8868\u4e2d\u7684\u591a\u4f59\u5143\u7d20\uff0c\u56e0\u6b64\u4f60\u9700\u8981\u786e\u4fdd\u6240\u6709\u5b50\u5217\u8868\u7684\u957f\u5ea6\u4e00\u81f4\uff0c\u6216\u8005\u4f7f\u7528\u4e00\u4e9b\u5176\u4ed6\u7684\u624b\u6bb5\u6765\u5904\u7406\u4e0d\u89c4\u5219\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868\uff08\u5305\u542b\u4e0d\u89c4\u5219\u5b50\u5217\u8868\uff09<\/p>\n<p>data = [<\/p>\n<p>    [1, &#39;a&#39;, 3.5],<\/p>\n<p>    [2, &#39;b&#39;],<\/p>\n<p>    [3, &#39;c&#39;, 5.5]<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528 zip_longest \u5904\u7406\u4e0d\u89c4\u5219\u5217\u8868<\/strong><\/h2>\n<p>from itertools import zip_longest<\/p>\n<p>columns = list(zip_longest(*data, fillvalue=None))<\/p>\n<p>print(columns)  # \u8f93\u51fa: [(1, 2, 3), (&#39;a&#39;, &#39;b&#39;, &#39;c&#39;), (3.5, None, 5.5)]<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c\u4e8c\u5217\uff08\u6ce8\u610f\u5904\u7406 None \u503c\uff09<\/strong><\/h2>\n<p>second_column = [item for item in columns[1] if item is not None]<\/p>\n<p>print(second_column)  # \u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>zip_longest<\/code>\u51fd\u6570\u4f1a\u586b\u5145\u8f83\u77ed\u7684\u5b50\u5217\u8868\uff0c\u4f7f\u5176\u957f\u5ea6\u4e00\u81f4\uff0c\u4ece\u800c\u907f\u514d\u6570\u636e\u4e22\u5931\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528NumPy\u5e93<\/h2>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u548c\u591a\u7ef4\u6570\u7ec4\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u7b80\u5316\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5NumPy<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5NumPy\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5c06\u4e8c\u7ef4\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u5207\u7247\u7684\u65b9\u5f0f\u8f7b\u677e\u63d0\u53d6\u67d0\u4e00\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [1, &#39;a&#39;, 3.5],<\/p>\n<p>    [2, &#39;b&#39;, 4.5],<\/p>\n<p>    [3, &#39;c&#39;, 5.5]<\/p>\n<p>]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a NumPy \u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array(data, dtype=object)<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c\u4e8c\u5217<\/strong><\/h2>\n<p>second_column = array[:, 1]<\/p>\n<p>print(second_column)  # \u8f93\u51fa: [&#39;a&#39; &#39;b&#39; &#39;c&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5904\u7406\u4e0d\u89c4\u5219\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u4e0e\u5217\u8868\u63a8\u5bfc\u548c<code>zip()<\/code>\u51fd\u6570\u4e0d\u540c\uff0cNumPy\u8981\u6c42\u6570\u7ec4\u7684\u5f62\u72b6\u662f\u89c4\u5219\u7684\u3002\u56e0\u6b64\uff0c\u5728\u5904\u7406\u4e0d\u89c4\u5219\u5217\u8868\u65f6\uff0c\u4f60\u9700\u8981\u9996\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u89c4\u5219\u5f62\u72b6\uff0c\u6bd4\u5982\u901a\u8fc7\u586b\u5145\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868\uff08\u5305\u542b\u4e0d\u89c4\u5219\u5b50\u5217\u8868\uff09<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [1, &#39;a&#39;, 3.5],<\/p>\n<p>    [2, &#39;b&#39;],<\/p>\n<p>    [3, &#39;c&#39;, 5.5]<\/p>\n<p>]<\/p>\n<h2><strong>\u627e\u5230\u6700\u957f\u5b50\u5217\u8868\u7684\u957f\u5ea6<\/strong><\/h2>\n<p>max_length = max(len(row) for row in data)<\/p>\n<h2><strong>\u586b\u5145\u7f3a\u5931\u503c\uff0c\u4f7f\u6bcf\u4e2a\u5b50\u5217\u8868\u957f\u5ea6\u4e00\u81f4<\/strong><\/h2>\n<p>filled_data = [row + [None] * (max_length - len(row)) for row in data]<\/p>\n<h2><strong>\u5c06\u586b\u5145\u540e\u7684\u5217\u8868\u8f6c\u6362\u4e3a NumPy \u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array(filled_data, dtype=object)<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c\u4e8c\u5217\uff0c\u5e76\u5904\u7406 None \u503c<\/strong><\/h2>\n<p>second_column = [item for item in array[:, 1] if item is not None]<\/p>\n<p>print(second_column)  # \u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u586b\u5145\u7f3a\u5931\u503c\uff0c\u4f7f\u5f97\u6bcf\u4e2a\u5b50\u5217\u8868\u7684\u957f\u5ea6\u4e00\u81f4\uff0c\u4ece\u800c\u80fd\u591f\u4f7f\u7528NumPy\u6570\u7ec4\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001Pandas\u5e93\u7684\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>Pandas\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u6e05\u6d17\u4efb\u52a1\u3002Pandas\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u4e8eNumPy\u7684\u9ad8\u6548\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u64cd\u4f5c\u51fd\u6570\uff0c\u4f46\u5b83\u7684\u529f\u80fd\u66f4\u52a0\u4e30\u5bcc\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5Pandas<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5Pandas\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5c06\u4e8c\u7ef4\u5217\u8868\u8f6c\u6362\u4e3aPandas\u7684DataFrame\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u540d\u6216\u7d22\u5f15\u6765\u63d0\u53d6\u67d0\u4e00\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [1, &#39;a&#39;, 3.5],<\/p>\n<p>    [2, &#39;b&#39;, 4.5],<\/p>\n<p>    [3, &#39;c&#39;, 5.5]<\/p>\n<p>]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a DataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data, columns=[&#39;col1&#39;, &#39;col2&#39;, &#39;col3&#39;])<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c\u4e8c\u5217<\/strong><\/h2>\n<p>second_column = df[&#39;col2&#39;]<\/p>\n<p>print(second_column)  # \u8f93\u51fa: 0    a<\/p>\n<p>                      #      1    b<\/p>\n<p>                      #      2    c<\/p>\n<p>                      #      Name: col2, dtype: object<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5904\u7406\u4e0d\u89c4\u5219\u5217\u8868<\/h3>\n<\/p>\n<p><p>Pandas\u5728\u5904\u7406\u4e0d\u89c4\u5219\u5217\u8868\u65f6\u4e5f\u975e\u5e38\u65b9\u4fbf\u3002\u4f60\u53ef\u4ee5\u5728\u521b\u5efaDataFrame\u65f6\u6307\u5b9a\u7f3a\u5931\u503c\u7684\u586b\u5145\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868\uff08\u5305\u542b\u4e0d\u89c4\u5219\u5b50\u5217\u8868\uff09<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [1, &#39;a&#39;, 3.5],<\/p>\n<p>    [2, &#39;b&#39;],<\/p>\n<p>    [3, &#39;c&#39;, 5.5]<\/p>\n<p>]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a DataFrame\uff0c\u5e76\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df = pd.DataFrame(data, columns=[&#39;col1&#39;, &#39;col2&#39;, &#39;col3&#39;]).fillna(value=np.nan)<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c\u4e8c\u5217\uff0c\u5e76\u5904\u7406 NaN \u503c<\/strong><\/h2>\n<p>second_column = df[&#39;col2&#39;].dropna()<\/p>\n<p>print(second_column)  # \u8f93\u51fa: 0    a<\/p>\n<p>                      #      1    b<\/p>\n<p>                      #      2    c<\/p>\n<p>                      #      Name: col2, dtype: object<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>dropna()<\/code>\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u53bb\u9664\u7f3a\u5931\u503c\uff0c\u4ece\u800c\u5f97\u5230\u5e72\u51c0\u7684\u6570\u636e\u5217\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4ece\u5217\u8868\u4e2d\u63d0\u53d6\u5217\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u5217\u8868\u63a8\u5bfc<\/strong>\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u4e8c\u7ef4\u5217\u8868\uff0c\u8bed\u6cd5\u7b80\u6d01\u76f4\u89c2\uff1b<strong><code>zip()<\/code>\u51fd\u6570<\/strong>\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5e76\u884c\u6570\u636e\uff0c\u5e76\u4e14\u53ef\u4ee5\u5904\u7406\u4e0d\u89c4\u5219\u7684\u5b50\u5217\u8868\uff1b<strong>NumPy<\/strong>\u5219\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u548c\u591a\u7ef4\u6570\u7ec4\u64cd\u4f5c\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff1b<strong>Pandas<\/strong>\u5219\u662f\u6570\u636e\u5206\u6790\u548c\u6e05\u6d17\u7684\u5229\u5668\uff0c\u529f\u80fd\u4e30\u5bcc\u4e14\u6613\u4e8e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5982\u679c\u6570\u636e\u91cf\u8f83\u5c0f\u4e14\u7ed3\u6784\u7b80\u5355\uff0c\u5217\u8868\u63a8\u5bfc\u6216<code>zip()<\/code>\u51fd\u6570\u53ef\u80fd\u662f\u6700\u4f73\u9009\u62e9\uff1b\u5982\u679c\u9700\u8981\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u6216\u8fdb\u884c\u590d\u6742\u7684\u6570\u7ec4\u64cd\u4f5c\uff0cNumPy\u4f1a\u66f4\u4e3a\u5408\u9002\uff1b\u800c\u5982\u679c\u4f60\u9700\u8981\u8fdb\u884c\u6570\u636e\u5206\u6790\u6216\u6570\u636e\u6e05\u6d17\u4efb\u52a1\uff0cPandas\u65e0\u7591\u662f\u6700\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u7406\u89e3\u5176\u57fa\u672c\u539f\u7406\u548c\u9002\u7528\u573a\u666f\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7075\u6d3b\u8fd0\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u5217\u8868\u7684\u7279\u5b9a\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6216NumPy\u5e93\u6765\u63d0\u53d6\u7279\u5b9a\u5217\u3002\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff08\u5373\u5217\u8868\u7684\u5217\u8868\uff09\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u6bcf\u4e00\u5217\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u89e3\u6790\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u63d0\u53d6\u7b2c\u4e00\u5217\uff1a<code>[row[0] for row in your_list]<\/code>\u3002\u5982\u679c\u4f7f\u7528NumPy\uff0c\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528\u5207\u7247\u63d0\u53d6\u5217\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u54ea\u4e9b\u65b9\u6cd5\u66f4\u9ad8\u6548\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0c\u4f7f\u7528NumPy\u901a\u5e38\u66f4\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5e95\u5c42\u7684C\u5b9e\u73b0\uff0c\u53ef\u4ee5\u66f4\u5feb\u5730\u5904\u7406\u6570\u7ec4\u64cd\u4f5c\u3002NumPy\u7684\u6570\u7ec4\u4e0d\u4ec5\u8282\u7701\u5185\u5b58\uff0c\u8fd8\u80fd\u5229\u7528\u5411\u91cf\u5316\u64cd\u4f5c\u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\u3002\u5bf9\u4e8e\u5c0f\u578b\u6570\u636e\u96c6\uff0c\u4f7f\u7528\u6807\u51c6\u5217\u8868\u548c\u5217\u8868\u89e3\u6790\u53ef\u80fd\u66f4\u7b80\u5355\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5217\u8868\u4e2d\u7f3a\u5931\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5728\u63d0\u53d6\u5217\u65f6\uff0c\u5982\u679c\u5217\u8868\u4e2d\u5b58\u5728\u7f3a\u5931\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u6765\u5904\u7406\u3002\u6bd4\u5982\uff0c\u5728\u5217\u8868\u89e3\u6790\u4e2d\uff0c\u53ef\u4ee5\u589e\u52a0\u6761\u4ef6\u6765\u8fc7\u6ee4\u6389\u7f3a\u5931\u503c\uff0c\u6216\u8005\u4f7f\u7528NumPy\u7684<code>numpy.nan<\/code>\u6765\u8868\u793a\u7f3a\u5931\u6570\u636e\u3002\u6b64\u5916\uff0c\u8003\u8651\u4f7f\u7528Pandas\u5e93\uff0c\u5b83\u4e13\u95e8\u5904\u7406\u7f3a\u5931\u6570\u636e\uff0c\u63d0\u4f9b\u4e86\u66f4\u4fbf\u6377\u7684\u65b9\u6cd5\u6765\u586b\u5145\u6216\u5220\u9664\u7f3a\u5931\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8981\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u63d0\u53d6\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\u5217\u8868\u63a8\u5bfc\u3001zip()\u51fd\u6570\u3001NumPy\u5e93\u7b49\u3002 \u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1133015,"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\/1133002"}],"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=1133002"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133002\/revisions"}],"predecessor-version":[{"id":1133017,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133002\/revisions\/1133017"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1133015"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1133002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1133002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1133002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}