{"id":923416,"date":"2024-12-26T14:49:16","date_gmt":"2024-12-26T06:49:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/923416.html"},"modified":"2024-12-26T14:49:18","modified_gmt":"2024-12-26T06:49:18","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89list","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/923416.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49list"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211506\/43d43beb-b8b9-4598-8a39-0645e0c46e5e.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49list\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u5217\u8868\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u65b9\u62ec\u53f7 []\u3001\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528list()\u51fd\u6570<\/strong>\u3002\u4f7f\u7528\u65b9\u62ec\u53f7\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u521b\u5efa\u66f4\u590d\u6742\u7684\u5217\u8868\uff0c\u800c\u4f7f\u7528list()\u51fd\u6570\u53ef\u4ee5\u5c06\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u3002<strong>\u4f7f\u7528\u65b9\u62ec\u53f7\u5b9a\u4e49\u5217\u8868\u65f6\uff0c\u5143\u7d20\u7528\u9017\u53f7\u5206\u9694<\/strong>\u3002\u4f8b\u5982\uff1amy_list = [1, 2, 3]\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u65b9\u62ec\u53f7\u5b9a\u4e49\u5217\u8868<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u65b9\u62ec\u53f7\u5b9a\u4e49\u5217\u8868\u662f\u6700\u76f4\u63a5\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u7528\u4e8e\u5b9a\u4e49\u9759\u6001\u7684\u3001\u5df2\u77e5\u5143\u7d20\u7684\u5217\u8868\u3002\u5143\u7d20\u53ef\u4ee5\u662f\u4efb\u610f\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\u3001\u5b57\u7b26\u4e32\u3001\u6d6e\u70b9\u6570\uff0c\u751a\u81f3\u662f\u5176\u4ed6\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u5217\u8868<\/p>\n<p>int_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684\u5217\u8868<\/strong><\/h2>\n<p>string_list = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u5217\u8868<\/strong><\/h2>\n<p>mixed_list = [1, &quot;apple&quot;, 3.14, True]<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5d4c\u5957\u5217\u8868<\/strong><\/h2>\n<p>nested_list = [[1, 2], [3, 4], [5, 6]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u65b9\u62ec\u53f7\u5b9a\u4e49\u5217\u8868\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5728\u65b9\u62ec\u53f7\u5185\u6dfb\u52a0\u5143\u7d20\uff0c\u5143\u7d20\u4e4b\u95f4\u7528\u9017\u53f7\u5206\u9694\u3002\u6ce8\u610f\uff0c\u5217\u8868\u4e2d\u53ef\u4ee5\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u4e5f\u53ef\u4ee5\u5305\u542b\u5176\u4ed6\u5217\u8868\u4f5c\u4e3a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5b9a\u4e49\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5217\u8868\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u9700\u8981\u901a\u8fc7\u67d0\u79cd\u89c4\u5219\u751f\u6210\u5217\u8868\u7684\u573a\u666f\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>[expression for item in iterable]<\/code>\uff0c\u5176\u4e2d<code>expression<\/code>\u662f\u5bf9\u6bcf\u4e2a\u5143\u7d20\u7684\u5904\u7406\u65b9\u5f0f\uff0c<code>iterable<\/code>\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5e73\u65b9\u6570\u7684\u5217\u8868<\/p>\n<p>squares = [x2 for x in range(10)]<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u5305\u542b\u5076\u6570\u7684\u5217\u8868<\/strong><\/h2>\n<p>even_numbers = [x for x in range(20) if x % 2 == 0]<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u957f\u5ea6\u5927\u4e8e5\u7684\u5b57\u7b26\u4e32\u7684\u5217\u8868<\/strong><\/h2>\n<p>words = [&quot;hello&quot;, &quot;world&quot;, &quot;Python&quot;, &quot;programming&quot;]<\/p>\n<p>long_words = [word for word in words if len(word) &gt; 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u4e0d\u4ec5\u7b80\u6d01\uff0c\u8fd8\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\uff0c\u9002\u5408\u7528\u4e8e\u9700\u8981\u4ece\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u63d0\u53d6\u6216\u8f6c\u6362\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528list()\u51fd\u6570\u5b9a\u4e49\u5217\u8868<\/p>\n<\/p>\n<p><p>list()\u51fd\u6570\u7528\u4e8e\u5c06\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u3001\u5b57\u5178\u7b49\uff09\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u5728\u9700\u8981\u52a8\u6001\u751f\u6210\u5217\u8868\u6216\u4ece\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u63d0\u53d6\u6570\u636e\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868<\/p>\n<p>tuple_data = (1, 2, 3)<\/p>\n<p>list_from_tuple = list(tuple_data)<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff08\u6bcf\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u4e00\u4e2a\u5143\u7d20\uff09<\/strong><\/h2>\n<p>string_data = &quot;hello&quot;<\/p>\n<p>list_from_string = list(string_data)<\/p>\n<h2><strong>\u5c06\u5b57\u5178\u7684\u952e\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>dict_data = {&quot;a&quot;: 1, &quot;b&quot;: 2, &quot;c&quot;: 3}<\/p>\n<p>keys_list = list(dict_data.keys())<\/p>\n<h2><strong>\u5c06\u5b57\u5178\u7684\u503c\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>values_list = list(dict_data.values())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528list()\u51fd\u6570\u65f6\uff0c\u8f93\u5165\u7684\u5fc5\u987b\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u8fd9\u79cd\u65b9\u6cd5\u5e38\u7528\u4e8e\u9700\u8981\u4ece\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u63d0\u53d6\u6570\u636e\u5f62\u6210\u5217\u8868\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u9009\u62e9\u5b9a\u4e49\u5217\u8868\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5982\u679c\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u9759\u6001\u7684\u3001\u5df2\u77e5\u5143\u7d20\u7684\u5217\u8868\uff0c\u4f7f\u7528\u65b9\u62ec\u53f7\u6700\u4e3a\u7b80\u5355\u76f4\u63a5\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u6839\u636e\u67d0\u79cd\u89c4\u5219\u751f\u6210\u5217\u8868\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u4e2a\u9ad8\u6548\u4e14\u7b80\u6d01\u7684\u9009\u62e9\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u4ece\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u63d0\u53d6\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u5217\u8868\uff0clist()\u51fd\u6570\u662f\u6700\u4f73\u9009\u62e9\u3002<\/li>\n<\/ol>\n<p><p>\u6b64\u5916\uff0cPython\u7684\u5217\u8868\u662f\u52a8\u6001\u7684\u3001\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u6539\u53d8\u5217\u8868\u7684\u957f\u5ea6\u548c\u5185\u5bb9\u3002\u5229\u7528\u8fd9\u4e00\u7279\u6027\uff0c\u7ed3\u5408Python\u63d0\u4f9b\u7684\u4e30\u5bcc\u7684\u5185\u7f6e\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7075\u6d3b\u5730\u5904\u7406\u5404\u79cd\u6570\u636e\u548c\u95ee\u9898\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\u7a7a\u7684\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u65b9\u62ec\u53f7<code>[]<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>my_list = []<\/code>\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>my_list<\/code>\u7684\u7a7a\u5217\u8868\u3002\u60a8\u53ef\u4ee5\u5728\u6b64\u5217\u8868\u4e2d\u7a0d\u540e\u6dfb\u52a0\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u5411\u5df2\u7ecf\u5b9a\u4e49\u7684\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>append()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = []<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_list.append(10)<\/code>\u5c06\u6570\u5b5710\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u6b64\u65b9\u6cd5\u4f1a\u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u7684\u672b\u5c3e\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002Python\u7684\u5217\u8868\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u56e0\u6b64\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f0\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3]<\/code>\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>my_list[0]<\/code>\u6765\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u7ed3\u679c\u5c06\u662f1\u3002\u5bf9\u4e8e\u8d1f\u6570\u7d22\u5f15\uff0c<code>my_list[-1]<\/code>\u5c06\u8fd4\u56de\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u5217\u8868\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u65b9\u62ec\u53f7 []\u3001\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528list()\u51fd\u6570\u3002\u4f7f\u7528\u65b9\u62ec\u53f7\u662f\u6700\u5e38\u89c1\u7684 [&hellip;]","protected":false},"author":3,"featured_media":923418,"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\/923416"}],"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=923416"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/923416\/revisions"}],"predecessor-version":[{"id":923421,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/923416\/revisions\/923421"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/923418"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=923416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=923416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=923416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}