{"id":1133035,"date":"2025-01-08T21:02:44","date_gmt":"2025-01-08T13:02:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133035.html"},"modified":"2025-01-08T21:02:46","modified_gmt":"2025-01-08T13:02:46","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%af%b9%e4%b8%80%e7%bb%84%e6%95%b0%e7%bb%84%e8%bf%9b%e8%a1%8c%e5%88%87%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133035.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5bf9\u4e00\u7ec4\u6570\u7ec4\u8fdb\u884c\u5207\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102405\/8a4e236d-f461-4fcc-9f7c-7bb215e6bcdd.webp\" alt=\"python\u4e2d\u5982\u4f55\u5bf9\u4e00\u7ec4\u6570\u7ec4\u8fdb\u884c\u5207\u7247\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5bf9\u4e00\u7ec4\u6570\u7ec4\u8fdb\u884c\u5207\u7247\u7684\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u7d22\u5f15\u3001\u6b65\u957f\u3001\u8d1f\u7d22\u5f15<\/strong>\u3002\u5207\u7247\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u5b50\u6570\u7ec4\u3002\u5728Python\u4e2d\uff0c\u5207\u7247\u64cd\u4f5c\u5bf9\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u548c\u590d\u6742\u7684\u6570\u7ec4\u64cd\u4f5c\u5c24\u4e3a\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u5bf9\u5176\u4e2d\u4e00\u70b9\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u7d22\u5f15<\/strong>\uff1a\u5728Python\u4e2d\uff0c\u6570\u7ec4\u7684\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5f00\u59cb\u548c\u7ed3\u675f\u7684\u7d22\u5f15\u6765\u83b7\u53d6\u4e00\u4e2a\u5b50\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u6570\u7ec4<code>arr[2:5]<\/code>\u4f1a\u8fd4\u56de\u4ece\u7d22\u5f152\u5230\u7d22\u5f154\u7684\u5143\u7d20\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7ed3\u675f\u7d22\u5f15\u662f\u4e0d\u5305\u542b\u5728\u5207\u7247\u7ed3\u679c\u4e2d\u7684\u3002<\/p>\n<\/p>\n<p><p>\u5207\u7247\u4e0d\u4ec5\u4ec5\u9650\u4e8e\u4e00\u7ef4\u6570\u7ec4\uff0c\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\u4e5f\u540c\u6837\u9002\u7528\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u7d22\u5f15\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u5207\u7247\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h4>1. \u5355\u7ef4\u6570\u7ec4\u5207\u7247<\/h4>\n<\/p>\n<p><p>\u5355\u7ef4\u6570\u7ec4\u5207\u7247\u662f\u6700\u57fa\u672c\u7684\u5207\u7247\u64cd\u4f5c\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5f00\u59cb\u548c\u7ed3\u675f\u7d22\u5f15\u6765\u83b7\u53d6\u4e00\u4e2a\u5b50\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])<\/p>\n<h2><strong>\u83b7\u53d6\u4ece\u7d22\u5f152\u5230\u7d22\u5f155\u7684\u5b50\u6570\u7ec4<\/strong><\/h2>\n<p>sub_arr = arr[2:6]<\/p>\n<p>print(sub_arr)  # \u8f93\u51fa: [3 4 5 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b9\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528\u7d22\u5f152\u5230\u7d22\u5f155\uff08\u4e0d\u5305\u62ec5\uff09\u6765\u83b7\u53d6\u5b50\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>2. \u591a\u7ef4\u6570\u7ec4\u5207\u7247<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u5207\u7247\u64cd\u4f5c\u66f4\u52a0\u590d\u6742\uff0c\u4f46\u4e5f\u66f4\u52a0\u7075\u6d3b\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u7279\u5b9a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a2D\u6570\u7ec4<\/p>\n<p>arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u83b7\u53d6\u7b2c\u4e8c\u884c<\/strong><\/h2>\n<p>second_row = arr_2d[1, :]<\/p>\n<p>print(second_row)  # \u8f93\u51fa: [4 5 6]<\/p>\n<h2><strong>\u83b7\u53d6\u7b2c\u4e8c\u5217<\/strong><\/h2>\n<p>second_col = arr_2d[:, 1]<\/p>\n<p>print(second_col)  # \u8f93\u51fa: [2 5 8]<\/p>\n<h2><strong>\u83b7\u53d6\u4e00\u4e2a\u5b50\u77e9\u9635<\/strong><\/h2>\n<p>sub_matrix = arr_2d[0:2, 0:2]<\/p>\n<p>print(sub_matrix)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>[[1 2]<\/strong><\/h2>\n<h2><strong> [4 5]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5e76\u6f14\u793a\u4e86\u5982\u4f55\u83b7\u53d6\u7279\u5b9a\u7684\u884c\u3001\u5217\u548c\u5b50\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6b65\u957f<\/h3>\n<\/p>\n<p><p>\u6b65\u957f\u662f\u5207\u7247\u64cd\u4f5c\u4e2d\u53e6\u4e00\u4e2a\u91cd\u8981\u7684\u53c2\u6570\u3002\u5b83\u5141\u8bb8\u4f60\u4ee5\u4e00\u5b9a\u7684\u95f4\u9694\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1. \u5355\u7ef4\u6570\u7ec4\u6b65\u957f\u5207\u7247<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6b65\u957f\u6765\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u83b7\u53d6\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/p>\n<p>arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])<\/p>\n<h2><strong>\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u83b7\u53d6\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>sub_arr = arr[::2]<\/p>\n<p>print(sub_arr)  # \u8f93\u51fa: [1 3 5 7 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b65\u957f2\u6765\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>2. \u591a\u7ef4\u6570\u7ec4\u6b65\u957f\u5207\u7247<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6b65\u957f\u6765\u83b7\u53d6\u7279\u5b9a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a2D\u6570\u7ec4<\/p>\n<p>arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u6bcf\u9694\u4e00\u884c\u548c\u4e00\u5217\u83b7\u53d6\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>sub_matrix = arr_2d[::2, ::2]<\/p>\n<p>print(sub_matrix)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>[[1 3]<\/strong><\/h2>\n<h2><strong> [7 9]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b65\u957f2\u6765\u83b7\u53d6\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u6bcf\u9694\u4e00\u884c\u548c\u4e00\u5217\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u8d1f\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u8d1f\u7d22\u5f15\u662f\u5207\u7247\u64cd\u4f5c\u4e2d\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u4f60\u4ece\u6570\u7ec4\u7684\u672b\u5c3e\u5f00\u59cb\u83b7\u53d6\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1. \u5355\u7ef4\u6570\u7ec4\u8d1f\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u8d1f\u7d22\u5f15\u6765\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u6700\u540e\u51e0\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/p>\n<p>arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])<\/p>\n<h2><strong>\u83b7\u53d6\u6700\u540e\u4e09\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>sub_arr = arr[-3:]<\/p>\n<p>print(sub_arr)  # \u8f93\u51fa: [7 8 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u8d1f\u7d22\u5f15-3\u6765\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u6700\u540e\u4e09\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>2. \u591a\u7ef4\u6570\u7ec4\u8d1f\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u8d1f\u7d22\u5f15\u540c\u6837\u9002\u7528\u3002\u4f60\u53ef\u4ee5\u4ece\u6570\u7ec4\u7684\u672b\u5c3e\u5f00\u59cb\u83b7\u53d6\u7279\u5b9a\u7684\u884c\u548c\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a2D\u6570\u7ec4<\/p>\n<p>arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u83b7\u53d6\u6700\u540e\u4e00\u884c<\/strong><\/h2>\n<p>last_row = arr_2d[-1, :]<\/p>\n<p>print(last_row)  # \u8f93\u51fa: [7 8 9]<\/p>\n<h2><strong>\u83b7\u53d6\u6700\u540e\u4e00\u5217<\/strong><\/h2>\n<p>last_col = arr_2d[:, -1]<\/p>\n<p>print(last_col)  # \u8f93\u51fa: [3 6 9]<\/p>\n<h2><strong>\u83b7\u53d6\u4e00\u4e2a\u5b50\u77e9\u9635<\/strong><\/h2>\n<p>sub_matrix = arr_2d[-2:, -2:]<\/p>\n<p>print(sub_matrix)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>[[5 6]<\/strong><\/h2>\n<h2><strong> [8 9]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u8d1f\u7d22\u5f15\u6765\u83b7\u53d6\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u6700\u540e\u4e00\u884c\u3001\u6700\u540e\u4e00\u5217\u548c\u4e00\u4e2a\u5b50\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u4f7f\u7528\u7d22\u5f15\u3001\u6b65\u957f\u548c\u8d1f\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u7ecf\u5e38\u9700\u8981\u7ed3\u5408\u4f7f\u7528\u7d22\u5f15\u3001\u6b65\u957f\u548c\u8d1f\u7d22\u5f15\u6765\u8fdb\u884c\u590d\u6742\u7684\u5207\u7247\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/p>\n<p>arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])<\/p>\n<h2><strong>\u83b7\u53d6\u4ece\u7d22\u5f151\u5230\u7d22\u5f157\uff0c\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u83b7\u53d6\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>sub_arr = arr[1:8:2]<\/p>\n<p>print(sub_arr)  # \u8f93\u51fa: [2 4 6 8]<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a2D\u6570\u7ec4<\/strong><\/h2>\n<p>arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u83b7\u53d6\u4ece\u7b2c\u4e00\u884c\u5230\u6700\u540e\u4e00\u884c\uff0c\u6bcf\u9694\u4e00\u884c\u548c\u4e00\u5217\u83b7\u53d6\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>sub_matrix = arr_2d[::2, ::2]<\/p>\n<p>print(sub_matrix)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>[[1 3]<\/strong><\/h2>\n<h2><strong> [7 9]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u7d22\u5f15\u3001\u6b65\u957f\u548c\u8d1f\u7d22\u5f15\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u975e\u5e38\u7075\u6d3b\u548c\u590d\u6742\u7684\u5207\u7247\u64cd\u4f5c\u3002\u8fd9\u4e9b\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\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5207\u7247\u64cd\u4f5c\u7684\u5e38\u89c1\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u5f88\u591a\u7528\u9014\uff0c\u5305\u62ec\u6570\u636e\u9884\u5904\u7406\u3001\u7279\u5f81\u9009\u62e9\u548c\u6570\u636e\u53ef\u89c6\u5316\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u9884\u5904\u7406\u9636\u6bb5\uff0c\u4f60\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u4ece\u5927\u578b\u6570\u636e\u96c6\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u5217\u6216\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>        &#39;B&#39;: [6, 7, 8, 9, 10],<\/p>\n<p>        &#39;C&#39;: [11, 12, 13, 14, 15]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u63d0\u53d6\u7279\u5b9a\u7684\u5217<\/strong><\/h2>\n<p>sub_df = df[[&#39;A&#39;, &#39;C&#39;]]<\/p>\n<p>print(sub_df)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>   A   C<\/strong><\/h2>\n<h2><strong>0  1  11<\/strong><\/h2>\n<h2><strong>1  2  12<\/strong><\/h2>\n<h2><strong>2  3  13<\/strong><\/h2>\n<h2><strong>3  4  14<\/strong><\/h2>\n<h2><strong>4  5  15<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u4eceDataFrame\u4e2d\u63d0\u53d6\u4e86\u7279\u5b9a\u7684\u5217\u3002<\/p>\n<\/p>\n<p><h4>2. \u7279\u5f81\u9009\u62e9<\/h4>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u7279\u5f81\u9009\u62e9\u662f\u4e00\u4e2a\u91cd\u8981\u6b65\u9aa4\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u9009\u62e9\u7279\u5b9a\u7684\u7279\u5f81\u8fdb\u884c\u8bad\u7ec3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.datasets import load_iris<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e\u96c6<\/strong><\/h2>\n<p>iris = load_iris()<\/p>\n<p>X, y = iris.data, iris.target<\/p>\n<h2><strong>\u9009\u62e9\u524d\u4e24\u4e2a\u7279\u5f81\u8fdb\u884c\u8bad\u7ec3<\/strong><\/h2>\n<p>X_selected = X[:, :2]<\/p>\n<p>print(X_selected[:5])<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>[[5.1 3.5]<\/strong><\/h2>\n<h2><strong> [4.9 3. ]<\/strong><\/h2>\n<h2><strong> [4.7 3.2]<\/strong><\/h2>\n<h2><strong> [4.6 3.1]<\/strong><\/h2>\n<h2><strong> [5.  3.6]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u9009\u62e9\u4e86\u524d\u4e24\u4e2a\u7279\u5f81\u8fdb\u884c\u8bad\u7ec3\u3002<\/p>\n<\/p>\n<p><h4>3. \u6570\u636e\u53ef\u89c6\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u53ef\u89c6\u5316\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\uff0c\u4ee5\u4fbf\u5728\u56fe\u8868\u4e2d\u5c55\u793a\u7279\u5b9a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u83b7\u53d6\u524d50\u4e2a\u6570\u636e\u70b9\u8fdb\u884c\u7ed8\u56fe<\/strong><\/h2>\n<p>plt.plot(x[:50], y[:50])<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;sin(x)&#39;)<\/p>\n<p>plt.title(&#39;\u524d50\u4e2a\u6570\u636e\u70b9\u7684sin(x)\u51fd\u6570&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u83b7\u53d6\u4e86\u524d50\u4e2a\u6570\u636e\u70b9\uff0c\u5e76\u7ed8\u5236\u4e86sin(x)\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5207\u7247\u64cd\u4f5c\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u5207\u7247\u64cd\u4f5c\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u5728\u4f7f\u7528\u65f6\u4e5f\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h4>1. \u5207\u7247\u7ed3\u679c\u662f\u89c6\u56fe\u8fd8\u662f\u526f\u672c<\/h4>\n<\/p>\n<p><p>\u5728NumPy\u4e2d\uff0c\u5207\u7247\u64cd\u4f5c\u901a\u5e38\u8fd4\u56de\u7684\u662f\u539f\u6570\u7ec4\u7684\u89c6\u56fe\uff0c\u800c\u4e0d\u662f\u526f\u672c\u3002\u8fd9\u610f\u5473\u7740\u5bf9\u5207\u7247\u7ed3\u679c\u7684\u4fee\u6539\u4f1a\u5f71\u54cd\u539f\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = np.array([1, 2, 3, 4, 5])<\/p>\n<p>sub_arr = arr[2:4]<\/p>\n<h2><strong>\u4fee\u6539\u5b50\u6570\u7ec4<\/strong><\/h2>\n<p>sub_arr[0] = 99<\/p>\n<p>print(arr)  # \u8f93\u51fa: [1 2 99 4 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4fee\u6539\u4e86\u5b50\u6570\u7ec4\uff0c\u539f\u6570\u7ec4\u4e5f\u968f\u4e4b\u6539\u53d8\u3002<\/p>\n<\/p>\n<p><h4>2. \u9632\u6b62\u7d22\u5f15\u8d8a\u754c<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\u65f6\uff0c\u786e\u4fdd\u7d22\u5f15\u5728\u6570\u7ec4\u7684\u8303\u56f4\u5185\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u7d22\u5f15\u8d8a\u754c\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u5c1d\u8bd5\u83b7\u53d6\u8d8a\u754c\u7684\u7d22\u5f15<\/strong><\/h2>\n<p>try:<\/p>\n<p>    sub_arr = arr[5:7]<\/p>\n<p>except IndexError as e:<\/p>\n<p>    print(e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7d22\u5f15\u8d8a\u754c\u4f1a\u5bfc\u81f4IndexError\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u6570\u636e\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u7d22\u5f15\u3001\u6b65\u957f\u548c\u8d1f\u7d22\u5f15\uff0c\u4f60\u53ef\u4ee5\u9ad8\u6548\u5730\u5bf9\u6570\u7ec4\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u5728\u6570\u636e\u9884\u5904\u7406\u3001\u7279\u5f81\u9009\u62e9\u8fd8\u662f\u6570\u636e\u53ef\u89c6\u5316\u4e2d\uff0c\u5207\u7247\u64cd\u4f5c\u90fd\u80fd\u53d1\u6325\u91cd\u8981\u4f5c\u7528\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u5c06\u5927\u5927\u63d0\u5347\u4f60\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u6570\u7ec4\u8fdb\u884c\u5207\u7247\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6570\u7ec4\u5207\u7247\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5192\u53f7\uff08:\uff09\u8fd0\u7b97\u7b26\u6765\u5b9e\u73b0\u3002\u60a8\u53ef\u4ee5\u6307\u5b9a\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\uff0c\u683c\u5f0f\u4e3a<code>array[start:end]<\/code>\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u6570\u7ec4<code>arr = [0, 1, 2, 3, 4, 5]<\/code>\uff0c\u4f7f\u7528<code>arr[1:4]<\/code>\u5c06\u8fd4\u56de<code>[1, 2, 3]<\/code>\uff0c\u8868\u793a\u4ece\u7d22\u5f151\u5230\u7d22\u5f154\uff08\u4e0d\u5305\u62ec\u7d22\u5f154\uff09\u3002<\/p>\n<p><strong>\u5207\u7247\u662f\u5426\u652f\u6301\u8d1f\u7d22\u5f15\uff1f<\/strong><br \/>\u5207\u7247\u652f\u6301\u8d1f\u7d22\u5f15\uff0c\u8d1f\u6570\u8868\u793a\u4ece\u6570\u7ec4\u7684\u5c3e\u90e8\u5f00\u59cb\u8ba1\u6570\u3002\u4f8b\u5982\uff0c<code>arr[-3:]<\/code>\u5c06\u8fd4\u56de\u6570\u7ec4\u7684\u6700\u540e\u4e09\u4e2a\u5143\u7d20\u3002\u5982\u679c\u4f7f\u7528<code>arr[:-2]<\/code>\uff0c\u5219\u8fd4\u56de\u4ece\u5f00\u5934\u5230\u5012\u6570\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u5bf9\u5207\u7247\u7ed3\u679c\u8fdb\u884c\u6b65\u957f\u8bbe\u7f6e\uff1f<\/strong><br \/>\u5728\u5207\u7247\u65f6\uff0c\u60a8\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u6b65\u957f\uff0c\u683c\u5f0f\u4e3a<code>array[start:end:step]<\/code>\u3002\u4f8b\u5982\uff0c<code>arr[::2]<\/code>\u5c06\u8fd4\u56de<code>[0, 2, 4]<\/code>\uff0c\u8868\u793a\u4ece\u6570\u7ec4\u4e2d\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u63d0\u53d6\u4e00\u6b21\u3002\u6b65\u957f\u53ef\u4ee5\u662f\u8d1f\u6570\uff0c\u7528\u4e8e\u53cd\u5411\u5207\u7247\uff0c\u5982<code>arr[::-1]<\/code>\u5c06\u8fd4\u56de\u6570\u7ec4\u7684\u53cd\u8f6c\u7248\u672c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5bf9\u4e00\u7ec4\u6570\u7ec4\u8fdb\u884c\u5207\u7247\u7684\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u7d22\u5f15\u3001\u6b65\u957f\u3001\u8d1f\u7d22\u5f15\u3002\u5207\u7247\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb [&hellip;]","protected":false},"author":3,"featured_media":1133041,"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\/1133035"}],"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=1133035"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133035\/revisions"}],"predecessor-version":[{"id":1133042,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133035\/revisions\/1133042"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1133041"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1133035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1133035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1133035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}