{"id":1187666,"date":"2025-01-15T20:08:14","date_gmt":"2025-01-15T12:08:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1187666.html"},"modified":"2025-01-15T20:08:17","modified_gmt":"2025-01-15T12:08:17","slug":"python%e5%a6%82%e4%bd%95%e8%bd%ac%e5%8c%96%e6%88%90%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1187666.html","title":{"rendered":"python\u5982\u4f55\u8f6c\u5316\u6210\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140201\/134c197c-6a67-4615-9a69-7cc88368eaa7.webp\" alt=\"python\u5982\u4f55\u8f6c\u5316\u6210\u5217\u8868\" \/><\/p>\n<p><p> <strong>Python\u4e2d\uff0c\u8f6c\u5316\u6210\u5217\u8868\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528list()\u51fd\u6570\u3001split()\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528list()\u51fd\u6570<\/strong>\u662f\u6700\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u3001\u5143\u7ec4\u3001\u96c6\u5408\u7b49\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528list()\u51fd\u6570\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528list()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>list()\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u5b83\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001\u5b57\u7b26\u4e32\u8f6c\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528list()\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u6bcf\u4e2a\u5b57\u7b26\u5c06\u6210\u4e3a\u5217\u8868\u7684\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;hello&quot;<\/p>\n<p>list_s = list(s)<\/p>\n<p>print(list_s)  # \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><h4>1.2\u3001\u5143\u7ec4\u8f6c\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5143\u7ec4\u4e5f\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528list()\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t = (1, 2, 3)<\/p>\n<p>list_t = list(t)<\/p>\n<p>print(list_t)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.3\u3001\u96c6\u5408\u8f6c\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u96c6\u5408\u4e5f\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528list()\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = {1, 2, 3}<\/p>\n<p>list_s = list(s)<\/p>\n<p>print(list_s)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528split()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>split()\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u6309\u6307\u5b9a\u5206\u9694\u7b26\u5206\u5272\u4e3a\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002\u5b83\u662f\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u4e00\u4e2a\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u6309\u7a7a\u683c\u5206\u5272\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0csplit()\u65b9\u6cd5\u6309\u7a7a\u683c\u5206\u5272\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;hello world&quot;<\/p>\n<p>list_s = s.split()<\/p>\n<p>print(list_s)  # \u8f93\u51fa: [&#39;hello&#39;, &#39;world&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2\u3001\u6309\u6307\u5b9a\u5206\u9694\u7b26\u5206\u5272\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u6307\u5b9a\u4efb\u610f\u5206\u9694\u7b26\u6765\u5206\u5272\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;1,2,3&quot;<\/p>\n<p>list_s = s.split(&#39;,&#39;)<\/p>\n<p>print(list_s)  # \u8f93\u51fa: [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\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\u521b\u5efa\u5217\u8868\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u4ece\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001\u4ece\u8303\u56f4\u751f\u6210\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528range()\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e00\u4e2a\u5305\u542b\u4e00\u5b9a\u8303\u56f4\u6570\u5b57\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_range = [x for x in range(5)]<\/p>\n<p>print(list_range)  # \u8f93\u51fa: [0, 1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2\u3001\u4ece\u53e6\u4e00\u4e2a\u5217\u8868\u751f\u6210\u65b0\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u4ece\u4e00\u4e2a\u5df2\u6709\u5217\u8868\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">old_list = [1, 2, 3]<\/p>\n<p>new_list = [x * 2 for x in old_list]<\/p>\n<p>print(new_list)  # \u8f93\u51fa: [2, 4, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528map()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>map()\u51fd\u6570\u7528\u4e8e\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u5230\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u901a\u8fc7\u5c06\u5176\u7ed3\u679c\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528map()\u51fd\u6570\u548cint()\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;1 2 3&quot;<\/p>\n<p>list_s = list(map(int, s.split()))<\/p>\n<p>print(list_s)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2\u3001\u5c06\u6d6e\u70b9\u6570\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528map()\u51fd\u6570\u548cint()\u51fd\u6570\u5c06\u6d6e\u70b9\u6570\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">float_list = [1.1, 2.2, 3.3]<\/p>\n<p>int_list = list(map(int, float_list))<\/p>\n<p>print(int_list)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528for\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1Python\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u7b80\u5316\u5217\u8868\u7684\u521b\u5efa\uff0c\u4f46\u6709\u65f6\u4f7f\u7528for\u5faa\u73af\u521b\u5efa\u5217\u8868\u53ef\u80fd\u66f4\u52a0\u76f4\u89c2\u548c\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h4>5.1\u3001\u5c06\u5b57\u7b26\u4e32\u8f6c\u4e3a\u5b57\u7b26\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7for\u5faa\u73af\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u6dfb\u52a0\u5230\u65b0\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;hello&quot;<\/p>\n<p>list_s = []<\/p>\n<p>for char in s:<\/p>\n<p>    list_s.append(char)<\/p>\n<p>print(list_s)  # \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><h4>5.2\u3001\u5c06\u5143\u7ec4\u8f6c\u4e3a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7for\u5faa\u73af\u5c06\u5143\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u65b0\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t = (1, 2, 3)<\/p>\n<p>list_t = []<\/p>\n<p>for item in t:<\/p>\n<p>    list_t.append(item)<\/p>\n<p>print(list_t)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528json.loads()<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406JSON\u683c\u5f0f\u7684\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528json.loads()\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5217\u8868\u3002\u9996\u5148\u9700\u8981\u5bfc\u5165json\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>6.1\u3001\u5c06JSON\u6570\u7ec4\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868<\/h4>\n<\/p>\n<p><p>json.loads()\u51fd\u6570\u7528\u4e8e\u5c06JSON\u5b57\u7b26\u4e32\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_str = &#39;[&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]&#39;<\/p>\n<p>list_fruits = json.loads(json_str)<\/p>\n<p>print(list_fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2\u3001\u5c06JSON\u5bf9\u8c61\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06JSON\u5bf9\u8c61\u5b57\u7b26\u4e32\u89e3\u6790\u4e3aPython\u5b57\u5178\uff0c\u5e76\u4ece\u5b57\u5178\u4e2d\u63d0\u53d6\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">json_str = &#39;{&quot;fruits&quot;: [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]}&#39;<\/p>\n<p>dict_obj = json.loads(json_str)<\/p>\n<p>list_fruits = dict_obj[&quot;fruits&quot;]<\/p>\n<p>print(list_fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528eval()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>eval()\u51fd\u6570\u7528\u4e8e\u6267\u884c\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\uff0c\u5e76\u8fd4\u56de\u8868\u8fbe\u5f0f\u7684\u503c\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528eval()\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u5b89\u5168\u6027\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>7.1\u3001\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7eval()\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8868\u793a\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u5b9e\u9645\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;[1, 2, 3]&quot;<\/p>\n<p>list_s = eval(s)<\/p>\n<p>print(list_s)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.2\u3001\u5c06\u590d\u6742\u7ed3\u6784\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528eval()\u51fd\u6570\u5c06\u590d\u6742\u7ed3\u6784\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;[[1, 2], [3, 4], [5, 6]]&quot;<\/p>\n<p>list_s = eval(s)<\/p>\n<p>print(list_s)  # \u8f93\u51fa: [[1, 2], [3, 4], [5, 6]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528ast.literal_eval()<\/h3>\n<\/p>\n<p><p>ast.literal_eval()\u51fd\u6570\u662f\u4e00\u4e2a\u66f4\u5b89\u5168\u7684\u66ff\u4ee3eval()\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\u8f6c\u6362\u4e3aPython\u5bf9\u8c61\u3002\u9996\u5148\u9700\u8981\u5bfc\u5165ast\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>8.1\u3001\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7ast.literal_eval()\u5c06\u5b57\u7b26\u4e32\u8868\u793a\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u5b9e\u9645\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>s = &quot;[1, 2, 3]&quot;<\/p>\n<p>list_s = ast.literal_eval(s)<\/p>\n<p>print(list_s)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>8.2\u3001\u5c06\u590d\u6742\u7ed3\u6784\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528ast.literal_eval()\u5c06\u590d\u6742\u7ed3\u6784\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>s = &quot;[[1, 2], [3, 4], [5, 6]]&quot;<\/p>\n<p>list_s = ast.literal_eval(s)<\/p>\n<p>print(list_s)  # \u8f93\u51fa: [[1, 2], [3, 4], [5, 6]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u503c\u6570\u636e\u65f6\uff0cnumpy\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5\u5e76\u5bfc\u5165numpy\u5e93\u3002<\/p>\n<\/p>\n<p><h4>9.1\u3001\u5c06numpy\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7tolist()\u65b9\u6cd5\u5c06numpy\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>np_array = np.array([1, 2, 3])<\/p>\n<p>list_array = np_array.tolist()<\/p>\n<p>print(list_array)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>9.2\u3001\u5c06\u591a\u7ef4numpy\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7tolist()\u65b9\u6cd5\u5c06\u591a\u7ef4numpy\u6570\u7ec4\u8f6c\u6362\u4e3a\u5d4c\u5957\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>np_array = np.array([[1, 2], [3, 4], [5, 6]])<\/p>\n<p>list_array = np_array.tolist()<\/p>\n<p>print(list_array)  # \u8f93\u51fa: [[1, 2], [3, 4], [5, 6]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u5206\u6790\u4efb\u52a1\u65f6\uff0cpandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u548c\u5206\u6790\u5de5\u5177\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u5c06DataFrame\u6216Series\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5\u5e76\u5bfc\u5165pandas\u5e93\u3002<\/p>\n<\/p>\n<p><h4>10.1\u3001\u5c06pandas DataFrame\u8f6c\u6362\u4e3a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7values\u5c5e\u6027\u548ctolist()\u65b9\u6cd5\u5c06pandas DataFrame\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]})<\/p>\n<p>list_df = df.values.tolist()<\/p>\n<p>print(list_df)  # \u8f93\u51fa: [[1, 2, 3], [4, 5, 6]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>10.2\u3001\u5c06pandas Series\u8f6c\u6362\u4e3a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7tolist()\u65b9\u6cd5\u5c06pandas Series\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>s = pd.Series([1, 2, 3])<\/p>\n<p>list_s = s.tolist()<\/p>\n<p>print(list_s)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528list()\u51fd\u6570\u3001split()\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001map()\u51fd\u6570\u3001for\u5faa\u73af\u3001json.loads()\u3001eval()\u51fd\u6570\u3001ast.literal_eval()\u3001numpy\u5e93\u548cpandas\u5e93\u3002<strong>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3aPython\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5217\u8868\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u591a\u4e2a\u90e8\u5206\u3002\u4f8b\u5982\uff0c<code>&quot;apple,banana,cherry&quot;.split(&quot;,&quot;)<\/code>\u4f1a\u8fd4\u56de<code>[&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/code>\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u4f1a\u6309\u7a7a\u683c\u62c6\u5206\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u5316\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u652f\u6301\u5c06\u591a\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u5316\u4e3a\u5217\u8868\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u5143\u7ec4\u3001\u96c6\u5408\u6216\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5217\u8868\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c<code>list((1, 2, 3))<\/code>\u4f1a\u8fd4\u56de<code>[1, 2, 3]<\/code>\uff0c\u800c<code>list(&quot;hello&quot;)<\/code>\u4f1a\u8fd4\u56de<code>[&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u5316\u4e3a\u7279\u5b9a\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u6bd4\u5982\uff0c\u5982\u679c\u60a8\u60f3\u5c06\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u5b57\u7b26\u4e32\u7684\u5217\u8868\u8f6c\u5316\u4e3a\u6574\u6570\u5217\u8868\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>numbers = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;]<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>int(num) for num in numbers<\/code>\u6765\u751f\u6210\u65b0\u7684\u5217\u8868\uff0c\u5373<code>[1, 2, 3]<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\uff0c\u8f6c\u5316\u6210\u5217\u8868\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528list()\u51fd\u6570\u3001split()\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528lis [&hellip;]","protected":false},"author":3,"featured_media":1187673,"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\/1187666"}],"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=1187666"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187666\/revisions"}],"predecessor-version":[{"id":1187676,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187666\/revisions\/1187676"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1187673"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1187666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1187666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1187666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}