{"id":1009194,"date":"2024-12-27T11:09:38","date_gmt":"2024-12-27T03:09:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1009194.html"},"modified":"2024-12-27T11:09:40","modified_gmt":"2024-12-27T03:09:40","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%ae%be%e8%ae%a1%e5%88%87%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1009194.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8bbe\u8ba1\u5207\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084208\/c36e5c79-ef76-4b8d-9f72-76950b7a621b.webp\" alt=\"python\u4e2d\u5982\u4f55\u8bbe\u8ba1\u5207\u7247\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8bbe\u8ba1\u5207\u7247\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528<code>slice()<\/code>\u5bf9\u8c61\u3001\u76f4\u63a5\u4f7f\u7528\u5207\u7247\u8bed\u6cd5\u3001\u4f7f\u7528\u5207\u7247\u6765\u4fee\u6539\u5217\u8868\u3002<\/strong> \u5207\u7247\u662fPython\u4e2d\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u4f60\u4ece\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5b57\u7b26\u4e32\u3001\u5143\u7ec4\uff09\u4e2d\u63d0\u53d6\u90e8\u5206\u5143\u7d20\u3002\u4f7f\u7528<code>slice()<\/code>\u5bf9\u8c61\u53ef\u4ee5\u901a\u8fc7\u66f4\u7075\u6d3b\u7684\u65b9\u5f0f\u521b\u5efa\u5207\u7247\uff1b\u76f4\u63a5\u4f7f\u7528\u5207\u7247\u8bed\u6cd5\u5219\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff1b\u800c\u4f7f\u7528\u5207\u7247\u6765\u4fee\u6539\u5217\u8868\u5219\u80fd\u591f\u6709\u6548\u5730\u66f4\u65b0\u5e8f\u5217\u7684\u90e8\u5206\u5185\u5bb9\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>SLICE()<\/code>\u5bf9\u8c61<\/p>\n<\/p>\n<p><p><code>slice()<\/code>\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5207\u7247\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u52a8\u6001\u8c03\u6574\u5207\u7247\u8303\u56f4\u65f6\u3002<code>slice()<\/code>\u7684\u8bed\u6cd5\u4e3a<code>slice(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\u542b\uff09\uff0c<code>step<\/code>\u662f\u6b65\u957f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>slice()<\/code>\u5bf9\u8c61\u53ef\u4ee5\u5728\u4efb\u4f55\u652f\u6301\u5207\u7247\u64cd\u4f5c\u7684\u5bf9\u8c61\u4e0a\u4f7f\u7528\u3002\u5176\u4f18\u70b9\u662f\u53ef\u4ee5\u52a8\u6001\u6307\u5b9a\u5207\u7247\u53c2\u6570\u3002\u5728\u67d0\u4e9b\u9700\u8981\u9891\u7e41\u8c03\u6574\u5207\u7247\u8303\u56f4\u7684\u573a\u666f\u4e2d\uff0c\u4f7f\u7528<code>slice()<\/code>\u5bf9\u8c61\u4f1a\u66f4\u52a0\u4f18\u96c5\u548c\u9ad8\u6548\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5217\u8868\uff0c\u5e76\u4e14\u9700\u8981\u4ece\u4e2d\u63d0\u53d6\u4e0d\u540c\u7684\u5b50\u5e8f\u5217\uff0c\u4f7f\u7528<code>slice()<\/code>\u5bf9\u8c61\u53ef\u4ee5\u8ba9\u4ee3\u7801\u53d8\u5f97\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>s = slice(2, 8, 2)<\/p>\n<p>print(items[s])  # \u8f93\u51fa: [2, 4, 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<code>slice<\/code>\u5bf9\u8c61<code>(2, 8, 2)<\/code>\uff0c\u8868\u793a\u4ece\u7d22\u5f152\u52308\uff0c\u6bcf\u9694\u4e24\u4e2a\u5143\u7d20\u53d6\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u76f4\u63a5\u4f7f\u7528\u5207\u7247\u8bed\u6cd5<\/p>\n<\/p>\n<p><p>Python\u7684\u5207\u7247\u8bed\u6cd5\u975e\u5e38\u7b80\u6d01\u548c\u76f4\u89c2\uff0c\u683c\u5f0f\u4e3a<code>[start:stop:step]<\/code>\u3002\u8fd9\u4e5f\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u5207\u7247\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5e38\u89c1\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5207\u7247\u8bed\u6cd5\u53ef\u4ee5\u7528\u4e8e\u51e0\u4e4e\u6240\u6709\u7684\u5e8f\u5217\u7c7b\u578b\uff0c\u5305\u62ec\u5217\u8868\u3001\u5b57\u7b26\u4e32\u3001\u5143\u7ec4\u7b49\u3002\u901a\u8fc7\u6307\u5b9a\u4e0d\u540c\u7684<code>start<\/code>\u3001<code>stop<\/code>\u548c<code>step<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u63d0\u53d6\u51fa\u60f3\u8981\u7684\u5b50\u5e8f\u5217\u3002\u4f8b\u5982\uff0c\u63d0\u53d6\u524d\u4e94\u4e2a\u5143\u7d20\u3001\u63d0\u53d6\u4ece\u7b2c\u4e09\u4e2a\u5f00\u59cb\u5230\u7b2c\u4e03\u4e2a\u5143\u7d20\u3001\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u63d0\u53d6\u7b49\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World!&quot;<\/p>\n<p>print(text[0:5])  # \u8f93\u51fa: Hello<\/p>\n<p>print(text[7:12]) # \u8f93\u51fa: World<\/p>\n<p>print(text[::2])  # \u8f93\u51fa: Hlo ol!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5207\u7247\u8bed\u6cd5\u63d0\u53d6\u4e86\u5b57\u7b26\u4e32\u7684\u4e0d\u540c\u90e8\u5206\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5207\u7247\u6765\u4fee\u6539\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5207\u7247\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u63d0\u53d6\u5e8f\u5217\u7684\u5b50\u96c6\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u4fee\u6539\u5e8f\u5217\u7684\u5185\u5bb9\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u5927\u89c4\u6a21\u66f4\u65b0\u5217\u8868\u6216\u5b57\u7b26\u4e32\u5185\u5bb9\u65f6\u5c24\u5176\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u66ff\u6362\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5207\u7247\u6307\u5b9a\u8303\u56f4\uff0c\u7136\u540e\u5c06\u5176\u8d4b\u503c\u4e3a\u65b0\u7684\u5e8f\u5217\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5bf9\u5217\u8868\u4e2d\u591a\u4e2a\u5143\u7d20\u7684\u66ff\u6362\u3002\u7279\u522b\u662f\u5728\u9700\u8981\u6279\u91cf\u66f4\u65b0\u6216\u63d2\u5165\u5927\u91cf\u5143\u7d20\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>numbers[2:5] = [20, 30, 40]<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 20, 30, 40, 6, 7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u7d22\u5f152\u52305\u7684\u5143\u7d20\u66ff\u6362\u4e3a\u65b0\u7684\u5217\u8868<code>[20, 30, 40]<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5207\u7247\u7684\u9ad8\u7ea7\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5207\u7247\u5728Python\u4e2d\u4e0d\u4ec5\u4ec5\u7528\u4e8e\u7b80\u5355\u7684\u63d0\u53d6\u548c\u4fee\u6539\u64cd\u4f5c\uff0c\u901a\u8fc7\u7ec4\u5408\u4f7f\u7528\uff0c\u8fd8\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e9b\u590d\u6742\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u53cd\u8f6c\u5e8f\u5217<\/strong><\/p>\n<\/p>\n<p><p>\u5207\u7247\u53ef\u4ee5\u901a\u8fc7\u8d1f\u6b65\u957f\u5b9e\u73b0\u5e8f\u5217\u7684\u53cd\u8f6c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u4e14\u9ad8\u6548\uff0c\u975e\u5e38\u9002\u5408\u4e8e\u9700\u8981\u5feb\u901f\u53cd\u8f6c\u5e8f\u5217\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5]<\/p>\n<p>reversed_data = data[::-1]<\/p>\n<p>print(reversed_data)  # \u8f93\u51fa: [5, 4, 3, 2, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u590d\u5236\u5e8f\u5217<\/strong><\/p>\n<\/p>\n<p><p>\u5207\u7247\u8fd8\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5e8f\u5217\u7684\u526f\u672c\u3002\u901a\u8fc7\u4e0d\u6307\u5b9a<code>start<\/code>\u3001<code>stop<\/code>\u548c<code>step<\/code>\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u4e0e\u539f\u5e8f\u5217\u5185\u5bb9\u76f8\u540c\u4f46\u5bf9\u8c61\u4e0d\u540c\u7684\u526f\u672c\u3002\u8fd9\u5728\u9700\u8981\u4fdd\u62a4\u539f\u59cb\u6570\u636e\u4e0d\u88ab\u4fee\u6539\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original = [1, 2, 3, 4, 5]<\/p>\n<p>copy = original[:]<\/p>\n<p>print(copy)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5728\u5207\u7247\u4e2d\u4f7f\u7528\u6761\u4ef6\u903b\u8f91<\/p>\n<\/p>\n<p><p>\u867d\u7136Python\u5207\u7247\u672c\u8eab\u4e0d\u652f\u6301\u76f4\u63a5\u7684\u6761\u4ef6\u903b\u8f91\uff0c\u4f46\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6Python\u7279\u6027\uff08\u5982\u5217\u8868\u63a8\u5bfc\u5f0f\uff09\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7ed3\u5408\u6761\u4ef6\u7b5b\u9009<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u5728\u5207\u7247\u7684\u57fa\u7840\u4e0a\u8fdb\u4e00\u6b65\u8fdb\u884c\u6761\u4ef6\u7b5b\u9009\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u4ece\u5e8f\u5217\u4e2d\u63d0\u53d6\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u5143\u7d20\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>even_numbers = [n for n in numbers if n % 2 == 0]<\/p>\n<p>print(even_numbers)  # \u8f93\u51fa: [2, 4, 6, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed3\u5408<code>filter()<\/code>\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>filter()<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u4e3a\u901a\u7528\u7684\u65b9\u5f0f\u6765\u5bf9\u5e8f\u5217\u8fdb\u884c\u6761\u4ef6\u8fc7\u6ee4\u3002\u7ed3\u5408\u5207\u7247\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u63d0\u53d6\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>def is_even(n):<\/p>\n<p>    return n % 2 == 0<\/p>\n<p>even_numbers = list(filter(is_even, numbers))<\/p>\n<p>print(even_numbers)  # \u8f93\u51fa: [2, 4, 6, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u6ce8\u610f\u4e8b\u9879\u548c\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7d22\u5f15\u8d8a\u754c<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u4f7f\u7528\u5207\u7247\u65f6\uff0c\u5373\u4f7f\u6307\u5b9a\u7684<code>start<\/code>\u6216<code>stop<\/code>\u8d85\u51fa\u4e86\u5e8f\u5217\u7684\u8303\u56f4\uff0cPython\u4e5f\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u8fd9\u4e00\u70b9\u9700\u8981\u7279\u522b\u6ce8\u610f\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u52a8\u6001\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6b65\u957f\u4e3a\u96f6<\/strong><\/p>\n<\/p>\n<p><p>\u5207\u7247\u7684\u6b65\u957f\u4e0d\u80fd\u4e3a\u96f6\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4<code>ValueError<\/code>\u5f02\u5e38\u3002\u786e\u4fdd\u6b65\u957f\u4e3a\u6b63\u6574\u6570\u6216\u8d1f\u6574\u6570\uff0c\u4ee5\u907f\u514d\u9519\u8bef\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u5207\u7247\u590d\u5236<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u5e8f\u5217\u526f\u672c\u65f6\uff0c\u4f7f\u7528\u5207\u7247\u8bed\u6cd5<code>[:]<\/code>\u662f\u4e00\u79cd\u60ef\u7528\u65b9\u6cd5\u3002\u8fd9\u4e0d\u4ec5\u7b80\u6d01\uff0c\u8fd8\u80fd\u786e\u4fdd\u65b0\u7684\u5e8f\u5217\u5bf9\u8c61\u4e0e\u539f\u59cb\u5bf9\u8c61\u76f8\u4e92\u72ec\u7acb\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5207\u7247<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5207\u7247\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u5fc5\u8981\u7684\u6027\u80fd\u5f00\u9500\u3002\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0c\u5c24\u5176\u9700\u8981\u6ce8\u610f\u5207\u7247\u7684\u6548\u7387\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\u800c\u8a00\uff0cPython\u4e2d\u7684\u5207\u7247\u529f\u80fd\u5f3a\u5927\u800c\u7075\u6d3b\uff0c\u80fd\u591f\u6781\u5927\u7b80\u5316\u5bf9\u5e8f\u5217\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528<code>slice()<\/code>\u5bf9\u8c61\u3001\u5207\u7247\u8bed\u6cd5\u4ee5\u53ca\u4e0e\u6761\u4ef6\u903b\u8f91\u7684\u7ed3\u5408\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u3002\u540c\u65f6\uff0c\u6ce8\u610f\u5207\u7247\u64cd\u4f5c\u4e2d\u7684\u4e00\u4e9b\u7ec6\u8282\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u907f\u514d\u5e38\u89c1\u7684\u9519\u8bef\u548c\u6027\u80fd\u95ee\u9898\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5b50\u5e8f\u5217\u63d0\u53d6\uff0c\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u7b5b\u9009\u548c\u66ff\u6362\uff0c\u5207\u7247\u90fd\u80fd\u63d0\u4f9b\u7b80\u6d01\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5207\u7247\u5728Python\u4e2d\u6709\u4ec0\u4e48\u5b9e\u9645\u5e94\u7528\uff1f<\/strong><br \/>\u5207\u7247\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\u6570\u636e\u7ed3\u6784\u3002\u901a\u8fc7\u5207\u7247\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u63d0\u53d6\u51fa\u7279\u5b9a\u90e8\u5206\uff0c\u8fdb\u884c\u6570\u636e\u5206\u6790\u6216\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5728\u6570\u636e\u5904\u7406\u65f6\uff0c\u53ef\u4ee5\u4ece\u4e00\u4e2a\u5927\u5217\u8868\u4e2d\u63d0\u53d6\u51fa\u9700\u8981\u7684\u5b50\u96c6\uff0c\u6216\u5728\u5b57\u7b26\u4e32\u4e2d\u83b7\u53d6\u7279\u5b9a\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5207\u7247\u4e2d\u4f7f\u7528\u8d1f\u7d22\u5f15\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8d1f\u7d22\u5f15\u4ece\u5e8f\u5217\u7684\u672b\u5c3e\u5f00\u59cb\u8ba1\u6570\u3002\u4f7f\u7528\u8d1f\u7d22\u5f15\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u5e8f\u5217\u7684\u6700\u540e\u51e0\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>my_list[-1]<\/code>\u53ef\u4ee5\u83b7\u53d6\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c<code>my_list[-3:]<\/code>\u5219\u53ef\u4ee5\u83b7\u53d6\u5217\u8868\u7684\u6700\u540e\u4e09\u4e2a\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u5f0f\u7279\u522b\u9002\u5408\u5728\u5904\u7406\u52a8\u6001\u6570\u636e\u65f6\uff0c\u80fd\u591f\u7b80\u5316\u4ee3\u7801\u903b\u8f91\u3002<\/p>\n<p><strong>\u5207\u7247\u652f\u6301\u54ea\u4e9b\u64cd\u4f5c\u548c\u529f\u80fd\uff1f<\/strong><br \/>\u5207\u7247\u4e0d\u4ec5\u652f\u6301\u57fa\u672c\u7684\u63d0\u53d6\u64cd\u4f5c\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6b65\u957f\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002\u6b65\u957f\u53c2\u6570\u5141\u8bb8\u5f00\u53d1\u8005\u4ee5\u7279\u5b9a\u7684\u95f4\u9694\u83b7\u53d6\u5143\u7d20\uff0c\u4f8b\u5982\uff0c<code>my_list[::2]<\/code>\u5c06\u8fd4\u56de\u5217\u8868\u4e2d\u6240\u6709\u5076\u6570\u7d22\u5f15\u7684\u5143\u7d20\u3002\u6b64\u5916\uff0c\u5207\u7247\u8fd8\u53ef\u4ee5\u7528\u4e8e\u53cd\u8f6c\u5e8f\u5217\uff0c\u901a\u8fc7\u8bbe\u7f6e\u8d1f\u6b65\u957f\uff0c\u4f8b\u5982<code>my_list[::-1]<\/code>\uff0c\u53ef\u4ee5\u5feb\u901f\u83b7\u5f97\u53cd\u8f6c\u540e\u7684\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bbe\u8ba1\u5207\u7247\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528slice()\u5bf9\u8c61\u3001\u76f4\u63a5\u4f7f\u7528\u5207\u7247\u8bed\u6cd5\u3001\u4f7f\u7528\u5207\u7247\u6765\u4fee\u6539\u5217\u8868\u3002 \u5207\u7247\u662fPyt [&hellip;]","protected":false},"author":3,"featured_media":1009198,"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\/1009194"}],"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=1009194"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009194\/revisions"}],"predecessor-version":[{"id":1009202,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009194\/revisions\/1009202"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1009198"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1009194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1009194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1009194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}