{"id":1122629,"date":"2025-01-08T19:23:02","date_gmt":"2025-01-08T11:23:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1122629.html"},"modified":"2025-01-08T19:23:05","modified_gmt":"2025-01-08T11:23:05","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba%e6%95%b0%e7%bb%84%e6%9c%80%e5%90%8e%e4%b8%80%e5%88%97%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1122629.html","title":{"rendered":"python\u5982\u4f55\u53d6\u51fa\u6570\u7ec4\u6700\u540e\u4e00\u5217\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084412\/97e679ba-9cf6-434b-9f1c-b688534b1ce9.webp\" alt=\"python\u5982\u4f55\u53d6\u51fa\u6570\u7ec4\u6700\u540e\u4e00\u5217\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u53d6\u51fa\u6570\u7ec4\u6700\u540e\u4e00\u5217\u6570\u636e\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528\u5207\u7247\u3001\u7d22\u5f15\u548cNumPy\u5e93\u7b49\u3002<\/strong> \u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u5229\u7528Python\u7684\u5207\u7247\u548cNumPy\u5e93\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u5206\u4eab\u4e00\u4e9b\u5b9e\u7528\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>Python\u7684\u5217\u8868\u7d22\u5f15\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\uff0c\u901a\u8fc7\u7d22\u5f15\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u548c\u64cd\u4f5c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u8981\u53d6\u51fa\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u8d1f\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u7ec4<\/p>\n<p>array = [<\/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>\u53d6\u51fa\u6700\u540e\u4e00\u5217\u6570\u636e<\/strong><\/h2>\n<p>last_column = [row[-1] for row in array]<\/p>\n<p>print(last_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u6bcf\u4e00\u884c\uff0c\u5e76\u901a\u8fc7\u7d22\u5f15 <code>-1<\/code> \u53d6\u51fa\u6bcf\u884c\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u8fd9\u6837\u5c31\u5f97\u5230\u4e86\u6700\u540e\u4e00\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u7684\u5f3a\u5927\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002\u901a\u8fc7NumPy\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5927\u578b\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\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>array = np.array([<\/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>\u53d6\u51fa\u6700\u540e\u4e00\u5217\u6570\u636e<\/strong><\/h2>\n<p>last_column = array[:, -1]<\/p>\n<p>print(last_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u7684\u6570\u7ec4\u5207\u7247\u529f\u80fd\uff0c\u901a\u8fc7 <code>[:, -1]<\/code> \u53ef\u4ee5\u76f4\u63a5\u53d6\u51fa\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\u6570\u636e\u3002<code>:<\/code> \u8868\u793a\u9009\u53d6\u6240\u6709\u884c\uff0c<code>-1<\/code> \u8868\u793a\u9009\u53d6\u6bcf\u884c\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c\u7684\u6d41\u884c\u5e93\uff0c\u5c24\u5176\u5728\u5904\u7406\u8868\u683c\u6570\u636e\u65f6\u975e\u5e38\u65b9\u4fbf\u3002\u901a\u8fc7Pandas\u7684DataFrame\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u53d6\u51fa\u67d0\u4e00\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u7ec4<\/strong><\/h2>\n<p>array = [<\/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>\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(array)<\/p>\n<h2><strong>\u53d6\u51fa\u6700\u540e\u4e00\u5217\u6570\u636e<\/strong><\/h2>\n<p>last_column = df.iloc[:, -1]<\/p>\n<p>print(last_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5148\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aPandas\u7684DataFrame\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528 <code>iloc<\/code> \u65b9\u6cd5\u9009\u53d6\u6700\u540e\u4e00\u5217\u6570\u636e\u3002<code>iloc[:, -1]<\/code> \u8868\u793a\u9009\u53d6\u6240\u6709\u884c\u7684\u6700\u540e\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5217\u8868\u89e3\u5305<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u5305\u662f\u4e00\u79cdPython\u7279\u6027\uff0c\u53ef\u4ee5\u7528\u4e8e\u89e3\u5305\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u901a\u8fc7\u5217\u8868\u89e3\u5305\uff0c\u53ef\u4ee5\u7b80\u5316\u53d6\u51fa\u6700\u540e\u4e00\u5217\u6570\u636e\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u7ec4<\/p>\n<p>array = [<\/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>\u53d6\u51fa\u6700\u540e\u4e00\u5217\u6570\u636e<\/strong><\/h2>\n<p>_, _, last_column = zip(*array)<\/p>\n<p>print(last_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>zip(*array)<\/code> \u5c06\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u89e3\u5305\uff0c\u5f97\u5230\u6bcf\u4e00\u5217\u7684\u5143\u7d20\uff0c\u7136\u540e\u901a\u8fc7\u89e3\u5305\u64cd\u4f5c <code>_<\/code> \u5ffd\u7565\u524d\u4e24\u5217\uff0c\u76f4\u63a5\u53d6\u51fa\u6700\u540e\u4e00\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u901a\u8fc7\u5b57\u5178\u5217\u8868\u63d0\u53d6<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6570\u7ec4\u53ef\u80fd\u5b58\u50a8\u5728\u5b57\u5178\u5217\u8868\u4e2d\u3002\u901a\u8fc7\u904d\u5386\u5b57\u5178\u5217\u8868\uff0c\u53ef\u4ee5\u63d0\u53d6\u6307\u5b9a\u5b57\u6bb5\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5b57\u5178\u5217\u8868<\/p>\n<p>array = [<\/p>\n<p>    {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3},<\/p>\n<p>    {&#39;a&#39;: 4, &#39;b&#39;: 5, &#39;c&#39;: 6},<\/p>\n<p>    {&#39;a&#39;: 7, &#39;b&#39;: 8, &#39;c&#39;: 9}<\/p>\n<p>]<\/p>\n<h2><strong>\u53d6\u51fa\u6700\u540e\u4e00\u5217\u6570\u636e<\/strong><\/h2>\n<p>last_column = [d[&#39;c&#39;] for d in array]<\/p>\n<p>print(last_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6570\u7ec4\u4ee5\u5b57\u5178\u5217\u8868\u7684\u5f62\u5f0f\u5b58\u50a8\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u6bcf\u4e2a\u5b57\u5178\uff0c\u5e76\u63d0\u53d6\u952e\u4e3a <code>&#39;c&#39;<\/code> \u7684\u503c\uff0c\u5f97\u5230\u6700\u540e\u4e00\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u591a\u7ef4\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u591a\u7ef4\u6570\u7ec4\uff08\u4f8b\u5982\uff0c\u4e09\u7ef4\u6570\u7ec4\uff09\u3002\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u65f6\uff0c\u4f9d\u7136\u53ef\u4ee5\u4f7f\u7528NumPy\u7684\u5207\u7247\u529f\u80fd\u6765\u63d0\u53d6\u6307\u5b9a\u7ef4\u5ea6\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u4e09\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([<\/p>\n<p>    [<\/p>\n<p>        [1, 2, 3],<\/p>\n<p>        [4, 5, 6],<\/p>\n<p>        [7, 8, 9]<\/p>\n<p>    ],<\/p>\n<p>    [<\/p>\n<p>        [10, 11, 12],<\/p>\n<p>        [13, 14, 15],<\/p>\n<p>        [16, 17, 18]<\/p>\n<p>    ]<\/p>\n<p>])<\/p>\n<h2><strong>\u53d6\u51fa\u6bcf\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\u6570\u636e<\/strong><\/h2>\n<p>last_columns = array[:, :, -1]<\/p>\n<p>print(last_columns)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528NumPy\u7684\u4e09\u7ef4\u6570\u7ec4\u5207\u7247\u529f\u80fd\uff0c\u901a\u8fc7 <code>[:, :, -1]<\/code> \u53d6\u51fa\u6bcf\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\u6570\u636e\u3002<code>:<\/code> \u8868\u793a\u9009\u53d6\u6240\u6709\u5143\u7d20\uff0c<code>-1<\/code> \u8868\u793a\u9009\u53d6\u6bcf\u884c\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027\uff0c\u53ef\u4ee5\u5c06\u53d6\u51fa\u6570\u7ec4\u6700\u540e\u4e00\u5217\u6570\u636e\u7684\u64cd\u4f5c\u5c01\u88c5\u5230\u81ea\u5b9a\u4e49\u51fd\u6570\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def get_last_column(array):<\/p>\n<p>    return [row[-1] for row in array]<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u7ec4<\/strong><\/h2>\n<p>array = [<\/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>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>last_column = get_last_column(array)<\/p>\n<p>print(last_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>get_last_column<\/code> \u7684\u51fd\u6570\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u53d6\u51fa\u6bcf\u884c\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u4f20\u5165\u6570\u7ec4\u5373\u53ef\u5f97\u5230\u6700\u540e\u4e00\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u7a7a\u6570\u7ec4\u548c\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7a7a\u6570\u7ec4\u6216\u5f02\u5e38\u60c5\u51b5\u3002\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u65f6\uff0c\u9700\u8981\u8fdb\u884c\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u548c\u8fb9\u754c\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def get_last_column(array):<\/p>\n<p>    if not array or not array[0]:<\/p>\n<p>        return []<\/p>\n<p>    return [row[-1] for row in array]<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u7ec4<\/strong><\/h2>\n<p>empty_array = []<\/p>\n<p>array_with_empty_row = [[]]<\/p>\n<p>valid_array = [<\/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>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>print(get_last_column(empty_array))  # \u8f93\u51fa\uff1a[]<\/p>\n<p>print(get_last_column(array_with_empty_row))  # \u8f93\u51fa\uff1a[]<\/p>\n<p>print(get_last_column(valid_array))  # \u8f93\u51fa\uff1a[3, 6, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5bf9\u6570\u7ec4\u8fdb\u884c\u975e\u7a7a\u68c0\u67e5\uff0c\u5982\u679c\u6570\u7ec4\u4e3a\u7a7a\u6216\u7b2c\u4e00\u884c\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de\u7a7a\u5217\u8868\u3002\u5426\u5219\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53d6\u51fa\u6bcf\u884c\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5e94\u7528\u573a\u666f\u548c\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u53d6\u51fa\u6570\u7ec4\u6700\u540e\u4e00\u5217\u6570\u636e\u7684\u64cd\u4f5c\u5728\u6570\u636e\u5206\u6790\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\u975e\u5e38\u5e38\u89c1\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u6027\u80fd\u6bd4\u8f83\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5c0f\u578b\u6570\u7ec4\u65f6\uff0c\u5217\u8868\u7d22\u5f15\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u6027\u80fd\u5dee\u5f02\u4e0d\u5927\u3002\u7136\u800c\uff0c\u5728\u5904\u7406\u5927\u578b\u6570\u7ec4\u65f6\uff0cNumPy\u7684\u6027\u80fd\u663e\u8457\u4f18\u4e8e\u7eafPython\u7684\u65b9\u6cd5\u3002\u5bf9\u4e8e\u9700\u8981\u9891\u7e41\u8fdb\u884c\u6570\u7ec4\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u63a8\u8350\u4f7f\u7528NumPy\u5e93\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff08\u6027\u80fd\u6d4b\u8bd5\uff09\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import time<\/p>\n<h2><strong>\u751f\u6210\u5927\u578b\u6570\u7ec4<\/strong><\/h2>\n<p>large_array = np.random.rand(10000, 1000)<\/p>\n<h2><strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u65b9\u6cd5<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>last_column_list = [row[-1] for row in large_array]<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;\u5217\u8868\u63a8\u5bfc\u5f0f\u65b9\u6cd5\u8017\u65f6\uff1a&quot;, end_time - start_time)<\/p>\n<h2><strong>NumPy\u65b9\u6cd5<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>last_column_numpy = large_array[:, -1]<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;NumPy\u65b9\u6cd5\u8017\u65f6\uff1a&quot;, end_time - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u6027\u80fd\u6d4b\u8bd5\u793a\u4f8b\u4e2d\uff0c\u751f\u6210\u4e86\u4e00\u4e2a\u5927\u578b\u968f\u673a\u6570\u7ec4\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u548cNumPy\u65b9\u6cd5\u5206\u522b\u53d6\u51fa\u6700\u540e\u4e00\u5217\u6570\u636e\uff0c\u5e76\u6bd4\u8f83\u4e24\u79cd\u65b9\u6cd5\u7684\u8017\u65f6\u3002\u7ed3\u679c\u663e\u793a\uff0cNumPy\u65b9\u6cd5\u7684\u6027\u80fd\u663e\u8457\u4f18\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u4e86Python\u53d6\u51fa\u6570\u7ec4\u6700\u540e\u4e00\u5217\u6570\u636e\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u5217\u8868\u7d22\u5f15\u3001NumPy\u5e93\u3001Pandas\u5e93\u3001\u5217\u8868\u89e3\u5305\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u3001\u5904\u7406\u7a7a\u6570\u7ec4\u548c\u5f02\u5e38\u60c5\u51b5\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><p><strong>\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/strong> \u5bf9\u4e8e\u5927\u578b\u6570\u7ec4\u548c\u9891\u7e41\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u63a8\u8350\u4f7f\u7528NumPy\u5e93\u3002\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u8868\u683c\u6570\u636e\u7684\u573a\u666f\uff0cPandas\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002\u901a\u8fc7\u5408\u7406\u7684\u9519\u8bef\u5904\u7406\u548c\u8fb9\u754c\u68c0\u67e5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u7406\u89e3\u548c\u638c\u63e1Python\u53d6\u51fa\u6570\u7ec4\u6700\u540e\u4e00\u5217\u6570\u636e\u7684\u65b9\u6cd5\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u63d0\u53d6\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5904\u7406\u6570\u7ec4\u3002\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8f7b\u677e\u63d0\u53d6\u51fa\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u7ec4\narray = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n\n# \u63d0\u53d6\u6700\u540e\u4e00\u5217\nlast_column = array[:, -1]\nprint(last_column)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c<code>last_column<\/code>\u5c06\u5305\u542b\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\u6570\u636e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u4ee5\u63d0\u53d6\u6700\u540e\u4e00\u5217\uff1f<\/strong><br \/>\u591a\u7ef4\u6570\u7ec4\u7684\u64cd\u4f5c\u4e0e\u4e00\u7ef4\u6570\u7ec4\u7c7b\u4f3c\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7ef4\u5ea6\u7684\u5904\u7406\u3002\u4f7f\u7528NumPy\u7684\u5207\u7247\u529f\u80fd\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u8bbf\u95ee\u591a\u7ef4\u6570\u7ec4\u7684\u7279\u5b9a\u5217\u3002\u4e0a\u9762\u7684\u793a\u4f8b\u53ef\u4ee5\u6269\u5c55\u5230\u4efb\u610f\u7ef4\u5ea6\u7684\u6570\u7ec4\uff0c\u53ea\u9700\u8c03\u6574\u7d22\u5f15\u5373\u53ef\u3002<\/p>\n<p><strong>\u5982\u679c\u4e0d\u4f7f\u7528NumPy\uff0c\u600e\u6837\u5728Python\u4e2d\u63d0\u53d6\u5217\u8868\u7684\u6700\u540e\u4e00\u5217\uff1f<\/strong><br \/>\u5728\u6ca1\u6709NumPy\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u63d0\u53d6\u5217\u8868\u4e2d\u6bcf\u4e2a\u5b50\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\nlast_column = [row[-1] for row in data]\nprint(last_column)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>last_column<\/code>\u5c06\u5305\u542b\u6240\u6709\u5b50\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53d6\u51fa\u6570\u7ec4\u6700\u540e\u4e00\u5217\u6570\u636e\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528\u5207\u7247\u3001\u7d22\u5f15\u548cNumPy\u5e93\u7b49\u3002 \u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u5229\u7528Pyth [&hellip;]","protected":false},"author":3,"featured_media":1122635,"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\/1122629"}],"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=1122629"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122629\/revisions"}],"predecessor-version":[{"id":1122636,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122629\/revisions\/1122636"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1122635"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1122629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1122629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1122629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}