{"id":1145920,"date":"2025-01-08T23:12:24","date_gmt":"2025-01-08T15:12:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1145920.html"},"modified":"2025-01-08T23:12:27","modified_gmt":"2025-01-08T15:12:27","slug":"python%e5%a6%82%e4%bd%95%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\/1145920.html","title":{"rendered":"python\u5982\u4f55\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\/24182245\/a1ddaaf7-ce21-420d-a637-46f8933f80d2.webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\" \/><\/p>\n<p><p> <strong>Python\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u3001\u5229\u7528NumPy\u5e93\u3001\u91c7\u7528SymPy\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c\u5229\u7528\u6570\u5b66\u516c\u5f0f\u662f\u6700\u5e38\u89c1\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528Python\u5185\u7f6e\u7684\u6570\u5b66\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u6839\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u79cd\u65b9\u6cd5\u6765\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5229\u7528\u6570\u5b66\u516c\u5f0f\u6c42\u89e3<\/h2>\n<\/p>\n<p><h3>1. \u516c\u5f0f\u80cc\u666f\u53ca\u57fa\u672c\u539f\u7406<\/h3>\n<\/p>\n<p><p>\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u6807\u51c6\u5f62\u5f0f\u4e3a (ax^2 + bx + c = 0)\u3002\u6839\u636e\u6c42\u6839\u516c\u5f0f\uff1a<\/p>\n<p>[ x = \\frac{-b \\pm \\sqrt{b^2 &#8211; 4ac}}{2a} ]<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u8ba1\u7b97\u51fa\u65b9\u7a0b\u7684\u4e24\u4e2a\u6839\u3002\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u65b9\u7a0b\u7684\u6839\u6570\u53d6\u51b3\u4e8e\u5224\u522b\u5f0f (b^2 &#8211; 4ac) \u7684\u503c\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5224\u522b\u5f0f\u5927\u4e8e0<\/strong>\uff1a\u65b9\u7a0b\u6709\u4e24\u4e2a\u4e0d\u540c\u7684\u5b9e\u6839\u3002<\/li>\n<li><strong>\u5224\u522b\u5f0f\u7b49\u4e8e0<\/strong>\uff1a\u65b9\u7a0b\u6709\u4e00\u4e2a\u5b9e\u6839\uff08\u4e24\u4e2a\u76f8\u540c\u7684\u5b9e\u6839\uff09\u3002<\/li>\n<li><strong>\u5224\u522b\u5f0f\u5c0f\u4e8e0<\/strong>\uff1a\u65b9\u7a0b\u65e0\u5b9e\u6839\uff08\u6709\u4e24\u4e2a\u865a\u6839\uff09\u3002<\/li>\n<\/ul>\n<p><h3>2. \u5b9e\u73b0\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u5229\u7528\u6570\u5b66\u516c\u5f0f\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684Python\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def solve_quadratic(a, b, c):<\/p>\n<p>    # \u8ba1\u7b97\u5224\u522b\u5f0f<\/p>\n<p>    discriminant = b2 - 4*a*c<\/p>\n<p>    if discriminant &gt; 0:<\/p>\n<p>        root1 = (-b + math.sqrt(discriminant)) \/ (2*a)<\/p>\n<p>        root2 = (-b - math.sqrt(discriminant)) \/ (2*a)<\/p>\n<p>        return root1, root2<\/p>\n<p>    elif discriminant == 0:<\/p>\n<p>        root = -b \/ (2*a)<\/p>\n<p>        return root,<\/p>\n<p>    else:<\/p>\n<p>        real_part = -b \/ (2*a)<\/p>\n<p>        imaginary_part = math.sqrt(-discriminant) \/ (2*a)<\/p>\n<p>        return (real_part + imaginary_part*1j, real_part - imaginary_part*1j)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>a = 1<\/p>\n<p>b = -3<\/p>\n<p>c = 2<\/p>\n<p>roots = solve_quadratic(a, b, c)<\/p>\n<p>print(&quot;The roots of the quadratic equation are:&quot;, roots)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/h2>\n<\/p>\n<p><h3>1. NumPy\u5e93\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u6570\u5b66\u8fd0\u7b97\u51fd\u6570\u3002\u5229\u7528NumPy\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u7b80\u6d01\u5730\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u548c\u7ebf\u6027\u4ee3\u6570\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>2. \u5b9e\u73b0\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528NumPy\u5e93\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def solve_quadratic_numpy(a, b, c):<\/p>\n<p>    coefficients = [a, b, c]<\/p>\n<p>    roots = np.roots(coefficients)<\/p>\n<p>    return roots<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>a = 1<\/p>\n<p>b = -3<\/p>\n<p>c = 2<\/p>\n<p>roots = solve_quadratic_numpy(a, b, c)<\/p>\n<p>print(&quot;The roots of the quadratic equation are:&quot;, roots)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528SymPy\u5e93<\/h2>\n<\/p>\n<p><h3>1. SymPy\u5e93\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>SymPy\u662fPython\u4e2d\u4e00\u4e2a\u7528\u4e8e\u7b26\u53f7\u8ba1\u7b97\u7684\u5e93\uff0c\u53ef\u4ee5\u8fdb\u884c\u4ee3\u6570\u65b9\u7a0b\u6c42\u89e3\u3001\u5fae\u79ef\u5206\u3001\u77e9\u9635\u4ee3\u6570\u7b49\u64cd\u4f5c\u3002\u5229\u7528SymPy\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u6c42\u89e3\u65b9\u7a0b\uff0c\u800c\u4e0d\u9700\u8981\u624b\u52a8\u8ba1\u7b97\u5224\u522b\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>2. \u5b9e\u73b0\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528SymPy\u5e93\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, Eq, solve<\/p>\n<p>def solve_quadratic_sympy(a, b, c):<\/p>\n<p>    x = symbols(&#39;x&#39;)<\/p>\n<p>    equation = Eq(a*x2 + b*x + c, 0)<\/p>\n<p>    roots = solve(equation, x)<\/p>\n<p>    return roots<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>a = 1<\/p>\n<p>b = -3<\/p>\n<p>c = 2<\/p>\n<p>roots = solve_quadratic_sympy(a, b, c)<\/p>\n<p>print(&quot;The roots of the quadratic equation are:&quot;, roots)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5404\u65b9\u6cd5\u6bd4\u8f83<\/h2>\n<\/p>\n<p><h3>1. \u6570\u5b66\u516c\u5f0f\u6cd5<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u5408\u521d\u5b66\u8005\uff0c\u7406\u89e3\u5e95\u5c42\u6570\u5b66\u539f\u7406\u3002<\/p>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a\u9700\u8981\u624b\u52a8\u8ba1\u7b97\u5224\u522b\u5f0f\uff0c\u4ee3\u7801\u8f83\u591a\u3002<\/p>\n<\/p>\n<p><h3>2. NumPy\u5e93<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\uff0c\u9002\u5408\u9700\u8981\u8fdb\u884c\u5927\u91cf\u79d1\u5b66\u8ba1\u7b97\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u5bf9\u521d\u5b66\u8005\u6709\u4e00\u5b9a\u95e8\u69db\u3002<\/p>\n<\/p>\n<p><h3>3. SymPy\u5e93<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u7b26\u53f7\u8ba1\u7b97\u5f3a\u5927\uff0c\u53ef\u4ee5\u76f4\u63a5\u6c42\u89e3\u65b9\u7a0b\uff0c\u9002\u5408\u590d\u6742\u7684\u4ee3\u6570\u8fd0\u7b97\u3002<\/p>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a\u5e93\u8f83\u5927\uff0c\u5b66\u4e60\u66f2\u7ebf\u7a0d\u9661\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u4e09\u79cd\u65b9\u6cd5\uff1a\u5229\u7528\u6570\u5b66\u516c\u5f0f\u3001\u4f7f\u7528NumPy\u5e93\u548c\u91c7\u7528SymPy\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u7f3a\u70b9\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u4e2a\u4eba\u4e60\u60ef\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u5728\u6570\u5b66\u8ba1\u7b97\u4e2d\u7684\u5e94\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 \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6c42\u6839\u516c\u5f0f\u6765\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b ( ax^2 + bx + c = 0 )\u3002\u5177\u4f53\u6b65\u9aa4\u4e3a\uff1a\u5bfc\u5165\u6570\u5b66\u5e93\uff0c\u8ba1\u7b97\u5224\u522b\u5f0f ( D = b^2 &#8211; 4ac )\u3002\u82e5 ( D &gt; 0 )\uff0c\u5219\u65b9\u7a0b\u6709\u4e24\u4e2a\u4e0d\u540c\u7684\u5b9e\u6839\uff1b\u82e5 ( D = 0 )\uff0c\u5219\u6709\u4e00\u4e2a\u5b9e\u6839\uff1b\u82e5 ( D &lt; 0 )\uff0c\u5219\u65b9\u7a0b\u6ca1\u6709\u5b9e\u6570\u6839\u3002\u53ef\u4ee5\u4f7f\u7528<code>math.sqrt()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\uff0c\u6700\u7ec8\u901a\u8fc7\u6c42\u6839\u516c\u5f0f ( x = \\frac{{-b \\pm \\sqrt{D}}}{{2a}} ) \u5f97\u5230\u6839\u7684\u503c\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u8ba1\u7b97\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u8f93\u5165\u7684\u7cfb\u6570 ( a, b, c ) \u662f\u6709\u6548\u7684\u6570\u5b57\uff0c\u4e14 ( a ) \u4e0d\u80fd\u4e3a\u96f6\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5bfc\u81f4\u65b9\u7a0b\u4e0d\u518d\u662f\u4e8c\u6b21\u65b9\u7a0b\u3002\u540c\u65f6\uff0c\u6ce8\u610f\u5904\u7406\u5224\u522b\u5f0f\u4e3a\u8d1f\u7684\u60c5\u51b5\uff0c\u9632\u6b62\u8ba1\u7b97\u9519\u8bef\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff0c\u6bd4\u5982\u8f93\u5165\u975e\u6570\u5b57\u7c7b\u578b\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u8f85\u52a9\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\uff1f<\/strong><br \/>\u9664\u4e86\u624b\u52a8\u7f16\u5199\u4ee3\u7801\u6c42\u89e3\u5916\uff0cPython\u7684<code>numpy<\/code>\u548c<code>sympy<\/code>\u5e93\u4e5f\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5904\u7406\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u3002<code>numpy.roots()<\/code>\u51fd\u6570\u80fd\u591f\u76f4\u63a5\u6c42\u89e3\u591a\u9879\u5f0f\u7684\u6839\uff0c\u800c<code>sympy<\/code>\u5219\u63d0\u4f9b\u4e86\u7b26\u53f7\u8ba1\u7b97\u7684\u80fd\u529b\uff0c\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u8fdb\u884c\u6570\u5b66\u64cd\u4f5c\u3002\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u52a0\u9ad8\u6548\u5730\u5b8c\u6210\u8ba1\u7b97\uff0c\u5e76\u63d0\u4f9b\u66f4\u52a0\u6e05\u6670\u7684\u7ed3\u679c\u5c55\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8ba1\u7b97\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u3001\u5229\u7528NumPy\u5e93\u3001\u91c7\u7528SymPy\u5e93\u3002 \u5176\u4e2d\uff0c\u5229\u7528\u6570\u5b66\u516c\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":1145925,"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\/1145920"}],"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=1145920"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145920\/revisions"}],"predecessor-version":[{"id":1145926,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145920\/revisions\/1145926"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1145925"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1145920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1145920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1145920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}