{"id":1072001,"date":"2025-01-08T11:12:18","date_gmt":"2025-01-08T03:12:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1072001.html"},"modified":"2025-01-08T11:12:20","modified_gmt":"2025-01-08T03:12:20","slug":"python%e5%a6%82%e4%bd%95%e6%8d%a2%e5%88%97%e8%a1%a8%e9%87%8c%e9%9d%a2%e7%9a%84%e5%86%85%e5%ae%b9-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1072001.html","title":{"rendered":"python\u5982\u4f55\u6362\u5217\u8868\u91cc\u9762\u7684\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102339\/835125ad-fc09-44ac-af4c-93e0d056164f.webp\" alt=\"python\u5982\u4f55\u6362\u5217\u8868\u91cc\u9762\u7684\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6362\u5217\u8868\u91cc\u9762\u7684\u5185\u5bb9\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8d4b\u503c\u3001\u5207\u7247\u8d4b\u503c\u3001\u5217\u8868\u65b9\u6cd5\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002<\/strong> \u7d22\u5f15\u8d4b\u503c\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u5355\u4e2a\u5143\u7d20\u7684\u66ff\u6362\uff1b\u5207\u7247\u8d4b\u503c\u9002\u7528\u4e8e\u591a\u4e2a\u5143\u7d20\u7684\u66ff\u6362\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u5c55\u5f00\u7d22\u5f15\u8d4b\u503c\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u7d22\u5f15\u8d4b\u503c\u662f\u901a\u8fc7\u5217\u8868\u7684\u7d22\u5f15\u6765\u76f4\u63a5\u66ff\u6362\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3, 4, 5]<\/code>\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u7b2c\u4e8c\u4e2a\u5143\u7d20\u66ff\u6362\u4e3a<code>20<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_list[1] = 20<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7d22\u5f15\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u7d22\u5f15\u8d4b\u503c\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u7528\u4e8e\u66ff\u6362\u5217\u8868\u4e2d\u7684\u5355\u4e2a\u5143\u7d20\u3002\u901a\u8fc7\u7d22\u5f15\uff0c\u53ef\u4ee5\u7cbe\u786e\u5730\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u4f4d\u7f6e\uff0c\u7136\u540e\u5c06\u5176\u503c\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list[1] = 20<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 20, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5207\u7247\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u8d4b\u503c\u7528\u4e8e\u66ff\u6362\u5217\u8868\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\u3002\u901a\u8fc7\u5207\u7247\uff0c\u53ef\u4ee5\u4e00\u6b21\u6027\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u591a\u4e2a\u4f4d\u7f6e\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u4f4d\u7f6e\u7684\u503c\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list[1:3] = [20, 30]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 20, 30, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5217\u8868\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u65b9\u6cd5\u5982<code>append()<\/code>\u3001<code>insert()<\/code>\u3001<code>remove()<\/code>\u3001<code>pop()<\/code>\u3001<code>extend()<\/code>\u7b49\u4e5f\u53ef\u4ee5\u7528\u6765\u4fee\u6539\u5217\u8868\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>append()<\/strong>\uff1a\u5728\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list.append(6)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>insert()<\/strong>\uff1a\u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list.insert(2, 2.5)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 2.5, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>remove()<\/strong>\uff1a\u79fb\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list.remove(3)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>pop()<\/strong>\uff1a\u79fb\u9664\u5e76\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\uff0c\u9ed8\u8ba4\u4e3a\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>removed_item = my_list.pop(2)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p>print(removed_item)  # \u8f93\u51fa: 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>extend()<\/strong>\uff1a\u5c06\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list.extend([6, 7, 8])<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6, 7, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u6216\u4fee\u6539\u5217\u8868\u3002\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u5bf9\u5217\u8868\u8fdb\u884c\u6761\u4ef6\u7b5b\u9009\u6216\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>new_list = [x * 2 for x in my_list]<\/p>\n<p>print(new_list)  # \u8f93\u51fa: [2, 4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528map()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>map()<\/code>\u51fd\u6570\u53ef\u4ee5\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u76f8\u540c\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>new_list = list(map(lambda x: x * 2, my_list))<\/p>\n<p>print(new_list)  # \u8f93\u51fa: [2, 4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5217\u8868\u5207\u7247\u8fdb\u884c\u66ff\u6362<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u5207\u7247\u53ef\u4ee5\u7528\u4e8e\u66ff\u6362\u5217\u8868\u4e2d\u7684\u4e00\u90e8\u5206\u5185\u5bb9\uff0c\u9002\u7528\u4e8e\u9700\u8981\u66ff\u6362\u8fde\u7eed\u591a\u4e2a\u5143\u7d20\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list[1:4] = [20, 30, 40]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 20, 30, 40, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u7ed3\u5408\u4f7f\u7528\u5217\u8868\u65b9\u6cd5\u4e0e\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u5c06\u5217\u8868\u65b9\u6cd5\u548c\u5207\u7247\u7ed3\u5408\u8d77\u6765\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5217\u8868\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>extend()<\/code>\u65b9\u6cd5\u5c06\u4e00\u4e2a\u5217\u8868\u7684\u5207\u7247\u66ff\u6362\u4e3a\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list[1:4] = []<\/p>\n<p>my_list.extend([20, 30, 40])<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 5, 20, 30, 40]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u6df1\u5ea6\u62f7\u8d1d\u4e0e\u6d45\u62f7\u8d1d<\/h3>\n<\/p>\n<p><p>\u5728\u4fee\u6539\u5217\u8868\u65f6\uff0c\u6709\u65f6\u9700\u8981\u6ce8\u610f\u6df1\u5ea6\u62f7\u8d1d\u4e0e\u6d45\u62f7\u8d1d\u7684\u95ee\u9898\u3002\u6d45\u62f7\u8d1d\u53ea\u662f\u590d\u5236\u4e86\u5217\u8868\u7684\u5f15\u7528\uff0c\u800c\u6df1\u5ea6\u62f7\u8d1d\u5219\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5e76\u9012\u5f52\u5730\u590d\u5236\u4e86\u6240\u6709\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<p>my_list = [[1, 2], [3, 4]]<\/p>\n<p>shallow_copy = my_list.copy()<\/p>\n<p>deep_copy = copy.deepcopy(my_list)<\/p>\n<p>my_list[0][0] = 10<\/p>\n<p>print(shallow_copy)  # \u8f93\u51fa: [[10, 2], [3, 4]]<\/p>\n<p>print(deep_copy)     # \u8f93\u51fa: [[1, 2], [3, 4]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u590d\u6742\u7684\u5217\u8868\u64cd\u4f5c\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6765\u7b80\u5316\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c<code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u6570\u7ec4\u548c\u5217\u8868\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>np_array = np.array(my_list)<\/p>\n<p>np_array[1:4] = [20, 30, 40]<\/p>\n<p>print(np_array)  # \u8f93\u51fa: [ 1 20 30 40  5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u5217\u8868\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5217\u8868\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u9700\u8981\u9891\u7e41\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>deque<\/code>\uff08\u53cc\u7aef\u961f\u5217\uff09\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5177\u6709\u8f83\u9ad8\u7684\u64cd\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>my_deque = deque([1, 2, 3, 4, 5])<\/p>\n<p>my_deque.appendleft(0)<\/p>\n<p>print(my_deque)  # \u8f93\u51fa: deque([0, 1, 2, 3, 4, 5])<\/p>\n<p>my_deque.pop()<\/p>\n<p>print(my_deque)  # \u8f93\u51fa: deque([0, 1, 2, 3, 4])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u5e38\u89c1\u9519\u8bef\u4e0e\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u5728\u4fee\u6539\u5217\u8868\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u7d22\u5f15\u8d8a\u754c\u3001\u4f7f\u7528\u9519\u8bef\u7684\u5217\u8868\u65b9\u6cd5\u7b49\u3002\u9700\u8981\u901a\u8fc7\u8c03\u8bd5\u5de5\u5177\u548c\u65e5\u5fd7\u8bb0\u5f55\u6765\u6392\u67e5\u548c\u89e3\u51b3\u8fd9\u4e9b\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>try:<\/p>\n<p>    my_list[5] = 6<\/p>\n<p>except IndexError as e:<\/p>\n<p>    print(f&quot;\u9519\u8bef: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4fee\u6539\u5217\u8868\u5185\u5bb9\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ec\u7d22\u5f15\u8d4b\u503c\u3001\u5207\u7247\u8d4b\u503c\u3001\u5217\u8868\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001<code>map()<\/code>\u51fd\u6570\u3001\u6df1\u5ea6\u62f7\u8d1d\u4e0e\u6d45\u62f7\u8d1d\u3001\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u8fd0\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4ea4\u6362\u5217\u8868\u4e2d\u7684\u4e24\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u7d22\u5f15\u6765\u4ea4\u6362\u5217\u8868\u4e2d\u7684\u4e24\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3, 4]<\/code>\uff0c\u60f3\u8981\u4ea4\u6362\u5143\u7d20<code>2<\/code>\u548c<code>3<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list[1], my_list[2] = my_list[2], my_list[1]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>my_list<\/code>\u5c06\u53d8\u6210<code>[1, 3, 2, 4]<\/code>\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4ea4\u6362\u4efb\u610f\u4e24\u4e2a\u5143\u7d20\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u4ea4\u6362\u5217\u8868\u4e2d\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u9664\u4e86\u76f4\u63a5\u4f7f\u7528\u7d22\u5f15\u6765\u4ea4\u6362\u5143\u7d20\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570<code>pop()<\/code>\u548c<code>insert()<\/code>\u65b9\u6cd5\u3002\u5047\u8bbe\u6211\u4eec\u8981\u4ea4\u6362\u7d22\u5f15\u4e3a<code>1<\/code>\u548c<code>2<\/code>\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, 4]\ntemp = my_list.pop(1)  # \u79fb\u9664\u5e76\u8fd4\u56de\u7d22\u5f151\u7684\u5143\u7d20\nmy_list.insert(2, temp)  # \u5728\u7d22\u5f152\u63d2\u5165\u8fd9\u4e2a\u5143\u7d20\nmy_list[1], my_list[2] = my_list[2], my_list[1]  # \u6700\u540e\u518d\u4ea4\u6362\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u6bd4\u8f83\u590d\u6742\uff0c\u4f46\u5728\u9700\u8981\u5bf9\u5143\u7d20\u8fdb\u884c\u66f4\u591a\u5904\u7406\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u4e00\u6b21\u6027\u4ea4\u6362\u5217\u8868\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u540c\u65f6\u4ea4\u6362\u5217\u8868\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u548c\u5217\u8868\u89e3\u5305\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3, 4, 5]<\/code>\uff0c\u60f3\u8981\u5c06\u524d\u4e24\u4e2a\u5143\u7d20\u548c\u540e\u4e24\u4e2a\u5143\u7d20\u4ea4\u6362\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list[0:2], my_list[3:5] = my_list[3:5], my_list[0:2]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>my_list<\/code>\u5c06\u53d8\u6210<code>[4, 5, 3, 1, 2]<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5143\u7d20\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6362\u5217\u8868\u91cc\u9762\u7684\u5185\u5bb9\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8d4b\u503c\u3001\u5207\u7247\u8d4b\u503c\u3001\u5217\u8868\u65b9\u6cd5\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002 \u7d22\u5f15\u8d4b\u503c\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1072007,"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\/1072001"}],"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=1072001"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072001\/revisions"}],"predecessor-version":[{"id":1072011,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072001\/revisions\/1072011"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1072007"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1072001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1072001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1072001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}