{"id":1146720,"date":"2025-01-08T23:19:16","date_gmt":"2025-01-08T15:19:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1146720.html"},"modified":"2025-01-08T23:19:19","modified_gmt":"2025-01-08T15:19:19","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%ae%a1%e7%ae%97%e4%b8%80%e5%85%83%e4%ba%8c%e6%ac%a1%e6%96%b9%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1146720.html","title":{"rendered":"\u5982\u4f55\u7528python\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182641\/f998a718-4b30-43fa-8d98-b73bf8449a60.webp\" alt=\"\u5982\u4f55\u7528python\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b<\/strong>  <\/p>\n<\/p>\n<p><p><strong>\u7528Python\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u6807\u51c6\u516c\u5f0f\u3001NumPy\u5e93\u3001SymPy\u5e93\u7b49\u3002<\/strong> \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u6765\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u89e3\u3002\u5176\u4e2d\uff0c\u6807\u51c6\u516c\u5f0f\u662f\u6700\u57fa\u7840\u7684\u8ba1\u7b97\u65b9\u5f0f\uff0c\u800cNumPy\u548cSymPy\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u548c\u4fbf\u6377\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6807\u51c6\u516c\u5f0f\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6807\u51c6\u516c\u5f0f\u6cd5\u662f\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u6700\u57fa\u7840\u7684\u65b9\u6cd5\u3002\u5728\u6570\u5b66\u4e0a\uff0c\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u4e00\u822c\u5f62\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ ax^2 + bx + c = 0 ]<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0ca\u3001b\u3001c\u4e3a\u5e38\u6570\uff0c\u4e14a\u22600\u3002\u5176\u89e3\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u516c\u5f0f\u6c42\u5f97\uff1a<\/p>\n<\/p>\n<p><p>[ x = \\frac{-b \\pm \\sqrt{b^2 &#8211; 4ac}}{2a} ]<\/p>\n<\/p>\n<p><h4>1.1 \u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684<code>math<\/code>\u6a21\u5757\u6765\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u3002\u4ee5\u4e0b\u662f\u5bfc\u5165<code>math<\/code>\u6a21\u5757\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u5b9a\u4e49\u65b9\u7a0b\u7684\u7cfb\u6570<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u65b9\u7a0b\u7684\u7cfb\u6570a\u3001b\u548cc\uff0c\u53ef\u4ee5\u901a\u8fc7\u7528\u6237\u8f93\u5165\u6216\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u8d4b\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = float(input(&quot;\u8bf7\u8f93\u5165\u7cfb\u6570a: &quot;))<\/p>\n<p>b = float(input(&quot;\u8bf7\u8f93\u5165\u7cfb\u6570b: &quot;))<\/p>\n<p>c = float(input(&quot;\u8bf7\u8f93\u5165\u7cfb\u6570c: &quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.3 \u8ba1\u7b97\u5224\u522b\u5f0f<\/h4>\n<\/p>\n<p><p>\u5224\u522b\u5f0f\u0394\u51b3\u5b9a\u4e86\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u89e3\u7684\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><p>[ \\Delta = b^2 &#8211; 4ac ]<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">delta = b2 - 4*a*c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.4 \u6c42\u89e3\u65b9\u7a0b<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u5224\u522b\u5f0f\u7684\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u5224\u65ad\u65b9\u7a0b\u6709\u4e24\u4e2a\u5b9e\u6839\u3001\u4e00\u4e2a\u5b9e\u6839\u6216\u65e0\u5b9e\u6839\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if delta &gt; 0:<\/p>\n<p>    x1 = (-b + math.sqrt(delta)) \/ (2*a)<\/p>\n<p>    x2 = (-b - math.sqrt(delta)) \/ (2*a)<\/p>\n<p>    print(f&quot;\u65b9\u7a0b\u6709\u4e24\u4e2a\u5b9e\u6839\uff1ax1 = {x1}, x2 = {x2}&quot;)<\/p>\n<p>elif delta == 0:<\/p>\n<p>    x = -b \/ (2*a)<\/p>\n<p>    print(f&quot;\u65b9\u7a0b\u6709\u4e00\u4e2a\u5b9e\u6839\uff1ax = {x}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u65b9\u7a0b\u65e0\u5b9e\u6839&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u51fd\u6570\u6765\u89e3\u51b3\u6570\u5b66\u95ee\u9898\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u89e3\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5b89\u88c5NumPy<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5c1a\u672a\u5b89\u88c5NumPy\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u5bfc\u5165NumPy\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.3 \u4f7f\u7528NumPy\u7684roots\u51fd\u6570<\/h4>\n<\/p>\n<p><p>NumPy\u7684<code>roots<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u6c42\u89e3\u591a\u9879\u5f0f\u65b9\u7a0b\u7684\u6839\u3002\u6211\u4eec\u53ea\u9700\u63d0\u4f9b\u65b9\u7a0b\u7684\u7cfb\u6570\u5373\u53ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">coefficients = [a, b, c]<\/p>\n<p>roots = np.roots(coefficients)<\/p>\n<p>print(f&quot;\u65b9\u7a0b\u7684\u6839\uff1a{roots}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528SymPy\u5e93<\/h3>\n<\/p>\n<p><p>SymPy\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u7b26\u53f7\u8ba1\u7b97\u7684Python\u5e93\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u7b26\u53f7\u8fd0\u7b97\u548c\u89e3\u6790\u6c42\u89e3\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5SymPy<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5c1a\u672a\u5b89\u88c5SymPy\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install sympy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u5bfc\u5165SymPy\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sympy as sp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.3 \u5b9a\u4e49\u65b9\u7a0b\u5e76\u6c42\u89e3<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528SymPy\u5e93\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u975e\u5e38\u65b9\u4fbf\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528<code>symbols<\/code>\u51fd\u6570\u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf\uff0c\u5e76\u4f7f\u7528<code>solve<\/code>\u51fd\u6570\u6c42\u89e3\u65b9\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = sp.symbols(&#39;x&#39;)<\/p>\n<p>equation = a*x2 + b*x + c<\/p>\n<p>solutions = sp.solve(equation, x)<\/p>\n<p>print(f&quot;\u65b9\u7a0b\u7684\u89e3\uff1a{solutions}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u89e3\uff0c\u5305\u62ec\u6807\u51c6\u516c\u5f0f\u6cd5\u3001\u4f7f\u7528NumPy\u5e93\u548c\u4f7f\u7528SymPy\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong>\u6807\u51c6\u516c\u5f0f\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u7b80\u5355\u7684\u8ba1\u7b97\uff0c\u4ee3\u7801\u91cf\u5c11\uff0c\u4f46\u9700\u8981\u624b\u52a8\u8ba1\u7b97\u5224\u522b\u5f0f\u548c\u6839\u3002<\/p>\n<\/p>\n<p><p><strong>NumPy\u5e93<\/strong>\uff1a\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u548c\u590d\u6742\u8ba1\u7b97\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong>SymPy\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u7b26\u53f7\u8ba1\u7b97\u548c\u89e3\u6790\u6c42\u89e3\uff0c\u4ee3\u7801\u7b80\u6d01\uff0c\u529f\u80fd\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u60a8\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u89e3\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u80fd\u591f\u7075\u6d3b\u8fd0\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u6839\uff1f<\/strong><br \/>\u8981\u7528Python\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u6839\uff0c\u53ef\u4ee5\u5229\u7528\u6570\u5b66\u516c\u5f0f\u8fdb\u884c\u8ba1\u7b97\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u5b9a\u4e49\u65b9\u7a0b\u7684\u7cfb\u6570\uff0c\u7136\u540e\u4f7f\u7528\u6c42\u6839\u516c\u5f0f\uff08\u5373\u6c42\u89e3ax\u00b2 + bx + c = 0\u7684\u6839\uff09\u8fdb\u884c\u8ba1\u7b97\u3002\u53ef\u4ee5\u4f7f\u7528<code>cmath<\/code>\u5e93\u6765\u5904\u7406\u590d\u6570\u6839\u7684\u60c5\u51b5\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import cmath\n\ndef solve_quadratic(a, b, c):\n    d = (b**2) - (4*a*c)\n    root1 = (-b + cmath.sqrt(d)) \/ (2*a)\n    root2 = (-b - cmath.sqrt(d)) \/ (2*a)\n    return root1, root2\n\n# \u793a\u4f8b\na, b, c = 1, 5, 6\nroots = solve_quadratic(a, b, c)\nprint(&quot;\u65b9\u7a0b\u7684\u6839\u4e3a:&quot;, roots)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u590d\u6570\u89e3\uff1f<\/strong><br \/>\u5904\u7406\u590d\u6570\u89e3\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cmath<\/code>\u5e93\u800c\u4e0d\u662f<code>math<\/code>\u5e93\uff0c\u56e0\u4e3a<code>cmath<\/code>\u652f\u6301\u590d\u6570\u8fd0\u7b97\u3002\u5f53\u5224\u522b\u5f0f\u5c0f\u4e8e\u96f6\u65f6\uff0c\u65b9\u7a0b\u5c06\u4ea7\u751f\u590d\u6570\u6839\u3002\u901a\u8fc7\u4f7f\u7528<code>cmath.sqrt()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u6b63\u786e\u8ba1\u7b97\u51fa\u590d\u6570\u6839\u3002\u4f8b\u5982\uff0c\u5982\u679c\u65b9\u7a0b\u7684\u7cfb\u6570\u662f1, 2, 5\uff0c\u5224\u522b\u5f0f\u5c06\u4e3a\u8d1f\uff0c\u56e0\u6b64\u6839\u5c06\u662f\u590d\u6570\u3002\u5b8c\u6574\u4ee3\u7801\u548c\u8ba1\u7b97\u65b9\u6cd5\u4e0e\u524d\u9762\u63d0\u5230\u7684\u76f8\u4f3c\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5e93\u6765\u7b80\u5316\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u6c42\u89e3\u8fc7\u7a0b\uff1f<\/strong><br \/>\u786e\u5b9e\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\uff0c\u6bd4\u5982<code>numpy<\/code>\u6216\u8005<code>sympy<\/code>\uff0c\u6765\u7b80\u5316\u6c42\u89e3\u8fc7\u7a0b\u3002<code>numpy<\/code>\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u6570\u503c\u8ba1\u7b97\u529f\u80fd\uff0c\u800c<code>sympy<\/code>\u5219\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7b26\u53f7\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bf9\u65b9\u7a0b\u8fdb\u884c\u89e3\u6790\u89e3\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>sympy<\/code>\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code class=\"language-python\">from sympy import symbols, Eq, solve\n\nx = symbols(&#39;x&#39;)\nequation = Eq(a*x**2 + b*x + c, 0)\nsolutions = solve(equation, x)\nprint(&quot;\u65b9\u7a0b\u7684\u89e3\u4e3a:&quot;, solutions)\n<\/code><\/pre>\n<p>\u4f7f\u7528\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u4f7f\u8ba1\u7b97\u66f4\u4e3a\u7b80\u6d01\u548c\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b \u7528Python\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u6807\u51c6\u516c\u5f0f\u3001NumPy\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1146732,"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\/1146720"}],"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=1146720"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146720\/revisions"}],"predecessor-version":[{"id":1146736,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146720\/revisions\/1146736"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1146732"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1146720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1146720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1146720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}