{"id":1069719,"date":"2025-01-08T10:51:34","date_gmt":"2025-01-08T02:51:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1069719.html"},"modified":"2025-01-08T10:51:36","modified_gmt":"2025-01-08T02:51:36","slug":"python%e5%a6%82%e4%bd%95%e5%88%b6%e4%bd%9c%e7%ab%8b%e6%96%b9%e6%a0%b9%e7%9a%84%e5%87%bd%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1069719.html","title":{"rendered":"python\u5982\u4f55\u5236\u4f5c\u7acb\u65b9\u6839\u7684\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100650\/95e81993-a639-47d3-ae0a-910bc7f9ba1e.webp\" alt=\"python\u5982\u4f55\u5236\u4f5c\u7acb\u65b9\u6839\u7684\u51fd\u6570\" \/><\/p>\n<p><p> <strong>Python\u5236\u4f5c\u7acb\u65b9\u6839\u7684\u51fd\u6570\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u5b9e\u73b0\u9ad8\u6548\u8ba1\u7b97\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u5185\u7f6e\u51fd\u6570<\/strong> \u662f\u6700\u7b80\u5355\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u5185\u7f6e\u7684\u5e42\u8fd0\u7b97\u7b26\u5b9e\u73b0\u7acb\u65b9\u6839\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8ba1\u7b97\u7acb\u65b9\u6839\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6570\u5b66\u5e93\u4ee5\u53ca\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u7acb\u65b9\u6839\u7684\u8ba1\u7b97\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u7528\u7684\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5185\u7f6e\u51fd\u6570\u5b9e\u73b0\u7acb\u65b9\u6839<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26<code><\/code>\u6765\u8ba1\u7b97\u7acb\u65b9\u6839\u3002\u7acb\u65b9\u6839\u5b9e\u9645\u4e0a\u5c31\u662f\u5c06\u6570\u503c\u53d6<code>1\/3<\/code>\u6b21\u65b9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def cube_root(x):<\/p>\n<p>    return x  (1\/3)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(cube_root(27))  # \u8f93\u51fa\uff1a3.0<\/p>\n<p>print(cube_root(-8))  # \u8f93\u51fa\uff1a-2.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u3002\u540c\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u80fd\u5904\u7406\u8d1f\u6570\u7684\u7acb\u65b9\u6839\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528math\u5e93\u5b9e\u73b0\u7acb\u65b9\u6839<\/h3>\n<\/p>\n<p><p>Python\u7684<code>math<\/code>\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u51fd\u6570\uff0c\u4f46\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b\u7acb\u65b9\u6839\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>math.pow<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u7acb\u65b9\u6839\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def cube_root(x):<\/p>\n<p>    return math.pow(x, 1\/3)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(cube_root(27))  # \u8f93\u51fa\uff1a3.0<\/p>\n<p>print(cube_root(-8))  # \u8f93\u51fa\uff1a-2.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>math.pow<\/code>\u51fd\u6570\u7684\u6548\u679c\u4e0e<code><\/code>\u8fd0\u7b97\u7b26\u7c7b\u4f3c\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c<code>math.pow<\/code>\u53ef\u80fd\u5177\u6709\u66f4\u9ad8\u7684\u8ba1\u7b97\u7cbe\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u81ea\u5b9a\u4e49\u725b\u987f\u6cd5\u5b9e\u73b0\u7acb\u65b9\u6839<\/h3>\n<\/p>\n<p><p>\u725b\u987f\u6cd5\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u503c\u8ba1\u7b97\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u6c42\u89e3\u65b9\u7a0b\u7684\u6839\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u725b\u987f\u6cd5\u6765\u5b9e\u73b0\u7acb\u65b9\u6839\u7684\u8ba1\u7b97\u3002\u725b\u987f\u6cd5\u7684\u57fa\u672c\u601d\u60f3\u662f\u4ece\u4e00\u4e2a\u521d\u59cb\u731c\u6d4b\u503c\u51fa\u53d1\uff0c\u4e0d\u65ad\u8fed\u4ee3\uff0c\u9010\u6e10\u903c\u8fd1\u5b9e\u9645\u7684\u6839\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def cube_root_newton(x, tolerance=1e-7, max_iterations=1000):<\/p>\n<p>    if x == 0:<\/p>\n<p>        return 0<\/p>\n<p>    guess = x<\/p>\n<p>    for _ in range(max_iterations):<\/p>\n<p>        guess = (2 * guess + x \/ (guess  2)) \/ 3<\/p>\n<p>        if abs(guess  3 - x) &lt; tolerance:<\/p>\n<p>            return guess<\/p>\n<p>    return guess<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(cube_root_newton(27))  # \u8f93\u51fa\uff1a3.0<\/p>\n<p>print(cube_root_newton(-8))  # \u8f93\u51fa\uff1a-2.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4ece\u521d\u59cb\u731c\u6d4b\u503c<code>x<\/code>\u5f00\u59cb\uff0c\u4e0d\u65ad\u8fed\u4ee3\u66f4\u65b0\u731c\u6d4b\u503c\uff0c\u76f4\u5230\u8bef\u5dee\u5c0f\u4e8e\u6307\u5b9a\u7684\u5bb9\u5dee<code>tolerance<\/code>\u6216\u8fbe\u5230\u6700\u5927\u8fed\u4ee3\u6b21\u6570<code>max_iterations<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u590d\u6570\u5904\u7406\u8d1f\u6570\u7acb\u65b9\u6839<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u590d\u6570\u6765\u5904\u7406\u8d1f\u6570\u7684\u7acb\u65b9\u6839\u3002\u590d\u6570\u7684\u7acb\u65b9\u6839\u53ef\u4ee5\u901a\u8fc7<code>cmath<\/code>\u5e93\u6765\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cmath<\/p>\n<p>def cube_root_complex(x):<\/p>\n<p>    return x  (1\/3)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(cube_root_complex(complex(-8)))  # \u8f93\u51fa\uff1a1.0000000000000002+1.7320508075688774j<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba1\u7b97\u51fa\u8d1f\u6570\u7684\u590d\u6570\u7acb\u65b9\u6839\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528SymPy\u5e93\u5b9e\u73b0\u7acb\u65b9\u6839<\/h3>\n<\/p>\n<p><p>SymPy\u662fPython\u7684\u4e00\u4e2a\u7b26\u53f7\u6570\u5b66\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u6570\u5b66\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528SymPy\u6765\u5b9e\u73b0\u7acb\u65b9\u6839\u7684\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sympy as sp<\/p>\n<p>def cube_root_sympy(x):<\/p>\n<p>    return sp.cbrt(x)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(cube_root_sympy(27))  # \u8f93\u51fa\uff1a3<\/p>\n<p>print(cube_root_sympy(-8))  # \u8f93\u51fa\uff1a-2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>SymPy\u5e93\u63d0\u4f9b\u7684<code>sp.cbrt<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u7acb\u65b9\u6839\uff0c\u9002\u5408\u7b26\u53f7\u8ba1\u7b97\u548c\u9ad8\u7cbe\u5ea6\u9700\u6c42\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528NumPy\u5e93\u5b9e\u73b0\u7acb\u65b9\u6839<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u7684\u4e00\u4e2a\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u6570\u5b66\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u6765\u5b9e\u73b0\u7acb\u65b9\u6839\u7684\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def cube_root_numpy(x):<\/p>\n<p>    return np.cbrt(x)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(cube_root_numpy(27))  # \u8f93\u51fa\uff1a3.0<\/p>\n<p>print(cube_root_numpy(-8))  # \u8f93\u51fa\uff1a-2.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NumPy\u5e93\u63d0\u4f9b\u7684<code>np.cbrt<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u7acb\u65b9\u6839\uff0c\u9002\u5408\u5927\u89c4\u6a21\u6570\u503c\u8ba1\u7b97\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u6765\u6d4b\u91cf\u6bcf\u79cd\u65b9\u6cd5\u7684\u6267\u884c\u65f6\u95f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<h2><strong>\u5b9a\u4e49\u6d4b\u8bd5\u51fd\u6570<\/strong><\/h2>\n<p>def test_func(func, x):<\/p>\n<p>    return func(x)<\/p>\n<h2><strong>\u5b9a\u4e49\u6d4b\u8bd5\u503c<\/strong><\/h2>\n<p>x = 27<\/p>\n<h2><strong>\u6d4b\u8bd5\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd<\/strong><\/h2>\n<p>print(timeit.timeit(lambda: test_func(cube_root, x), number=1000000))        # \u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/p>\n<p>print(timeit.timeit(lambda: test_func(cube_root_newton, x), number=1000000)) # \u4f7f\u7528\u725b\u987f\u6cd5<\/p>\n<p>print(timeit.timeit(lambda: test_func(cube_root_numpy, x), number=1000000))  # \u4f7f\u7528NumPy\u5e93<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6d4b\u91cf\u4e0d\u540c\u65b9\u6cd5\u7684\u6267\u884c\u65f6\u95f4\uff0c\u5e76\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u4ecb\u7ecd\u4e86\u591a\u79cd\u8ba1\u7b97\u7acb\u65b9\u6839\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001math\u5e93\u3001\u725b\u987f\u6cd5\u3001\u590d\u6570\u5904\u7406\u3001SymPy\u5e93\u548cNumPy\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u7acb\u65b9\u6839\u7684\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u8ba1\u7b97\u7acb\u65b9\u6839\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>**<\/code>\u8fd0\u7b97\u7b26\u6765\u8ba1\u7b97\u7acb\u65b9\u6839\u3002\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u521b\u5efa\u4e00\u4e2a\u63a5\u53d7\u6570\u5b57\u4f5c\u4e3a\u53c2\u6570\u7684\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u8be5\u6570\u5b57\u7684\u7acb\u65b9\u6839\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def cube_root(x):\n    return x ** (1\/3)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6570\u5b57<code>x<\/code>\u5e76\u8fd4\u56de\u5176\u7acb\u65b9\u6839\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u4e2d\u7684\u5e93\u6765\u8ba1\u7b97\u7acb\u65b9\u6839\u6709\u54ea\u4e9b\u9009\u62e9\uff1f<\/strong><br \/>Python\u7684<code>math<\/code>\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u51fd\u6570\u3002\u867d\u7136\u6ca1\u6709\u76f4\u63a5\u7684\u7acb\u65b9\u6839\u51fd\u6570\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528<code>math.pow<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u7acb\u65b9\u6839\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import math\n\ndef cube_root(x):\n    return math.pow(x, 1\/3)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u53ef\u4ee5\u5f97\u5230\u7acb\u65b9\u6839\uff0c\u4e14\u5728\u9700\u8981\u590d\u6742\u6570\u5b66\u8fd0\u7b97\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8d1f\u6570\u7684\u7acb\u65b9\u6839\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8d1f\u6570\u7684\u7acb\u65b9\u6839\u53ef\u4ee5\u901a\u8fc7\u76f8\u540c\u7684\u51fd\u6570\u8ba1\u7b97\u3002\u5728\u6570\u5b66\u4e0a\uff0c\u8d1f\u6570\u7684\u7acb\u65b9\u6839\u662f\u4e00\u4e2a\u8d1f\u6570\u3002\u4f8b\u5982\uff0c<code>-8<\/code>\u7684\u7acb\u65b9\u6839\u662f<code>-2<\/code>\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">print(cube_root(-8))  # \u8f93\u51fa -2.0\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u80fd\u591f\u5904\u7406\u6b63\u6570\u3001\u8d1f\u6570\u548c\u96f6\uff0c\u786e\u4fdd\u5176\u901a\u7528\u6027\u548c\u5b9e\u7528\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5236\u4f5c\u7acb\u65b9\u6839\u7684\u51fd\u6570\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u5b9e\u73b0\u9ad8\u6548\u8ba1\u7b97\u3002 \u5176\u4e2d\uff0c\u5185\u7f6e\u51fd\u6570 \u662f\u6700\u7b80\u5355\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u5185\u7f6e\u7684\u5e42\u8fd0\u7b97\u7b26 [&hellip;]","protected":false},"author":3,"featured_media":1069727,"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\/1069719"}],"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=1069719"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069719\/revisions"}],"predecessor-version":[{"id":1069730,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069719\/revisions\/1069730"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1069727"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1069719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1069719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1069719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}