{"id":938771,"date":"2024-12-26T20:18:45","date_gmt":"2024-12-26T12:18:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/938771.html"},"modified":"2024-12-26T20:18:47","modified_gmt":"2024-12-26T12:18:47","slug":"python%e5%a6%82%e4%bd%95%e5%88%87%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/938771.html","title":{"rendered":"python\u5982\u4f55\u5207\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074113\/ec792575-70a5-4233-a129-474f08345b83.webp\" alt=\"python\u5982\u4f55\u5207\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u5207\u6570\u636e\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5207\u7247\u3001Pandas\u5e93\u3001NumPy\u5e93\u7b49\u5de5\u5177\u3002\u5207\u7247\u662fPython\u4e2d\u7684\u4e00\u79cd\u57fa\u672c\u64cd\u4f5c\uff0c\u53ef\u7528\u4e8e\u5b57\u7b26\u4e32\u3001\u5217\u8868\u548c\u5143\u7ec4\uff1bPandas\u5e93\u63d0\u4f9b\u4e86DataFrame\u548cSeries\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff1bNumPy\u5e93\u4e13\u6ce8\u4e8e\u5904\u7406\u5927\u578b\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u6548\u7684\u5207\u7247\u65b9\u5f0f\u3002<\/strong>\u4e0b\u9762\u5c06\u5bf9\u4f7f\u7528\u5207\u7247\u65b9\u6cd5\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u662fPython\u7684\u4e00\u79cd\u5185\u7f6e\u529f\u80fd\uff0c\u5141\u8bb8\u4f60\u901a\u8fc7\u6307\u5b9a\u8d77\u59cb\u548c\u7ed3\u675f\u7d22\u5f15\u6765\u63d0\u53d6\u5e8f\u5217\u7684\u67d0\u4e2a\u5b50\u96c6\u3002\u57fa\u672c\u7684\u5207\u7247\u8bed\u6cd5\u662f<code>[start:stop:step]<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u662f\u8d77\u59cb\u7d22\u5f15\uff0c<code>stop<\/code>\u662f\u7ed3\u675f\u7d22\u5f15\uff08\u4e0d\u5305\u62ec\u8be5\u7d22\u5f15\u4f4d\u7f6e\u7684\u5143\u7d20\uff09\uff0c<code>step<\/code>\u662f\u6b65\u957f\uff0c\u9ed8\u8ba4\u4e3a1\u3002\u5207\u7247\u64cd\u4f5c\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u5728\u5904\u7406\u6570\u636e\u65f6\u975e\u5e38\u9ad8\u6548\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5217\u8868<code>data = [1, 2, 3, 4, 5]<\/code>\uff0c\u4f7f\u7528<code>data[1:4]<\/code>\u5c06\u8fd4\u56de<code>[2, 3, 4]<\/code>\u3002\u5229\u7528\u5207\u7247\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u53d6\u6570\u636e\u7684\u67d0\u4e00\u90e8\u5206\uff0c\u8fdb\u884c\u6570\u636e\u7684\u5206\u5272\u3001\u7b5b\u9009\u548c\u91cd\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5207\u7247\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u5904\u7406\u5b57\u7b26\u4e32\u3001\u5217\u8868\u548c\u5143\u7ec4\u7b49\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u901a\u8fc7\u6307\u5b9a\u7d22\u5f15\u533a\u95f4\uff0c\u5207\u7247\u5141\u8bb8\u6211\u4eec\u63d0\u53d6\u6570\u636e\u7684\u5b50\u96c6\u3002<\/p>\n<\/p>\n<p><h4>1. \u5207\u7247\u57fa\u7840\u8bed\u6cd5<\/h4>\n<\/p>\n<p><p>\u5207\u7247\u7684\u57fa\u672c\u8bed\u6cd5\u4e3a<code>[start:stop:step]<\/code>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>start<\/strong>\uff1a\u8d77\u59cb\u7d22\u5f15\uff0c\u5305\u542b\u8be5\u4f4d\u7f6e\u5143\u7d20\u3002<\/li>\n<li><strong>stop<\/strong>\uff1a\u7ed3\u675f\u7d22\u5f15\uff0c\u4e0d\u5305\u542b\u8be5\u4f4d\u7f6e\u5143\u7d20\u3002<\/li>\n<li><strong>step<\/strong>\uff1a\u6b65\u957f\uff0c\u9ed8\u8ba4\u4e3a1\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u7ed9\u5b9a\u5217\u8868<code>data = [10, 20, 30, 40, 50]<\/code>\uff0c<code>data[1:4]<\/code>\u8fd4\u56de<code>[20, 30, 40]<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2. \u53cd\u5411\u5207\u7247<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u6307\u5b9a\u8d1f\u7684\u6b65\u957f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u53cd\u5411\u5207\u7247\u3002\u4f8b\u5982\uff0c<code>data[::-1]<\/code>\u5c06\u8fd4\u56de\u539f\u5217\u8868\u7684\u5012\u5e8f\u7248\u672c<code>[50, 40, 30, 20, 10]<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3. \u5207\u7247\u7684\u8fb9\u754c\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u4e2d\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u7d22\u5f15\u53ef\u4ee5\u7701\u7565\uff0c\u7701\u7565\u540e\u7684\u9ed8\u8ba4\u884c\u4e3a\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7701\u7565<code>start<\/code>\uff1a\u9ed8\u8ba4\u4ece0\u5f00\u59cb\u3002<\/li>\n<li>\u7701\u7565<code>stop<\/code>\uff1a\u9ed8\u8ba4\u5230\u5e8f\u5217\u7ed3\u5c3e\u3002<\/li>\n<li>\u7701\u7565<code>step<\/code>\uff1a\u9ed8\u8ba4\u6b65\u957f\u4e3a1\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c<code>data[:3]<\/code>\u7b49\u540c\u4e8e<code>data[0:3]<\/code>\uff0c\u8fd4\u56de<code>[10, 20, 30]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001Pandas\u5e93\u4e2d\u7684\u6570\u636e\u5207\u5206<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002Pandas\u63d0\u4f9b\u4e86DataFrame\u548cSeries\u5bf9\u8c61\uff0c\u5177\u5907\u4e30\u5bcc\u7684\u6570\u636e\u5207\u5206\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528loc\u548ciloc\u8fdb\u884c\u5207\u7247<\/h4>\n<\/p>\n<ul>\n<li><strong>loc<\/strong>\uff1a\u6807\u7b7e\u7d22\u5f15\uff0c\u901a\u8fc7\u884c\u6807\u7b7e\u548c\u5217\u6807\u7b7e\u8fdb\u884c\u5207\u7247\u3002<\/li>\n<li><strong>iloc<\/strong>\uff1a\u4f4d\u7f6e\u7d22\u5f15\uff0c\u901a\u8fc7\u6574\u6570\u4f4d\u7f6e\u8fdb\u884c\u5207\u7247\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u8003\u8651\u4e00\u4e2aDataFrame\u5bf9\u8c61<code>df<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>df.loc[0:1, &#39;A&#39;]<\/code>\u8fd4\u56de\u524d\u4e24\u884c\u7684\u201cA\u201d\u5217\u3002<\/li>\n<li><code>df.iloc[0:2, 0]<\/code>\u8fd4\u56de\u524d\u4e24\u884c\u7684\u7b2c\u4e00\u5217\u3002<\/li>\n<\/ul>\n<p><h4>2. \u5e03\u5c14\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>Pandas\u652f\u6301\u5e03\u5c14\u7d22\u5f15\uff0c\u901a\u8fc7\u5e03\u5c14\u6761\u4ef6\u7b5b\u9009\u6570\u636e\u3002\u4f8b\u5982\uff0c<code>df[df[&#39;A&#39;] &gt; 1]<\/code>\u5c06\u8fd4\u56de\u201cA\u201d\u5217\u5927\u4e8e1\u7684\u6240\u6709\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001NumPy\u5e93\u4e2d\u7684\u6570\u7ec4\u5207\u7247<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u7684\u4e00\u4e2a\u5f3a\u5927\u5e93\uff0c\u4e13\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u3002\u5b83\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\uff0c\u5e76\u63d0\u4f9b\u4e86\u6bd4Python\u5217\u8868\u66f4\u9ad8\u6548\u7684\u5207\u7247\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u5207\u7247<\/h4>\n<\/p>\n<p><p>NumPy\u6570\u7ec4\u7684\u5207\u7247\u8bed\u6cd5\u4e0e\u5217\u8868\u76f8\u4f3c\u3002\u7ed9\u5b9a\u6570\u7ec4<code>arr = np.array([1, 2, 3, 4, 5])<\/code>\uff0c<code>arr[1:4]<\/code>\u8fd4\u56de<code>[2, 3, 4]<\/code>\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\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6bcf\u4e2a\u7ef4\u5ea6\u7684\u5207\u7247\u6765\u63d0\u53d6\u5b50\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>sub_arr = arr[:2, 1:]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>sub_arr<\/code>\u5c06\u8fd4\u56de<code>[[2, 3], [5, 6]]<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3. \u9ad8\u7ea7\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>NumPy\u652f\u6301\u9ad8\u7ea7\u7d22\u5f15\uff0c\u5305\u62ec\u6574\u6570\u6570\u7ec4\u7d22\u5f15\u548c\u5e03\u5c14\u6570\u7ec4\u7d22\u5f15\u3002\u6574\u6570\u6570\u7ec4\u7d22\u5f15\u5141\u8bb8\u4f60\u901a\u8fc7\u6570\u7ec4\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\uff0c\u800c\u5e03\u5c14\u6570\u7ec4\u7d22\u5f15\u57fa\u4e8e\u6761\u4ef6\u7b5b\u9009\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">indices = [0, 2]<\/p>\n<p>sub_arr = arr[indices, :]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u9009\u62e9\u7b2c0\u548c\u7b2c2\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u7b26\u4e32\u7684\u5207\u7247<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\u5e8f\u5217\uff0c\u53ef\u4ee5\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\u4ee5\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u5b57\u7b26\u4e32\u5207\u7247<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u7684\u5207\u7247\u4e0e\u5217\u8868\u7c7b\u4f3c\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32<code>s = &quot;Hello, World!&quot;<\/code>\uff0c<code>s[0:5]<\/code>\u8fd4\u56de<code>&quot;Hello&quot;<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2. \u6b65\u957f\u5207\u7247<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u6307\u5b9a\u6b65\u957f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u95f4\u9694\u63d0\u53d6\u3002\u4f8b\u5982\uff0c<code>s[::2]<\/code>\u5c06\u8fd4\u56de<code>&quot;Hlo ol!&quot;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5207\u7247\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1. \u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u6e05\u6d17\u8fc7\u7a0b\u4e2d\uff0c\u5207\u7247\u53ef\u7528\u4e8e\u63d0\u53d6\u548c\u8fc7\u6ee4\u9700\u8981\u5904\u7406\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4ece\u5927\u578b\u6570\u636e\u96c6\u4e2d\u63d0\u53d6\u7279\u5b9a\u5217\u6216\u884c\u3002<\/p>\n<\/p>\n<p><h4>2. \u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5207\u7247\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u3001\u7b5b\u9009\u548c\u91cd\u7ec4\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><h4>3. \u6570\u636e\u53ef\u89c6\u5316<\/h4>\n<\/p>\n<p><p>\u5207\u7247\u7528\u4e8e\u9009\u62e9\u6570\u636e\u7684\u5b50\u96c6\u8fdb\u884c\u53ef\u89c6\u5316\uff0c\u4ee5\u4fbf\u66f4\u6e05\u6670\u5730\u5c55\u793a\u6570\u636e\u7684\u7279\u5f81\u548c\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5207\u7247\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528NumPy\u8fdb\u884c\u9ad8\u6548\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>NumPy\u7684\u6570\u7ec4\u5207\u7247\u6bd4Python\u5217\u8868\u66f4\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u3002\u5b83\u5229\u7528\u5e95\u5c42C\u8bed\u8a00\u7684\u5b9e\u73b0\uff0c\u63d0\u4f9b\u4e86\u663e\u8457\u7684\u6027\u80fd\u63d0\u5347\u3002<\/p>\n<\/p>\n<p><h4>2. \u907f\u514d\u4e0d\u5fc5\u8981\u7684\u590d\u5236<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u521b\u5efa\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u526f\u672c\uff0c\u4ee5\u8282\u7701\u5185\u5b58\u548c\u63d0\u9ad8\u6548\u7387\u3002NumPy\u7684\u5207\u7247\u8fd4\u56de\u7684\u662f\u539f\u6570\u7ec4\u7684\u89c6\u56fe\uff0c\u800c\u4e0d\u662f\u526f\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1. \u5207\u7247\u8d8a\u754c<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\u65f6\uff0cPython\u4e0d\u4f1a\u629b\u51fa\u8d8a\u754c\u9519\u8bef\u3002\u5373\u4f7f\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\uff0c\u5207\u7247\u4e5f\u4f1a\u81ea\u52a8\u8c03\u6574\u5230\u53ef\u7528\u8303\u56f4\u5185\u3002<\/p>\n<\/p>\n<p><h4>2. \u4e0d\u53ef\u53d8\u6027<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u5b57\u7b26\u4e32\u548c\u5143\u7ec4\uff09\uff0c\u5207\u7247\u64cd\u4f5c\u4e0d\u4f1a\u4fee\u6539\u539f\u5bf9\u8c61\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u65b0\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>3. \u591a\u7ef4\u6570\u7ec4\u7684\u5207\u7247<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u65f6\uff0c\u9700\u8c28\u614e\u5904\u7406\u4e0d\u540c\u7ef4\u5ea6\u7684\u5207\u7247\uff0c\u4ee5\u786e\u4fdd\u6b63\u786e\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5207\u7247\u64cd\u4f5c\u662f\u6570\u636e\u5904\u7406\u4e2d\u7684\u4e00\u9879\u57fa\u672c\u6280\u80fd\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\u548c\u64cd\u4f5c\u65b9\u5f0f\u3002\u901a\u8fc7\u5207\u7247\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u63d0\u53d6\u3001\u91cd\u7ec4\u548c\u5206\u6790\u6570\u636e\u3002\u65e0\u8bba\u662f\u57fa\u672c\u7684Python\u5207\u7247\uff0c\u8fd8\u662fPandas\u548cNumPy\u63d0\u4f9b\u7684\u9ad8\u7ea7\u529f\u80fd\uff0c\u638c\u63e1\u8fd9\u4e9b\u5de5\u5177\u5c06\u6781\u5927\u63d0\u5347\u4f60\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5207\u7247\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u5c06\u6709\u52a9\u4e8e\u5b9e\u73b0\u66f4\u9ad8\u6548\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u6570\u636e\u5207\u7247\u65b9\u6cd5\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5207\u7247\u65b9\u5f0f\uff0c\u6700\u5e38\u89c1\u7684\u5305\u62ec\u5217\u8868\u3001\u5b57\u7b26\u4e32\u548c\u5143\u7ec4\u7684\u5207\u7247\u3002\u4f7f\u7528\u5192\u53f7\uff08:\uff09\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5b50\u96c6\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3, 4, 5]<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_list[1:4]<\/code>\u83b7\u53d6\u5b50\u5217\u8868<code>[2, 3, 4]<\/code>\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u5f0f\uff0c\u5982<code>my_string = &quot;Hello, World!&quot;<\/code>\uff0c\u901a\u8fc7<code>my_string[7:12]<\/code>\u53ef\u4ee5\u5f97\u5230<code>&quot;World&quot;<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Pandas\u4e2d\u5bf9\u6570\u636e\u6846\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Pandas\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>.loc<\/code>\u548c<code>.iloc<\/code>\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u6846\u7684\u5207\u7247\u64cd\u4f5c\u3002<code>.loc<\/code>\u57fa\u4e8e\u6807\u7b7e\u8fdb\u884c\u5207\u7247\uff0c\u800c<code>.iloc<\/code>\u57fa\u4e8e\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c<code>df.loc[0:2, [&#39;column1&#39;, &#39;column2&#39;]]<\/code>\u4f1a\u83b7\u53d6\u6570\u636e\u6846<code>df<\/code>\u4e2d\u7b2c0\u5230\u7b2c2\u884c\u4ee5\u53ca\u6307\u5b9a\u5217\u7684\u6240\u6709\u6570\u636e\u3002\u5bf9\u4e8e\u4f4d\u7f6e\u5207\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528<code>df.iloc[0:2, 0:2]<\/code>\u6765\u83b7\u5f97\u524d\u4e24\u884c\u548c\u524d\u4e24\u5217\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5207\u7247\u7684\u6b65\u957f\u6709\u4ec0\u4e48\u7528\u5904\uff1f<\/strong><br \/>\u5207\u7247\u7684\u6b65\u957f\u5141\u8bb8\u7528\u6237\u5728\u5207\u7247\u65f6\u6307\u5b9a\u6b65\u5e45\uff0c\u8fd9\u6837\u53ef\u4ee5\u9009\u62e9\u95f4\u9694\u7684\u5143\u7d20\u3002\u683c\u5f0f\u4e3a<code>my_list[start:end:step]<\/code>\u3002\u4f8b\u5982\uff0c<code>my_list[::2]<\/code>\u5c06\u8fd4\u56de\u5217\u8868\u4e2d\u6240\u6709\u5076\u6570\u7d22\u5f15\u7684\u5143\u7d20\u3002\u5982\u679c\u60f3\u8981\u53cd\u8f6c\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u8d1f\u6b65\u957f\uff0c\u5982<code>my_list[::-1]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u5217\u8868\u7684\u53cd\u8f6c\u7248\u672c\u3002\u8fd9\u6837\u7684\u7075\u6d3b\u6027\u4f7f\u5f97\u6570\u636e\u5904\u7406\u66f4\u52a0\u9ad8\u6548\u548c\u4fbf\u6377\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5207\u6570\u636e\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5207\u7247\u3001Pandas\u5e93\u3001NumPy\u5e93\u7b49\u5de5\u5177\u3002\u5207\u7247\u662fPython\u4e2d\u7684\u4e00\u79cd [&hellip;]","protected":false},"author":3,"featured_media":938775,"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\/938771"}],"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=938771"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/938771\/revisions"}],"predecessor-version":[{"id":938776,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/938771\/revisions\/938776"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/938775"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=938771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=938771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=938771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}