{"id":1141404,"date":"2025-01-08T22:30:47","date_gmt":"2025-01-08T14:30:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141404.html"},"modified":"2025-01-08T22:30:51","modified_gmt":"2025-01-08T14:30:51","slug":"python3%e5%a6%82%e4%bd%95%e7%94%a8log%e4%bb%a5e%e4%b8%ba%e5%ba%95%e5%af%b9%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1141404.html","title":{"rendered":"python3\u5982\u4f55\u7528log\u4ee5e\u4e3a\u5e95\u5bf9\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104130\/d791a046-c1e6-4d60-aaf4-efe7e4fc02f4.webp\" alt=\"python3\u5982\u4f55\u7528log\u4ee5e\u4e3a\u5e95\u5bf9\u6570\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python3\u8fdb\u884c\u4ee5e\u4e3a\u5e95\u7684\u5bf9\u6570\u8ba1\u7b97<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python3\u4e2d\uff0c<strong>\u4f7f\u7528math\u6a21\u5757\u7684log\u51fd\u6570\u3001\u4f7f\u7528numpy\u6a21\u5757\u7684log\u51fd\u6570\u3001\u4f7f\u7528SymPy\u6a21\u5757\u8fdb\u884c\u7b26\u53f7\u8ba1\u7b97\u3001\u5229\u7528Scipy\u5e93\u8ba1\u7b97\u4ee5e\u4e3a\u5e95\u7684\u5bf9\u6570<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528math\u6a21\u5757\u7684log\u51fd\u6570\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528math\u6a21\u5757\u7684log\u51fd\u6570<\/h2>\n<\/p>\n<p><p>Python\u7684\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3amath\u7684\u6a21\u5757\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u8bb8\u591a\u6570\u5b66\u51fd\u6570\u3002\u8981\u8ba1\u7b97\u4ee5e\u4e3a\u5e95\u7684\u5bf9\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528math.log()\u51fd\u6570\u3002\u8be5\u51fd\u6570\u7684\u9ed8\u8ba4\u5e95\u6570\u5c31\u662fe\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570<\/strong><\/h2>\n<p>number = 10<\/p>\n<p>result = math.log(number)<\/p>\n<p>print(f&quot;The natural logarithm of {number} is {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86math\u6a21\u5757\uff0c\u5e76\u4f7f\u7528math.log()\u51fd\u6570\u6765\u8ba1\u7b97\u6570\u5b5710\u7684\u81ea\u7136\u5bf9\u6570\u3002\u7ed3\u679c\u663e\u793a\u4e3a2.302585092994046\uff0c\u8fd9\u662f\u6b63\u786e\u7684\u81ea\u7136\u5bf9\u6570\u503c\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528numpy\u6a21\u5757\u7684log\u51fd\u6570<\/h2>\n<\/p>\n<p><p>Numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u5b66\u5e93\uff0c\u7279\u522b\u9002\u5408\u8fdb\u884c\u5927\u91cf\u6570\u503c\u8ba1\u7b97\u3002Numpy\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e2alog\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u4ee5e\u4e3a\u5e95\u7684\u5bf9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570<\/strong><\/h2>\n<p>number = 10<\/p>\n<p>result = np.log(number)<\/p>\n<p>print(f&quot;The natural logarithm of {number} is {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0emath\u6a21\u5757\u7c7b\u4f3c\uff0cnumpy\u7684log\u51fd\u6570\u4e5f\u9ed8\u8ba4\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u3002\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528numpy\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528SymPy\u6a21\u5757\u8fdb\u884c\u7b26\u53f7\u8ba1\u7b97<\/h2>\n<\/p>\n<p><p>SymPy\u662f\u4e00\u4e2a\u7528\u4e8e\u7b26\u53f7\u6570\u5b66\u8ba1\u7b97\u7684Python\u5e93\u3002\u5b83\u53ef\u4ee5\u5904\u7406\u7b26\u53f7\u8868\u8fbe\u5f0f\uff0c\u5e76\u4e14\u53ef\u4ee5\u8fdb\u884c\u7cbe\u786e\u7684\u6570\u5b66\u8fd0\u7b97\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528SymPy\u6765\u8ba1\u7b97\u4ee5e\u4e3a\u5e95\u7684\u5bf9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sympy as sp<\/p>\n<h2><strong>\u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf<\/strong><\/h2>\n<p>x = sp.symbols(&#39;x&#39;)<\/p>\n<h2><strong>\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570<\/strong><\/h2>\n<p>expression = sp.log(x)<\/p>\n<p>result = expression.subs(x, 10)<\/p>\n<p>print(f&quot;The natural logarithm of 10 is {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b26\u53f7\u53d8\u91cfx\uff0c\u5e76\u4f7f\u7528sp.log()\u51fd\u6570\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u3002\u7136\u540e\uff0c\u6211\u4eec\u7528subs()\u65b9\u6cd5\u5c06x\u66ff\u6362\u4e3a\u5177\u4f53\u7684\u6570\u5b5710\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5229\u7528Scipy\u5e93\u8ba1\u7b97\u4ee5e\u4e3a\u5e95\u7684\u5bf9\u6570<\/h2>\n<\/p>\n<p><p>Scipy\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u57fa\u4e8enumpy\u6784\u5efa\uff0c\u5e76\u63d0\u4f9b\u4e86\u66f4\u591a\u9ad8\u7ea7\u7684\u6570\u5b66\u51fd\u6570\u3002Scipy\u4e5f\u63d0\u4f9b\u4e86\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.special import log<\/p>\n<h2><strong>\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570<\/strong><\/h2>\n<p>number = 10<\/p>\n<p>result = log(number)<\/p>\n<p>print(f&quot;The natural logarithm of {number} is {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Scipy\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u3002\u4e0e\u524d\u9762\u7684\u65b9\u6cd5\u7c7b\u4f3c\uff0cScipy\u7684log\u51fd\u6570\u4e5f\u9ed8\u8ba4\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5e94\u7528\u573a\u666f\u4e0e\u4f18\u5316\u5efa\u8bae<\/h2>\n<\/p>\n<p><h3>1\u3001\u6570\u636e\u5206\u6790\u4e0e\u79d1\u5b66\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u8ba1\u7b97\u5bf9\u6570\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5728\u7edf\u8ba1\u5206\u6790\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\uff0c\u4ee5\u4f7f\u6570\u636e\u66f4\u7b26\u5408\u6b63\u6001\u5206\u5e03\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u662f\u975e\u5e38\u65b9\u4fbf\u7684\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u5bf9\u6570\u8ba1\u7b97\uff0c\u5efa\u8bae\u4f7f\u7528numpy\u5e93\uff0c\u56e0\u4e3anumpy\u662f\u4e13\u95e8\u4e3a\u5927\u89c4\u6a21\u6570\u503c\u8ba1\u7b97\u8bbe\u8ba1\u7684\uff0c\u5176\u6027\u80fd\u8fdc\u8fdc\u4f18\u4e8e\u6807\u51c6\u5e93\u4e2d\u7684math\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u7b26\u53f7\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u8fdb\u884c\u7b26\u53f7\u8ba1\u7b97\uff0cSymPy\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u9009\u62e9\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u8ba1\u7b97\u5bf9\u6570\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u5176\u4ed6\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u4f8b\u5982\u5fae\u79ef\u5206\u3001\u65b9\u7a0b\u6c42\u89e3\u7b49\u3002<\/p>\n<\/p>\n<p><h3>4\u3001\u79d1\u5b66\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u8fdb\u884c\u9ad8\u7ea7\u79d1\u5b66\u8ba1\u7b97\u7684\u573a\u666f\uff0cScipy\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u9009\u62e9\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u51fd\u6570\uff0c\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u590d\u6742\u7684\u6570\u5b66\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h2>\u7ed3\u8bba<\/h2>\n<\/p>\n<p><p>\u5728Python3\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba1\u7b97\u4ee5e\u4e3a\u5e95\u7684\u5bf9\u6570\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528math\u6a21\u5757\u7684log\u51fd\u6570\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u6cd5\u8fdb\u884c\u8ba1\u7b97\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u8ba1\u7b97\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\uff0c\u5e76\u786e\u4fdd\u8ba1\u7b97\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python3\u4e2d\u4f7f\u7528log\u51fd\u6570\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7math\u6a21\u5757\u4e2d\u7684log\u51fd\u6570\u8ba1\u7b97\u4ee5e\u4e3a\u5e95\u7684\u81ea\u7136\u5bf9\u6570\u3002\u4f7f\u7528\u65f6\u53ea\u9700\u5bfc\u5165math\u6a21\u5757\uff0c\u7136\u540e\u8c03\u7528math.log()\u51fd\u6570\uff0c\u4f20\u5165\u9700\u8981\u8ba1\u7b97\u5bf9\u6570\u7684\u6570\u503c\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u8ba1\u7b972\u7684\u81ea\u7136\u5bf9\u6570\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">import math\nresult = math.log(2)\nprint(result)\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528NumPy\u5e93\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u652f\u6301\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u3002\u5982\u679c\u9700\u8981\u5bf9\u591a\u4e2a\u6570\u503c\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u7684np.log()\u51fd\u6570\uff0c\u8fd9\u6837\u53ef\u4ee5\u4e00\u6b21\u6027\u5bf9\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8fdb\u884c\u8fd0\u7b97\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\narray = np.array([1, 2, 3, 4])\nresult = np.log(array)\nprint(result)\n<\/code><\/pre>\n<p><strong>\u5728Python3\u4e2d\uff0c\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u65f6\u5982\u4f55\u5904\u7406\u8d1f\u6570\u6216\u96f6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u8d1f\u6570\u6216\u96f6\u7684\u81ea\u7136\u5bf9\u6570\u4f1a\u5f15\u53d1ValueError\u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u8f93\u5165\u503c\u662f\u5426\u5927\u4e8e\u96f6\u3002\u5728\u8ba1\u7b97\u5bf9\u6570\u4e4b\u524d\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u7b80\u5355\u7684\u5224\u65ad\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import math\n\nvalue = -1  # \u793a\u4f8b\u503c\nif value &gt; 0:\n    result = math.log(value)\nelse:\n    result = &quot;\u8f93\u5165\u503c\u5fc5\u987b\u5927\u4e8e\u96f6&quot;\nprint(result)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python3\u8fdb\u884c\u4ee5e\u4e3a\u5e95\u7684\u5bf9\u6570\u8ba1\u7b97 \u5728Python3\u4e2d\uff0c\u4f7f\u7528math\u6a21\u5757\u7684log\u51fd\u6570\u3001\u4f7f\u7528numpy\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1141414,"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\/1141404"}],"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=1141404"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141404\/revisions"}],"predecessor-version":[{"id":1141417,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141404\/revisions\/1141417"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141414"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}