{"id":1169417,"date":"2025-01-15T16:10:19","date_gmt":"2025-01-15T08:10:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1169417.html"},"modified":"2025-01-15T16:10:23","modified_gmt":"2025-01-15T08:10:23","slug":"python%e5%a6%82%e4%bd%95%e8%bd%ac%e6%8d%a2%e6%88%90list","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1169417.html","title":{"rendered":"python\u5982\u4f55\u8f6c\u6362\u6210list"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070405\/d17c8367-dbe7-46cf-ab05-1b5f420dc3c8.webp\" alt=\"python\u5982\u4f55\u8f6c\u6362\u6210list\" \/><\/p>\n<p><p> Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u6570\u636e\u8f6c\u6362\u6210\u5217\u8868\u3002<strong>\u4f7f\u7528list()\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570split()\u3001\u4f7f\u7528for\u5faa\u73af\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u662f\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u8be6\u7ec6\u89e3\u91ca\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528list()\u51fd\u6570<\/strong><\/p>\n<p><code>list()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5b57\u7b26\u4e32\u3001\u5143\u7ec4\u3001\u96c6\u5408\u3001\u5b57\u5178\u7b49\uff09\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u65f6\uff0c\u6bcf\u4e2a\u5b57\u7b26\u90fd\u4f1a\u6210\u4e3a\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello&quot;<\/p>\n<p>string_list = list(string)<\/p>\n<p>print(string_list)  # \u8f93\u51fa [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u7528\u4e8e\u751f\u6210\u5217\u8868\u3002\u5b83\u5141\u8bb8\u4f60\u4f7f\u7528\u7b80\u6d01\u7684\u8bed\u6cd5\u5728\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e0a\u8fdb\u884c\u64cd\u4f5c\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello&quot;<\/p>\n<p>string_list = [char for char in string]<\/p>\n<p>print(string_list)  # \u8f93\u51fa [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5904\u7406\u6216\u8fc7\u6ee4\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u8303\u56f4\u5185\u7684\u5076\u6570\u8f6c\u4e3a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">even_numbers = [x for x in range(10) if x % 2 == 0]<\/p>\n<p>print(even_numbers)  # \u8f93\u51fa [0, 2, 4, 6, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570split()<\/h2>\n<\/p>\n<p><p><strong>split()\u51fd\u6570<\/strong>\u4e3b\u8981\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5217\u8868\u3002\u5b83\u901a\u8fc7\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5207\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8fd9\u4e9b\u90e8\u5206\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u7528\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;apple,banana,cherry&quot;<\/p>\n<p>fruit_list = string.split(&#39;,&#39;)<\/p>\n<p>print(fruit_list)  # \u8f93\u51fa [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>split()\u51fd\u6570\u7684\u9ed8\u8ba4\u5206\u9694\u7b26\u662f\u7a7a\u683c\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u5c06\u7a7a\u683c\u5206\u9694\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello world&quot;<\/p>\n<p>word_list = string.split()<\/p>\n<p>print(word_list)  # \u8f93\u51fa [&#39;hello&#39;, &#39;world&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528for\u5faa\u73af<\/h2>\n<\/p>\n<p><p><strong>for\u5faa\u73af<\/strong>\u662f\u4e00\u79cd\u901a\u7528\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u5c06\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u867d\u7136\u4e0d\u5982\u524d\u9762\u7684\u65b9\u6cd5\u7b80\u6d01\uff0c\u4f46\u5b83\u975e\u5e38\u7075\u6d3b\u3002\u4f8b\u5982\uff0c\u5c06\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tuple_data = (1, 2, 3, 4, 5)<\/p>\n<p>tuple_list = []<\/p>\n<p>for item in tuple_data:<\/p>\n<p>    tuple_list.append(item)<\/p>\n<p>print(tuple_list)  # \u8f93\u51fa [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>for\u5faa\u73af\u8fd8\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u8f6c\u6362\u64cd\u4f5c\uff0c\u4f8b\u5982\u5c06\u5b57\u5178\u7684\u952e\u6216\u503c\u8f6c\u6362\u4e3a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict_data = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>keys_list = []<\/p>\n<p>for key in dict_data:<\/p>\n<p>    keys_list.append(key)<\/p>\n<p>print(keys_list)  # \u8f93\u51fa [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u65b9\u6cd5\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u4e0d\u540c\u7684\u6280\u672f\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u52bf\uff0c\u9009\u62e9\u9002\u5408\u4f60\u7684\u9700\u6c42\u7684\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528map()\u51fd\u6570<\/h2>\n<\/p>\n<p><p><strong>map()\u51fd\u6570<\/strong>\u662fPython\u5185\u7f6e\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u6307\u5b9a\u7684\u51fd\u6570\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u7ed3\u679c\u7684\u8fed\u4ee3\u5668\u3002\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u53ef\u4ee5\u5c06map()\u7684\u7ed3\u679c\u4f20\u9012\u7ed9list()\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5176ASCII\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello&quot;<\/p>\n<p>ascii_list = list(map(ord, string))<\/p>\n<p>print(ascii_list)  # \u8f93\u51fa [104, 101, 108, 108, 111]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>map()\u51fd\u6570\u5e38\u7528\u4e8e\u9700\u8981\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u76f8\u540c\u5904\u7406\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528numpy\u5e93<\/h2>\n<\/p>\n<p><p><strong>numpy\u5e93<\/strong>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u3002numpy\u7684\u6570\u7ec4\u5bf9\u8c61\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>numpy_array = np.array([1, 2, 3, 4, 5])<\/p>\n<p>list_from_numpy = numpy_array.tolist()<\/p>\n<p>print(list_from_numpy)  # \u8f93\u51fa [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>numpy\u5e93\u7279\u522b\u9002\u5408\u5904\u7406\u5927\u91cf\u6570\u503c\u6570\u636e\u548c\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528pandas\u5e93<\/h2>\n<\/p>\n<p><p><strong>pandas\u5e93<\/strong>\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u5c06\u6570\u636e\u6846\u4e2d\u7684\u5217\u6216\u884c\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;Age&#39;: [25, 30, 35]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>name_list = df[&#39;Name&#39;].tolist()<\/p>\n<p>print(name_list)  # \u8f93\u51fa [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pandas\u5e93\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\uff0c\u5982\u8868\u683c\u6570\u636e\u548c\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p>\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c<strong>\u96c6\u5408\u63a8\u5bfc\u5f0f<\/strong>\u4e5f\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u5217\u8868\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello&quot;<\/p>\n<p>char_list = list({char for char in string})<\/p>\n<p>print(char_list)  # \u8f93\u51fa [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;o&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u96c6\u5408\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7528\u4e8e\u53bb\u9664\u91cd\u590d\u5143\u7d20\uff0c\u56e0\u4e3a\u96c6\u5408\u4e0d\u80fd\u5305\u542b\u91cd\u590d\u9879\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p><strong>\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/strong>\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4e0d\u4f1a\u7acb\u5373\u751f\u6210\u5217\u8868\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\u3002\u53ef\u4ee5\u5c06\u751f\u6210\u5668\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello&quot;<\/p>\n<p>char_generator = (char for char in string)<\/p>\n<p>char_list = list(char_generator)<\/p>\n<p>print(char_list)  # \u8f93\u51fa [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u9002\u7528\u4e8e\u5904\u7406\u5927\u6570\u636e\u96c6\uff0c\u56e0\u4e3a\u5b83\u4e0d\u9700\u8981\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u51fd\u6570\u53ef\u80fd\u65e0\u6cd5\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u8f6c\u6362\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u5d4c\u5957\u5b57\u5178\u8f6c\u6362\u4e3a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def dict_to_list(d):<\/p>\n<p>    result = []<\/p>\n<p>    for key, value in d.items():<\/p>\n<p>        if isinstance(value, dict):<\/p>\n<p>            result.extend(dict_to_list(value))<\/p>\n<p>        else:<\/p>\n<p>            result.append((key, value))<\/p>\n<p>    return result<\/p>\n<p>nested_dict = {&#39;a&#39;: 1, &#39;b&#39;: {&#39;c&#39;: 2, &#39;d&#39;: 3}}<\/p>\n<p>list_from_dict = dict_to_list(nested_dict)<\/p>\n<p>print(list_from_dict)  # \u8f93\u51fa [(&#39;a&#39;, 1), (&#39;c&#39;, 2), (&#39;d&#39;, 3)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u51fd\u6570\u63d0\u4f9b\u4e86\u6700\u5927\u7684\u7075\u6d3b\u6027\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u8f6c\u6362\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h2>\n<\/p>\n<p><p>\u9664\u4e86numpy\u548cpandas\uff0cPython\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u8f6c\u6362\u3002\u4f8b\u5982\uff0c<strong>json\u5e93<\/strong>\u53ef\u4ee5\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_string = &#39;[&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]&#39;<\/p>\n<p>list_from_json = json.loads(json_string)<\/p>\n<p>print(list_from_json)  # \u8f93\u51fa [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7b80\u5316\u6570\u636e\u5904\u7406\u4efb\u52a1\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u4e00\u3001\u4f7f\u7528itertools\u6a21\u5757<\/h2>\n<\/p>\n<p><p><strong>itertools\u6a21\u5757<\/strong>\u63d0\u4f9b\u4e86\u4e00\u7ec4\u7528\u4e8e\u9ad8\u6548\u64cd\u4f5c\u8fed\u4ee3\u5668\u7684\u5de5\u5177\u3002\u4f8b\u5982\uff0c\u4f7f\u7528itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n()\u5c06\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fde\u63a5\u6210\u4e00\u4e2a\uff0c\u7136\u540e\u8f6c\u6362\u4e3a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>combined_list = list(itertools.chain(list1, list2))<\/p>\n<p>print(combined_list)  # \u8f93\u51fa [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>itertools\u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570eval()<\/h2>\n<\/p>\n<p><p><strong>eval()\u51fd\u6570<\/strong>\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u5c06\u8868\u793a\u5217\u8868\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b9e\u9645\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_list = &quot;[1, 2, 3, 4, 5]&quot;<\/p>\n<p>list_from_string = eval(string_list)<\/p>\n<p>print(list_from_string)  # \u8f93\u51fa [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6ce8\u610f\uff0ceval()\u51fd\u6570\u5b58\u5728\u5b89\u5168\u98ce\u9669\uff0c\u5c3d\u91cf\u907f\u514d\u5728\u4e0d\u53ef\u4fe1\u7684\u6570\u636e\u4e0a\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u4e09\u3001\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u9012\u5f52\u65b9\u6cd5\u662f\u4e00\u79cd\u5728\u51fd\u6570\u4e2d\u8c03\u7528\u81ea\u8eab\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u5904\u7406\u5d4c\u5957\u7ed3\u6784\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5c06\u5d4c\u5957\u5217\u8868\u5c55\u5e73\u4e3a\u4e00\u7ef4\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def flatten(nested_list):<\/p>\n<p>    result = []<\/p>\n<p>    for element in nested_list:<\/p>\n<p>        if isinstance(element, list):<\/p>\n<p>            result.extend(flatten(element))<\/p>\n<p>        else:<\/p>\n<p>            result.append(element)<\/p>\n<p>    return result<\/p>\n<p>nested_list = [1, [2, [3, 4], 5], 6]<\/p>\n<p>flat_list = flatten(nested_list)<\/p>\n<p>print(flat_list)  # \u8f93\u51fa [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9012\u5f52\u65b9\u6cd5\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u56db\u3001\u4f7f\u7528csv\u6a21\u5757<\/h2>\n<\/p>\n<p><p><strong>csv\u6a21\u5757<\/strong>\u53ef\u4ee5\u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165CSV\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4eceCSV\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;data.csv&#39;, newline=&#39;&#39;) as csvfile:<\/p>\n<p>    reader = csv.reader(csvfile)<\/p>\n<p>    csv_list = list(reader)<\/p>\n<p>print(csv_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>csv\u6a21\u5757\u9002\u5408\u5904\u7406\u8868\u683c\u6570\u636e\uff0c\u7279\u522b\u662f\u4e0e\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u4e94\u3001\u4f7f\u7528json\u6a21\u5757<\/h2>\n<\/p>\n<p><p><strong>json\u6a21\u5757<\/strong>\u53ef\u4ee5\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5217\u8868\u6216\u5b57\u5178\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u4f8b\u5982\uff0c\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_string = &#39;[&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]&#39;<\/p>\n<p>list_from_json = json.loads(json_string)<\/p>\n<p>print(list_from_json)  # \u8f93\u51fa [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>json\u6a21\u5757\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\uff0c\u7279\u522b\u662fWeb API\u8fd4\u56de\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u516d\u3001\u4f7f\u7528ast.literal_eval()<\/h2>\n<\/p>\n<p><p><strong>ast.literal_eval()\u51fd\u6570<\/strong>\u53ef\u4ee5\u5b89\u5168\u5730\u5c06\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u5c06\u8868\u793a\u5217\u8868\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b9e\u9645\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>string_list = &quot;[1, 2, 3, 4, 5]&quot;<\/p>\n<p>list_from_string = ast.literal_eval(string_list)<\/p>\n<p>print(list_from_string)  # \u8f93\u51fa [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0eeval()\u76f8\u6bd4\uff0cast.literal_eval()\u66f4\u5b89\u5168\uff0c\u56e0\u4e3a\u5b83\u53ea\u89e3\u6790\u5b57\u9762\u503c\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u4e03\u3001\u4f7f\u7528copy\u6a21\u5757<\/h2>\n<\/p>\n<p><p><strong>copy\u6a21\u5757<\/strong>\u63d0\u4f9b\u4e86\u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u5c06\u4e00\u4e2a\u5217\u8868\u590d\u5236\u4e3a\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<p>original_list = [1, 2, 3, 4, 5]<\/p>\n<p>shallow_copy = copy.copy(original_list)<\/p>\n<p>deep_copy = copy.deepcopy(original_list)<\/p>\n<p>print(shallow_copy)  # \u8f93\u51fa [1, 2, 3, 4, 5]<\/p>\n<p>print(deep_copy)  # \u8f93\u51fa [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>copy\u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u590d\u5236\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u516b\u3001\u4f7f\u7528functools\u6a21\u5757<\/h2>\n<\/p>\n<p><p><strong>functools\u6a21\u5757<\/strong>\u63d0\u4f9b\u4e86\u9ad8\u9636\u51fd\u6570\u548c\u64cd\u4f5c\u5de5\u5177\u3002\u4f8b\u5982\uff0c\u4f7f\u7528functools.reduce()\u5c06\u591a\u4e2a\u5143\u7d20\u5408\u5e76\u4e3a\u4e00\u4e2a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>list_of_lists = [[1, 2], [3, 4], [5, 6]]<\/p>\n<p>flattened_list = reduce(lambda x, y: x + y, list_of_lists)<\/p>\n<p>print(flattened_list)  # \u8f93\u51fa [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>functools\u6a21\u5757\u9002\u5408\u5904\u7406\u9700\u8981\u9ad8\u9636\u51fd\u6570\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u4e5d\u3001\u4f7f\u7528operator\u6a21\u5757<\/h2>\n<\/p>\n<p><p><strong>operator\u6a21\u5757<\/strong>\u63d0\u4f9b\u4e86\u6807\u51c6\u64cd\u4f5c\u7b26\u7684\u51fd\u6570\u5f62\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528operator.attrgetter()\u5c06\u5bf9\u8c61\u5c5e\u6027\u8f6c\u6362\u4e3a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import operator<\/p>\n<p>class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>people = [Person(&quot;Alice&quot;, 25), Person(&quot;Bob&quot;, 30), Person(&quot;Charlie&quot;, 35)]<\/p>\n<p>names = list(map(operator.attrgetter(&#39;name&#39;), people))<\/p>\n<p>print(names)  # \u8f93\u51fa [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>operator\u6a21\u5757\u9002\u5408\u5904\u7406\u9700\u8981\u6807\u51c6\u64cd\u4f5c\u7b26\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u5341\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u591a\u79cd\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5185\u7f6e\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u5e93\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u52bf\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387<\/strong>\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u5e2e\u52a9\u4f60\u5728\u5904\u7406\u6570\u636e\u65f6\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684<code>split()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u8be5\u65b9\u6cd5\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\u3002\u4f8b\u5982\uff0c<code>&quot;a,b,c&quot;.split(&quot;,&quot;)<\/code>\u5c06\u8fd4\u56de<code>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/code>\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u4ee5\u7a7a\u683c\u4e3a\u5206\u9694\u7b26\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u53ea\u9700\u5c06\u5143\u7ec4\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>list()<\/code>\u51fd\u6570\uff0c\u4f8b\u5982<code>my_tuple = (1, 2, 3)<\/code>\uff0c\u4f7f\u7528<code>my_list = list(my_tuple)<\/code>\u5373\u53ef\u5f97\u5230<code>[1, 2, 3]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5b57\u5178\u7684\u503c\u8f6c\u6362\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u4f7f\u7528<code>dict.values()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u63d0\u53d6\u5b57\u5178\u7684\u6240\u6709\u503c\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u7ed3\u5408<code>list()<\/code>\u51fd\u6570\uff0c\u4f8b\u5982\uff0c<code>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_list = list(my_dict.values())<\/code>\u5f97\u5230<code>[1, 2]<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u5b57\u5178\u4e2d\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u6570\u636e\u8f6c\u6362\u6210\u5217\u8868\u3002\u4f7f\u7528list()\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570split() [&hellip;]","protected":false},"author":3,"featured_media":1169424,"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\/1169417"}],"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=1169417"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169417\/revisions"}],"predecessor-version":[{"id":1169426,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169417\/revisions\/1169426"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1169424"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1169417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1169417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1169417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}