{"id":939415,"date":"2024-12-26T20:34:31","date_gmt":"2024-12-26T12:34:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/939415.html"},"modified":"2024-12-26T20:34:34","modified_gmt":"2024-12-26T12:34:34","slug":"python-%e5%a6%82%e4%bd%95%e6%9b%b4%e6%96%b0list","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/939415.html","title":{"rendered":"python \u5982\u4f55\u66f4\u65b0list"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074453\/d93076d2-a593-4722-97e2-ef5760c784be.webp\" alt=\"python \u5982\u4f55\u66f4\u65b0list\" \/><\/p>\n<p><p> <strong>\u5728 Python \u4e2d\uff0c\u66f4\u65b0\u5217\u8868\uff08list\uff09\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3001\u4f7f\u7528\u5217\u8868\u65b9\u6cd5\uff08\u5982 append\u3001extend\u3001insert \u7b49\uff09\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u8d4b\u503c\u6700\u4e3a\u7b80\u5355\u4e14\u76f4\u89c2\uff0c\u4f8b\u5982 list[\u7d22\u5f15] = \u65b0\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u7684\u503c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u8d4b\u503c<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u8d4b\u503c\u662f\u66f4\u65b0\u5217\u8868\u4e2d\u67d0\u4e2a\u7279\u5b9a\u5143\u7d20\u7684\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002Python \u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u7d22\u5f15\u6765\u66f4\u6539\u5176\u5185\u5bb9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list[2] = 10<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5217\u8868 <code>my_list<\/code> \u7684\u7b2c\u4e09\u4e2a\u5143\u7d20\u88ab\u66f4\u65b0\u4e3a <code>10<\/code>\u3002\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u8d4b\u503c\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u5730\u66f4\u65b0\u5217\u8868\u4e2d\u7684\u5355\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5207\u7247\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u5141\u8bb8\u60a8\u540c\u65f6\u66f4\u65b0\u5217\u8868\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\u3002\u901a\u8fc7\u5207\u7247\uff0c\u60a8\u53ef\u4ee5\u6307\u5b9a\u8981\u66f4\u65b0\u7684\u8303\u56f4\uff0c\u5e76\u5c06\u5176\u66ff\u6362\u4e3a\u4e00\u4e2a\u7b49\u957f\u7684\u5e8f\u5217\u3002\u4f8b\u5982\uff1a<\/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)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>my_list<\/code> \u7684\u7b2c\u4e8c\u548c\u7b2c\u4e09\u4e2a\u5143\u7d20\u88ab\u66ff\u6362\u4e3a <code>20<\/code> \u548c <code>30<\/code>\u3002\u5207\u7247\u64cd\u4f5c\u53ef\u4ee5\u7528\u4e8e\u5217\u8868\u7684\u6279\u91cf\u66f4\u65b0\uff0c\u5e76\u4e14\u53ef\u4ee5\u6709\u6548\u5730\u6539\u53d8\u5217\u8868\u7684\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u7684\u65b9\u6cd5\u6765\u66f4\u65b0\u5217\u8868\u5185\u5bb9\uff0c\u5982 <code>append()<\/code>\u3001<code>extend()<\/code> \u548c <code>insert()<\/code> \u7b49\u3002\u4e0b\u9762\u662f\u8fd9\u4e9b\u65b9\u6cd5\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>append()<\/strong>: \u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3]<\/p>\n<p>my_list.append(4)<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 <code>append()<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5411\u5217\u8868\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u65b0\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>extend()<\/strong>: \u5c06\u53e6\u4e00\u4e2a\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3]<\/p>\n<p>my_list.extend([4, 5])<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>extend()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5c06\u53e6\u4e00\u4e2a\u5217\u8868\u6216\u5e8f\u5217\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u5f53\u524d\u5217\u8868\u7684\u672b\u5c3e\u3002\u4e0e <code>append()<\/code> \u4e0d\u540c\uff0c<code>extend()<\/code> \u53ef\u4ee5\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>insert()<\/strong>: \u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 4]<\/p>\n<p>my_list.insert(2, 3)<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>insert()<\/code> \u65b9\u6cd5\u5141\u8bb8\u60a8\u5728\u5217\u8868\u7684\u7279\u5b9a\u4f4d\u7f6e\u63d2\u5165\u4e00\u4e2a\u65b0\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u800c\u5f3a\u5927\u7684\u8bed\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u66f4\u65b0\u5217\u8868\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6279\u91cf\u66f4\u65b0\u6216\u8f6c\u6362\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list = [x * 2 for x in my_list]<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7528\u4e8e\u5c06 <code>my_list<\/code> \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u4e58\u4ee5 <code>2<\/code>\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6279\u91cf\u64cd\u4f5c\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u901a\u8fc7\u5faa\u73af\u66f4\u65b0<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u4e4b\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u66f4\u65b0\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u52a8\u6001\u66f4\u65b0\u5217\u8868\u7684\u60c5\u51b5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for i in range(len(my_list)):<\/p>\n<p>    if my_list[i] % 2 == 0:<\/p>\n<p>        my_list[i] *= 2<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u904d\u5386\u5217\u8868\uff0c\u5e76\u5c06\u6240\u6709\u5076\u6570\u5143\u7d20\u7ffb\u500d\u3002\u5faa\u73af\u66f4\u65b0\u65b9\u6cd5\u7075\u6d3b\u6027\u9ad8\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u66f4\u65b0\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u79cd\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u5f0f\u6765\u66f4\u65b0 Python \u5217\u8868\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u65f6\u5e94\u8003\u8651\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u6267\u884c\u6548\u7387\u548c\u529f\u80fd\u8981\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\u5230\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>append()<\/code>\u65b9\u6cd5\u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u7684\u672b\u5c3e\u3002\u4f8b\u5982\uff0c\u82e5\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3]<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_list.append(4)<\/code>\u5c06\u6570\u5b574\u6dfb\u52a0\u5230\u5217\u8868\u672b\u5c3e\u3002\u82e5\u60f3\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>extend()<\/code>\u65b9\u6cd5\uff0c\u4f8b\u5982<code>my_list.extend([5, 6])<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5220\u9664Python\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u4ece\u5217\u8868\u4e2d\u5220\u9664\u7279\u5b9a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\uff0c\u5b83\u4f1a\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u82e5\u5217\u8868\u4e3a<code>my_list = [1, 2, 3, 2]<\/code>\uff0c\u4f7f\u7528<code>my_list.remove(2)<\/code>\u5c06\u5220\u9664\u7b2c\u4e00\u4e2a\u51fa\u73b0\u76842\u3002\u5982\u679c\u4f60\u77e5\u9053\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u6765\u5220\u9664\uff0c\u4f8b\u5982<code>del my_list[1]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4fee\u6539\u5217\u8868\u4e2d\u67d0\u4e2a\u5143\u7d20\u7684\u503c\uff1f<\/strong><br \/>\u4fee\u6539\u5217\u8868\u4e2d\u67d0\u4e2a\u5143\u7d20\u7684\u503c\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u83b7\u53d6\u8be5\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u7136\u540e\u76f4\u63a5\u8d4b\u503c\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709<code>my_list = [10, 20, 30]<\/code>\uff0c\u60f3\u5c06\u7b2c\u4e8c\u4e2a\u5143\u7d20\u4fee\u6539\u4e3a25\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_list[1] = 25<\/code>\u5b9e\u73b0\u3002\u8fd9\u79cd\u64cd\u4f5c\u76f4\u63a5\u5f71\u54cd\u539f\u59cb\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Python \u4e2d\uff0c\u66f4\u65b0\u5217\u8868\uff08list\uff09\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3001\u4f7f\u7528\u5217\u8868\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":939416,"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\/939415"}],"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=939415"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/939415\/revisions"}],"predecessor-version":[{"id":939418,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/939415\/revisions\/939418"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/939416"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=939415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=939415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=939415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}