{"id":1008955,"date":"2024-12-27T11:07:24","date_gmt":"2024-12-27T03:07:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1008955.html"},"modified":"2024-12-27T11:07:27","modified_gmt":"2024-12-27T03:07:27","slug":"python%e7%9a%84list%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1008955.html","title":{"rendered":"python\u7684list\u5982\u4f55\u8c03\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084059\/36892ee5-de88-4607-af09-88c8e9f10639.webp\" alt=\"python\u7684list\u5982\u4f55\u8c03\u7528\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u8c03\u7528\u5217\u8868\uff08list\uff09\u7684\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\u5217\u8868\u7684\u521b\u5efa\u3001\u8bbf\u95ee\u3001\u4fee\u6539\u3001\u904d\u5386\u3001\u65b9\u6cd5\u8c03\u7528\u548c\u5207\u7247\u64cd\u4f5c<\/strong>\u3002\u9996\u5148\uff0cPython\u4e2d\u7684\u5217\u8868\u662f\u4e00\u79cd\u53ef\u53d8\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5b58\u50a8\u4efb\u610f\u7c7b\u578b\u7684\u5143\u7d20\u3002\u5217\u8868\u901a\u8fc7\u65b9\u62ec\u53f7<code>[]<\/code>\u521b\u5efa\uff0c\u5143\u7d20\u4e4b\u95f4\u7528\u9017\u53f7\u5206\u9694\u3002<strong>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3Python\u5217\u8868\u7684\u64cd\u4f5c\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5217\u8868\u7684\u5404\u4e2a\u65b9\u9762\uff0c\u5305\u62ec\u521b\u5efa\u3001\u8bbf\u95ee\u3001\u4fee\u6539\u3001\u904d\u5386\u3001\u5e38\u7528\u65b9\u6cd5\u548c\u5207\u7247\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efa\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u521b\u5efa\u5217\u8868\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u65b9\u62ec\u53f7<code>[]<\/code>\u5373\u53ef\u3002\u5217\u8868\u53ef\u4ee5\u5305\u542b\u4efb\u610f\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u5305\u62ec\u6574\u6570\u3001\u5b57\u7b26\u4e32\u3001\u751a\u81f3\u5176\u4ed6\u5217\u8868\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u7a7a\u5217\u8868<\/strong>\uff1a\u901a\u8fc7<code>my_list = []<\/code>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u3002<\/li>\n<li><strong>\u5e26\u6709\u5143\u7d20\u7684\u5217\u8868<\/strong>\uff1a\u4f8b\u5982\uff0c<code>my_list = [1, 2, 3, &#39;Python&#39;, [4, 5]]<\/code>\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u8bbf\u95ee\u5217\u8868\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8fdb\u884c\u8bbf\u95ee\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u8d1f\u7d22\u5f15\u4ece-1\u5f00\u59cb\u8868\u793a\u4ece\u5217\u8868\u672b\u5c3e\u5f00\u59cb\u8ba1\u6570\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6b63\u7d22\u5f15<\/strong>\uff1a\u4f8b\u5982\uff0c<code>my_list[0]<\/code>\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<li><strong>\u8d1f\u7d22\u5f15<\/strong>\uff1a\u4f8b\u5982\uff0c<code>my_list[-1]<\/code>\u8bbf\u95ee\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4fee\u6539\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u5176\u5143\u7d20\u3002\u901a\u8fc7\u7d22\u5f15\u8d4b\u503c\u53ef\u4ee5\u6539\u53d8\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4fee\u6539\u5143\u7d20<\/strong>\uff1a\u4f8b\u5982\uff0c<code>my_list[0] = &#39;Hello&#39;<\/code>\u5c06\u7b2c\u4e00\u4e2a\u5143\u7d20\u4fee\u6539\u4e3a\u5b57\u7b26\u4e32&quot;Hello&quot;\u3002<\/li>\n<li><strong>\u6dfb\u52a0\u5143\u7d20<\/strong>\uff1a\u4f7f\u7528<code>append()<\/code>\u65b9\u6cd5\u5728\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20\uff0c\u4f8b\u5982<code>my_list.append(&#39;World&#39;)<\/code>\u3002<\/li>\n<li><strong>\u63d2\u5165\u5143\u7d20<\/strong>\uff1a\u4f7f\u7528<code>insert()<\/code>\u65b9\u6cd5\u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\uff0c\u4f8b\u5982<code>my_list.insert(1, &#39;Python&#39;)<\/code>\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u904d\u5386\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u904d\u5386\u5217\u8868\u901a\u5e38\u4f7f\u7528<code>for<\/code>\u5faa\u73af\uff0c\u53ef\u4ee5\u8bbf\u95ee\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u5355\u904d\u5386<\/strong>\uff1a\u4f8b\u5982\uff0c<code>for item in my_list: print(item)<\/code>\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7d22\u5f15\u904d\u5386<\/strong>\uff1a\u4f8b\u5982\uff0c<code>for i in range(len(my_list)): print(my_list[i])<\/code>\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u5217\u8868\u7684\u5e38\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u5217\u8868\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5185\u7f6e\u65b9\u6cd5\u6765\u64cd\u4f5c\u5217\u8868\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>append()<\/strong>\uff1a\u5728\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<li><strong>extend()<\/strong>\uff1a\u901a\u8fc7\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\u6765\u6269\u5c55\u5217\u8868\u3002<\/li>\n<li><strong>insert()<\/strong>\uff1a\u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<li><strong>remove()<\/strong>\uff1a\u79fb\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6307\u5b9a\u503c\u3002<\/li>\n<li><strong>pop()<\/strong>\uff1a\u79fb\u9664\u5e76\u8fd4\u56de\u6307\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u9ed8\u8ba4\u4e3a\u6700\u540e\u4e00\u4e2a\u3002<\/li>\n<li><strong>clear()<\/strong>\uff1a\u79fb\u9664\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<\/li>\n<li><strong>index()<\/strong>\uff1a\u8fd4\u56de\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u7d22\u5f15\u3002<\/li>\n<li><strong>count()<\/strong>\uff1a\u8fd4\u56de\u6307\u5b9a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/li>\n<li><strong>sort()<\/strong>\uff1a\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<li><strong>reverse()<\/strong>\uff1a\u53cd\u8f6c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u987a\u5e8f\u3002<\/li>\n<li><strong>copy()<\/strong>\uff1a\u8fd4\u56de\u5217\u8868\u7684\u6d45\u590d\u5236\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u5217\u8868\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u5141\u8bb8\u8bbf\u95ee\u5217\u8868\u7684\u5b50\u96c6\uff0c\u4f7f\u7528<code>start:stop:step<\/code>\u8bed\u6cd5\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u57fa\u672c\u5207\u7247<\/strong>\uff1a\u4f8b\u5982\uff0c<code>my_list[1:3]<\/code>\u8fd4\u56de\u4ece\u7d22\u5f151\u5230\u7d22\u5f153\uff08\u4e0d\u5305\u62ec\uff09\u7684\u5143\u7d20\u3002<\/li>\n<li><strong>\u6b65\u957f\u5207\u7247<\/strong>\uff1a\u4f8b\u5982\uff0c<code>my_list[::2]<\/code>\u8fd4\u56de\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u7684\u5b50\u5217\u8868\u3002<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u5217\u8868\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\uff0c\u652f\u6301\u591a\u79cd\u64cd\u4f5c\u548c\u65b9\u6cd5\uff0c\u4f7f\u5176\u6210\u4e3a\u5904\u7406\u6570\u636e\u7684\u7406\u60f3\u9009\u62e9\u3002\u901a\u8fc7\u638c\u63e1\u5217\u8868\u7684\u57fa\u672c\u64cd\u4f5c\u3001\u65b9\u6cd5\u548c\u5207\u7247\u6280\u5de7\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6570\u636e\u3002\u65e0\u8bba\u662f\u521b\u5efa\u3001\u8bbf\u95ee\u3001\u4fee\u6539\u8fd8\u662f\u904d\u5386\u5217\u8868\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u7b80\u5355\u800c\u76f4\u89c2\u7684\u65b9\u5f0f\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u5217\u8868\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u65b9\u62ec\u53f7 <code>[]<\/code> \u6765\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868\uff0c\u5e76\u7528\u9017\u53f7\u5206\u9694\u6bcf\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff1a<code>my_list = [1, 2, 3, &#39;hello&#39;, 4.5]<\/code>\u3002\u5217\u8868\u53ef\u4ee5\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5305\u62ec\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u751a\u81f3\u5176\u4ed6\u5217\u8868\u3002<\/p>\n<p><strong>\u5982\u4f55\u4ecePython\u5217\u8868\u4e2d\u8bbf\u95ee\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u3002Python\u7684\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u8fd9\u610f\u5473\u7740\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f0\u3002\u4f8b\u5982\uff0c<code>my_list[0]<\/code>\u5c06\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u5982\u679c\u60a8\u60f3\u8bbf\u95ee\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u8d1f\u7d22\u5f15\uff0c<code>my_list[-1]<\/code>\u5c06\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u5217\u8868\u4e2d\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\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<code>my_list.append(&#39;new_item&#39;)<\/code>\u4f1a\u5c06&#39;new_item&#39;\u6dfb\u52a0\u5230\u5217\u8868\u7684\u672b\u5c3e\u3002\u8981\u5220\u9664\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\uff0c\u4f8b\u5982\uff0c<code>my_list.remove(2)<\/code>\u5c06\u5220\u9664\u503c\u4e3a2\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u5982\u679c\u60a8\u77e5\u9053\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\uff0c\u4f8b\u5982\uff0c<code>del my_list[0]<\/code>\u4f1a\u5220\u9664\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8c03\u7528\u5217\u8868\uff08list\uff09\u7684\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\u5217\u8868\u7684\u521b\u5efa\u3001\u8bbf\u95ee\u3001\u4fee\u6539\u3001\u904d\u5386\u3001\u65b9\u6cd5\u8c03\u7528\u548c\u5207\u7247\u64cd\u4f5c\u3002\u9996\u5148\uff0cP [&hellip;]","protected":false},"author":3,"featured_media":1008965,"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\/1008955"}],"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=1008955"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008955\/revisions"}],"predecessor-version":[{"id":1008966,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008955\/revisions\/1008966"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1008965"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1008955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1008955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1008955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}