{"id":1098248,"date":"2025-01-08T15:19:26","date_gmt":"2025-01-08T07:19:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098248.html"},"modified":"2025-01-08T15:19:30","modified_gmt":"2025-01-08T07:19:30","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%94%b9%e4%b8%ba%e6%95%b0%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098248.html","title":{"rendered":"python\u5982\u4f55\u628a\u5b57\u7b26\u4e32\u6539\u4e3a\u6570\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212641\/600cec6f-a290-4c26-82e6-90fb931f8414.webp\" alt=\"python\u5982\u4f55\u628a\u5b57\u7b26\u4e32\u6539\u4e3a\u6570\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528int()\u3001float()\u548ceval()\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u503c<\/strong>\u3002\u5176\u4e2d\uff0cint()\u7528\u4e8e\u8f6c\u6362\u4e3a\u6574\u6570\uff0cfloat()\u7528\u4e8e\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0ceval()\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u6c42\u503c\u3002\u5177\u4f53\u6765\u8bf4\uff0cint()\u51fd\u6570\u9002\u7528\u4e8e\u6574\u6570\u5f62\u5f0f\u7684\u5b57\u7b26\u4e32\uff0cfloat()\u51fd\u6570\u9002\u7528\u4e8e\u5305\u542b\u5c0f\u6570\u7684\u5b57\u7b26\u4e32\uff0c\u800ceval()\u51fd\u6570\u5219\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684int()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>int()\u51fd\u6570\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/strong>\u3002\u5f53\u5b57\u7b26\u4e32\u8868\u793a\u4e00\u4e2a\u6574\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528int()\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32&quot;123&quot;\u8f6c\u6362\u4e3a\u6574\u6570123\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u7b26\u4e32\u5fc5\u987b\u8868\u793a\u6709\u6548\u7684\u6574\u6570\uff0c\u5426\u5219\u4f1a\u5f15\u53d1ValueError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/p>\n<p>num_str = &quot;123&quot;<\/p>\n<p>num_int = int(num_str)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001int()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>int()\u51fd\u6570\u662fPython\u4e2d\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e0d\u7b26\u5408\u6574\u6570\u7684\u683c\u5f0f\uff0c\u5219\u4f1a\u5f15\u53d1ValueError\u5f02\u5e38\u3002\u4e0b\u9762\u662f\u4f7f\u7528int()\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u5177\u4f53\u6b65\u9aa4\u548c\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528int()\u51fd\u6570\u8f6c\u6362\u6574\u6570\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5f53\u5b57\u7b26\u4e32\u8868\u793a\u4e00\u4e2a\u6574\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528int()\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u7b26\u4e32\u5fc5\u987b\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\uff0c\u5e76\u4e14\u4e0d\u80fd\u5305\u542b\u5c0f\u6570\u70b9\u6216\u5176\u4ed6\u975e\u6570\u5b57\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/p>\n<p>num_str = &quot;123&quot;<\/p>\n<p>num_int = int(num_str)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5904\u7406\u65e0\u6548\u7684\u6574\u6570\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5b57\u7b26\u4e32\u4e0d\u7b26\u5408\u6574\u6570\u7684\u683c\u5f0f\uff0cint()\u51fd\u6570\u4f1a\u5f15\u53d1ValueError\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u8f6c\u6362\u4e4b\u524d\uff0c\u6700\u597d\u8fdb\u884c\u4e00\u4e9b\u9a8c\u8bc1\uff0c\u6216\u8005\u4f7f\u7528try-except\u8bed\u53e5\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;123a&quot;<\/p>\n<p>try:<\/p>\n<p>    num_int = int(num_str)<\/p>\n<p>    print(num_int)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5904\u7406\u5e26\u7b26\u53f7\u7684\u6574\u6570\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>int()\u51fd\u6570\u4e5f\u53ef\u4ee5\u5904\u7406\u5e26\u7b26\u53f7\u7684\u6574\u6570\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\u6b63\u8d1f\u53f7\u3002\u53ea\u8981\u5b57\u7b26\u4e32\u683c\u5f0f\u6b63\u786e\uff0cint()\u51fd\u6570\u90fd\u80fd\u6b63\u786e\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;-123&quot;<\/p>\n<p>num_int = int(num_str)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: -123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001float()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>float()\u51fd\u6570\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u5982\u679c\u5b57\u7b26\u4e32\u8868\u793a\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528float()\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u540c\u6837\u5730\uff0c\u5b57\u7b26\u4e32\u5fc5\u987b\u8868\u793a\u6709\u6548\u7684\u6d6e\u70b9\u6570\u683c\u5f0f\uff0c\u5426\u5219\u4f1a\u5f15\u53d1ValueError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528float()\u51fd\u6570\u8f6c\u6362\u6d6e\u70b9\u6570\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5f53\u5b57\u7b26\u4e32\u8868\u793a\u4e00\u4e2a\u6d6e\u70b9\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528float()\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u7b26\u4e32\u5fc5\u987b\u7b26\u5408\u6d6e\u70b9\u6570\u7684\u683c\u5f0f\uff0c\u53ef\u4ee5\u5305\u542b\u5c0f\u6570\u70b9\u548c\u79d1\u5b66\u8ba1\u6570\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/p>\n<p>num_str = &quot;123.45&quot;<\/p>\n<p>num_float = float(num_str)<\/p>\n<p>print(num_float)  # \u8f93\u51fa: 123.45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5904\u7406\u65e0\u6548\u7684\u6d6e\u70b9\u6570\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5b57\u7b26\u4e32\u4e0d\u7b26\u5408\u6d6e\u70b9\u6570\u7684\u683c\u5f0f\uff0cfloat()\u51fd\u6570\u4f1a\u5f15\u53d1ValueError\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u8f6c\u6362\u4e4b\u524d\uff0c\u6700\u597d\u8fdb\u884c\u4e00\u4e9b\u9a8c\u8bc1\uff0c\u6216\u8005\u4f7f\u7528try-except\u8bed\u53e5\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;123.45a&quot;<\/p>\n<p>try:<\/p>\n<p>    num_float = float(num_str)<\/p>\n<p>    print(num_float)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5904\u7406\u5e26\u7b26\u53f7\u7684\u6d6e\u70b9\u6570\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>float()\u51fd\u6570\u4e5f\u53ef\u4ee5\u5904\u7406\u5e26\u7b26\u53f7\u7684\u6d6e\u70b9\u6570\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\u6b63\u8d1f\u53f7\u3002\u53ea\u8981\u5b57\u7b26\u4e32\u683c\u5f0f\u6b63\u786e\uff0cfloat()\u51fd\u6570\u90fd\u80fd\u6b63\u786e\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;-123.45&quot;<\/p>\n<p>num_float = float(num_str)<\/p>\n<p>print(num_float)  # \u8f93\u51fa: -123.45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001eval()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>eval()\u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u6c42\u503c\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002\u867d\u7136eval()\u51fd\u6570\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\u4f7f\u7528eval()\u5b58\u5728\u5b89\u5168\u98ce\u9669\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6267\u884c\u4efb\u610f\u4ee3\u7801\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528eval()\u65f6\u8981\u683c\u5916\u5c0f\u5fc3\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528eval()\u51fd\u6570\u8f6c\u6362\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5f53\u5b57\u7b26\u4e32\u8868\u793a\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u503c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528eval()\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u503c\u3002eval()\u53ef\u4ee5\u5904\u7406\u6574\u6570\u3001\u6d6e\u70b9\u6570\u548c\u590d\u6742\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u6c42\u503c<\/p>\n<p>num_str = &quot;123 + 45&quot;<\/p>\n<p>num_eval = eval(num_str)<\/p>\n<p>print(num_eval)  # \u8f93\u51fa: 168<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5904\u7406\u590d\u6742\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>eval()\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u7b80\u5355\u7684\u6570\u503c\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u8868\u8fbe\u5f0f\uff0c\u4f8b\u5982\u5305\u542b\u8fd0\u7b97\u7b26\u3001\u51fd\u6570\u8c03\u7528\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">expression_str = &quot;3 * (2 + 5)&quot;<\/p>\n<p>result = eval(expression_str)<\/p>\n<p>print(result)  # \u8f93\u51fa: 21<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6ce8\u610feval()\u51fd\u6570\u7684\u5b89\u5168\u6027<\/h4>\n<\/p>\n<p><p>\u7531\u4e8eeval()\u51fd\u6570\u53ef\u4ee5\u6267\u884c\u4efb\u610f\u4ee3\u7801\uff0c\u5728\u5904\u7406\u4e0d\u53d7\u4fe1\u4efb\u7684\u8f93\u5165\u65f6\uff0c\u4f7f\u7528eval()\u5b58\u5728\u5b89\u5168\u98ce\u9669\u3002\u4e3a\u4e86\u907f\u514d\u5b89\u5168\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528ast.literal_eval()\u51fd\u6570\uff0c\u5b83\u53ea\u4f1a\u8ba1\u7b97\u5b57\u9762\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>num_str = &quot;123&quot;<\/p>\n<p>num_literal = ast.literal_eval(num_str)<\/p>\n<p>print(num_literal)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u503c\u53ef\u4ee5\u4f7f\u7528int()\u3001float()\u548ceval()\u51fd\u6570\u3002int()\u51fd\u6570\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0cfloat()\u51fd\u6570\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u800ceval()\u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u6c42\u503c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u7b26\u4e32\u5fc5\u987b\u7b26\u5408\u76f8\u5e94\u7684\u6570\u503c\u683c\u5f0f\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u6b64\u5916\uff0c\u5728\u4f7f\u7528eval()\u51fd\u6570\u65f6\u8981\u6ce8\u610f\u5b89\u5168\u95ee\u9898\uff0c\u5c3d\u91cf\u907f\u514d\u5904\u7406\u4e0d\u53d7\u4fe1\u4efb\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u503c\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u7f16\u7a0b\u9700\u6c42\u3002\u65e0\u8bba\u662f\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u3001\u6570\u636e\u89e3\u6790\u8fd8\u662f\u5176\u4ed6\u573a\u666f\u4e2d\uff0c\u90fd\u53ef\u4ee5\u6709\u6548\u5730\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u6570\u503c\u7c7b\u578b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>int()<\/code>\u548c<code>float()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002\u5bf9\u4e8e\u6574\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528<code>int()<\/code>\u5e76\u4f20\u5165\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982<code>int(&quot;123&quot;)<\/code>\u5c06\u8fd4\u56de123\u3002\u5bf9\u4e8e\u6d6e\u70b9\u6570\uff0c\u4f7f\u7528<code>float()<\/code>\u51fd\u6570\uff0c\u5982<code>float(&quot;123.45&quot;)<\/code>\u5c06\u8fd4\u56de123.45\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u7b26\u4e32\u5fc5\u987b\u80fd\u591f\u88ab\u6b63\u786e\u89e3\u6790\u4e3a\u6570\u5b57\uff0c\u5426\u5219\u4f1a\u629b\u51fa<code>ValueError<\/code>\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6570\u503c\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5f53\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u503c\u65f6\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u5185\u5bb9\u4e0d\u7b26\u5408\u6570\u5b57\u683c\u5f0f\uff0cPython\u4f1a\u629b\u51fa<code>ValueError<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    number = int(&quot;abc&quot;)\nexcept ValueError:\n    print(&quot;\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6570\u5b57&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u6709\u6548\u5904\u7406\u4e0d\u7b26\u5408\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u5e76\u907f\u514d\u7a0b\u5e8f\u7684\u4e2d\u65ad\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u540c\u65f6\u8f6c\u6362\u4e3a\u6570\u503c\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>map()<\/code>\u51fd\u6570\u6765\u6279\u91cf\u8f6c\u6362\u5b57\u7b26\u4e32\u4e3a\u6570\u503c\u3002\u4f8b\u5982\uff0c\u82e5\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868<code>str_list = [&quot;1&quot;, &quot;2.5&quot;, &quot;3&quot;]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u5176\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff1a  <\/p>\n<pre><code class=\"language-python\">float_list = [float(num) for num in str_list]\n<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\uff1a  <\/p>\n<pre><code class=\"language-python\">float_list = list(map(float, str_list))\n<\/code><\/pre>\n<p>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u6570\u503c\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528int()\u3001float()\u548ceval()\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u503c\u3002\u5176\u4e2d\uff0cint()\u7528\u4e8e [&hellip;]","protected":false},"author":3,"featured_media":1098259,"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\/1098248"}],"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=1098248"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098248\/revisions"}],"predecessor-version":[{"id":1098263,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098248\/revisions\/1098263"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098259"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}