{"id":1011369,"date":"2024-12-27T11:29:21","date_gmt":"2024-12-27T03:29:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1011369.html"},"modified":"2024-12-27T11:29:23","modified_gmt":"2024-12-27T03:29:23","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e8%a1%a8%e8%be%be%e5%88%86%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1011369.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528python\u8868\u8fbe\u5206\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085528\/a1321151-acf4-4228-8ac4-cee969f2c54b.webp\" alt=\"\u5982\u4f55\u4f7f\u7528python\u8868\u8fbe\u5206\u5f0f\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8868\u8fbe\u5206\u5f0f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757\u3001<code>sympy<\/code>\u5e93\u4e2d\u7684Rational\u7c7b\u3001\u5c06\u5206\u5f0f\u8f6c\u5316\u4e3a\u5c0f\u6570\u6216\u6d6e\u70b9\u6570\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<code>fractions<\/code>\u6a21\u5757\u548c<code>sympy<\/code>\u5e93\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7cbe\u786e\u7684\u65b9\u5f0f\u6765\u8868\u8fbe\u548c\u64cd\u4f5c\u5206\u5f0f\uff0c\u800c\u5c06\u5206\u5f0f\u8f6c\u5316\u4e3a\u5c0f\u6570\u6216\u6d6e\u70b9\u6570\u9002\u5408\u4e8e\u9700\u8981\u8fd1\u4f3c\u8ba1\u7b97\u7684\u573a\u5408\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001FRACTIONS\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>fractions<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u6709\u7406\u6570\u7684\u6a21\u5757\u3002\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u5206\u6570\u5bf9\u8c61\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u5404\u79cd\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u5206\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>fractions.Fraction<\/code>\u7c7b\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u5206\u6570\u5bf9\u8c61\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6574\u6570\u3001\u5b57\u7b26\u4e32\u751a\u81f3\u6d6e\u70b9\u6570\u6765\u521b\u5efa\u5206\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fractions import Fraction<\/p>\n<h2><strong>\u4f7f\u7528\u6574\u6570\u521b\u5efa\u5206\u6570<\/strong><\/h2>\n<p>f1 = Fraction(1, 2)  # \u4ee3\u88681\/2<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u521b\u5efa\u5206\u6570<\/strong><\/h2>\n<p>f2 = Fraction(&#39;3\/4&#39;)  # \u4ee3\u88683\/4<\/p>\n<h2><strong>\u4f7f\u7528\u6d6e\u70b9\u6570\u521b\u5efa\u5206\u6570\uff08\u6ce8\u610f\u6d6e\u70b9\u6570\u7684\u4e0d\u7cbe\u786e\u6027\uff09<\/strong><\/h2>\n<p>f3 = Fraction(0.75)  # \u53ef\u80fd\u4e0d\u7cbe\u786e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5206\u6570\u8fd0\u7b97<\/strong><\/p>\n<\/p>\n<p><p>\u5206\u6570\u5bf9\u8c61\u53ef\u4ee5\u8fdb\u884c\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u7b49\u8fd0\u7b97\uff0c\u5e76\u81ea\u52a8\u5316\u7b80\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = f1 + f2  # 1\/2 + 3\/4<\/p>\n<p>print(result)  # \u8f93\u51fa5\/4<\/p>\n<p>result = f1 * f2  # 1\/2 * 3\/4<\/p>\n<p>print(result)  # \u8f93\u51fa3\/8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5206\u6570\u7684\u5176\u4ed6\u64cd\u4f5c<\/strong><\/p>\n<\/p>\n<p><p><code>Fraction<\/code>\u5bf9\u8c61\u8fd8\u652f\u6301\u5176\u4ed6\u4e00\u4e9b\u64cd\u4f5c\uff0c\u6bd4\u5982\u53d6\u5012\u6570\u3001\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">reciprocal = 1 \/ f1  # \u53d6\u5012\u6570<\/p>\n<p>print(reciprocal)  # \u8f93\u51fa2<\/p>\n<p>float_value = float(f2)  # \u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/p>\n<p>print(float_value)  # \u8f93\u51fa0.75<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001SYMPY\u5e93<\/p>\n<\/p>\n<p><p><code>Sympy<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u7528\u4e8e\u7b26\u53f7\u6570\u5b66\u8ba1\u7b97\u3002\u5b83\u63d0\u4f9b\u4e86<code>Rational<\/code>\u7c7b\u6765\u8868\u793a\u5206\u6570\uff0c\u5e76\u652f\u6301\u4ee3\u6570\u8fd0\u7b97\u3001\u5316\u7b80\u7b49\u590d\u6742\u7684\u6570\u5b66\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaRational\u5bf9\u8c61<\/strong><\/p>\n<\/p>\n<p><p><code>Rational<\/code>\u7c7b\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u5206\u6570\uff0c\u7c7b\u4f3c\u4e8e<code>Fraction<\/code>\u7c7b\uff0c\u4f46\u5b83\u662f\u7b26\u53f7\u5316\u7684\uff0c\u53ef\u4ee5\u5728\u4ee3\u6570\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import Rational<\/p>\n<p>r1 = Rational(1, 2)<\/p>\n<p>r2 = Rational(3, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7b26\u53f7\u8fd0\u7b97<\/strong><\/p>\n<\/p>\n<p><p><code>sympy<\/code>\u5141\u8bb8\u5bf9\u7b26\u53f7\u8868\u8fbe\u5f0f\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f8b\u5982\u5c55\u5f00\u3001\u5316\u7b80\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import simplify<\/p>\n<p>expr = r1 + r2<\/p>\n<p>simplified_expr = simplify(expr)<\/p>\n<p>print(simplified_expr)  # \u8f93\u51fa5\/4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5176\u4ed6\u529f\u80fd<\/strong><\/p>\n<\/p>\n<p><p><code>sympy<\/code>\u8fd8\u652f\u6301\u6c42\u89e3\u65b9\u7a0b\u3001\u6c42\u5bfc\u3001\u79ef\u5206\u7b49\u8bb8\u591a\u6570\u5b66\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u6c42\u89e3\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, Eq, solve<\/p>\n<p>x = symbols(&#39;x&#39;)<\/p>\n<p>equation = Eq(r1*x, r2)<\/p>\n<p>solution = solve(equation, x)<\/p>\n<p>print(solution)  # \u8f93\u51fa[3\/2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u8f6c\u6362\u4e3a\u5c0f\u6570\u6216\u6d6e\u70b9\u6570<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u5206\u5f0f\u8f6c\u6362\u4e3a\u5c0f\u6570\u6216\u6d6e\u70b9\u6570\u3002\u8fd9\u5728\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u65f6\u5c24\u5176\u5e38\u89c1\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u635f\u5931\u7cbe\u5ea6\uff0c\u4f46\u5728\u67d0\u4e9b\u5e94\u7528\u4e2d\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u76f4\u63a5\u8f6c\u6362<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570<code>float()<\/code>\u53ef\u4ee5\u5c06\u5206\u6570\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">f1 = Fraction(1, 3)<\/p>\n<p>float_value = float(f1)<\/p>\n<p>print(float_value)  # \u8f93\u51fa0.3333333333333333<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56db\u820d\u4e94\u5165<\/strong><\/p>\n<\/p>\n<p><p>\u82e5\u9700\u8981\u63a7\u5236\u5c0f\u6570\u4f4d\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>round()<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rounded_value = round(float_value, 2)<\/p>\n<p>print(rounded_value)  # \u8f93\u51fa0.33<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5e94\u7528\u5b9e\u4f8b<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f7f\u7528Python\u8868\u8fbe\u5206\u5f0f\u7684\u9700\u6c42\u53ef\u80fd\u51fa\u73b0\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u91d1\u878d\u5206\u6790\u3001\u5de5\u7a0b\u8ba1\u7b97\u7b49\u591a\u4e2a\u9886\u57df\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u4f53\u7684\u5e94\u7528\u5b9e\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u79d1\u5b66\u8ba1\u7b97<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u5206\u6570\u5f62\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d\u6d6e\u70b9\u6570\u7684\u4e0d\u7cbe\u786e\u6027\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u67d0\u4e9b\u7269\u7406\u516c\u5f0f\u65f6\uff0c\u4f7f\u7528\u5206\u6570\u5f62\u5f0f\u53ef\u4ee5\u4fdd\u8bc1\u7ed3\u679c\u7684\u7cbe\u786e\u5ea6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u91d1\u878d\u5206\u6790<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u91d1\u878d\u5206\u6790\u4e2d\uff0c\u5229\u7387\u3001\u7a0e\u7387\u7b49\u901a\u5e38\u4ee5\u5206\u6570\u5f62\u5f0f\u8868\u793a\u3002\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u7cbe\u786e\u8ba1\u7b97\u5404\u7c7b\u91d1\u878d\u6307\u6807\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5de5\u7a0b\u8ba1\u7b97<\/strong><\/p>\n<\/p>\n<p><p>\u5de5\u7a0b\u8ba1\u7b97\u4e2d\uff0c\u67d0\u4e9b\u53c2\u6570\u53ef\u80fd\u9700\u8981\u4ee5\u7cbe\u786e\u7684\u5206\u6570\u8868\u793a\uff0c\u907f\u514d\u7d2f\u79ef\u8bef\u5dee\u5bf9\u6700\u7ec8\u7ed3\u679c\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u5b9e\u4f8b\u7684\u4ecb\u7ecd\uff0c\u53ef\u4ee5\u770b\u51faPython\u5728\u5904\u7406\u5206\u5f0f\u65b9\u9762\u63d0\u4f9b\u4e86\u4e30\u5bcc\u800c\u7075\u6d3b\u7684\u5de5\u5177\u3002\u65e0\u8bba\u662f\u9700\u8981\u7b26\u53f7\u5316\u8ba1\u7b97\u7684<code>sympy<\/code>\uff0c\u8fd8\u662f\u6ce8\u91cd\u7cbe\u5ea6\u7684<code>fractions<\/code>\u6a21\u5757\uff0cPython\u90fd\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u5728Python\u4e2d\u66f4\u597d\u5730\u8868\u8fbe\u548c\u64cd\u4f5c\u5206\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u5206\u5f0f\u7684\u8868\u793a\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u5206\u5f0f\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7<code>Fraction<\/code>\u7c7b\u6765\u5b9a\u4e49\u5206\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>from fractions import Fraction<\/code>\u5bfc\u5165\u6a21\u5757\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Fraction(1, 2)<\/code>\u6765\u8868\u793a1\/2\u7684\u5206\u5f0f\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u5206\u5f0f\u7684\u7b80\u5316\u548c\u6b63\u786e\u7684\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<p><strong>Python\u4e2d\u5206\u5f0f\u7684\u8fd0\u7b97\u5982\u4f55\u5b9e\u73b0\uff1f<\/strong><br \/>\u901a\u8fc7<code>fractions.Fraction<\/code>\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u8fdb\u884c\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u7b49\u8fd0\u7b97\u3002\u6bd4\u5982\uff0c\u82e5\u6709\u4e24\u4e2a\u5206\u5f0f<code>a = Fraction(1, 3)<\/code>\u548c<code>b = Fraction(1, 6)<\/code>\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>a + b<\/code>\u6765\u8ba1\u7b97\u5b83\u4eec\u7684\u548c\u3002\u8fd9\u4e9b\u8fd0\u7b97\u4f1a\u81ea\u52a8\u5904\u7406\u5206\u6bcd\u7684\u7ea6\u5206\uff0c\u786e\u4fdd\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8868\u8fbe\u5206\u5f0f\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u907f\u514d\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u5904\u7406\u5206\u5f0f\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u5c06\u6574\u6570\u4e0e\u5206\u5f0f\u76f4\u63a5\u76f8\u52a0\uff0c\u5bfc\u81f4\u7c7b\u578b\u4e0d\u5339\u914d\u3002\u5728\u8fdb\u884c\u8fd0\u7b97\u524d\uff0c\u6700\u597d\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5206\u5f0f\uff0c\u4f8b\u5982<code>Fraction(1)<\/code>\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4e0d\u8981\u624b\u52a8\u7b80\u5316\u5206\u5f0f\uff0c<code>fractions<\/code>\u6a21\u5757\u4f1a\u81ea\u52a8\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\uff0c\u907f\u514d\u6f5c\u5728\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8868\u8fbe\u5206\u5f0f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528fractions\u6a21\u5757\u3001sympy\u5e93\u4e2d\u7684Rational\u7c7b\u3001\u5c06\u5206\u5f0f\u8f6c\u5316\u4e3a\u5c0f [&hellip;]","protected":false},"author":3,"featured_media":1011379,"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\/1011369"}],"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=1011369"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011369\/revisions"}],"predecessor-version":[{"id":1011383,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011369\/revisions\/1011383"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1011379"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1011369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1011369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1011369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}