{"id":1178836,"date":"2025-01-15T18:15:45","date_gmt":"2025-01-15T10:15:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1178836.html"},"modified":"2025-01-15T18:15:47","modified_gmt":"2025-01-15T10:15:47","slug":"python%e5%be%aa%e7%8e%af%e5%b0%8f%e6%95%b0%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1178836.html","title":{"rendered":"python\u5faa\u73af\u5c0f\u6570\u5982\u4f55\u8868\u793a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113424\/02e20958-55b5-42f8-ae18-63c6c564f660.webp\" alt=\"python\u5faa\u73af\u5c0f\u6570\u5982\u4f55\u8868\u793a\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8868\u793a\u5faa\u73af\u5c0f\u6570\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c<strong>\u76f4\u63a5\u8868\u793a\u4e3a\u5206\u6570\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u8868\u793a\u3001\u4f7f\u7528\u7279\u5b9a\u7684\u5e93\u6765\u5904\u7406\u5faa\u73af\u5c0f\u6570<\/strong>\u3002\u5176\u4e2d\uff0c\u76f4\u63a5\u8868\u793a\u4e3a\u5206\u6570\u662f\u6700\u4e3a\u5e38\u89c1\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757\u4e2d\u7684<code>Fraction<\/code>\u7c7b\u6765\u8868\u793a\u5206\u6570\uff0c\u4ece\u800c\u95f4\u63a5\u8868\u793a\u5faa\u73af\u5c0f\u6570\u3002<\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u8868\u793a\u4e3a\u5206\u6570\u7684\u65b9\u6cd5\u662f\u901a\u8fc7<code>fractions.Fraction<\/code>\u7c7b\u6765\u5b9e\u73b0\u7684\u3002<code>fractions<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u5206\u6570\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u5c06\u5c0f\u6570\u8868\u793a\u4e3a\u5206\u6570\uff0c\u4ece\u800c\u907f\u514d\u4e86\u5faa\u73af\u5c0f\u6570\u7684\u9ebb\u70e6\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u8868\u793a\u4e3a\u5206\u6570<\/h3>\n<\/p>\n<p><p>Python \u4e2d\u7684 <code>fractions<\/code> \u6a21\u5757\u5141\u8bb8\u4f60\u901a\u8fc7\u5206\u6570\u6765\u8868\u793a\u5faa\u73af\u5c0f\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\uff0c\u800c\u4e14\u53ef\u4ee5\u907f\u514d\u6d6e\u70b9\u6570\u8868\u793a\u4e2d\u7684\u7cbe\u5ea6\u95ee\u9898\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fractions import Fraction<\/p>\n<h2><strong>0.333... can be represented as 1\/3<\/strong><\/h2>\n<p>fraction = Fraction(1, 3)<\/p>\n<p>print(fraction)  # \u8f93\u51fa: 1\/3<\/p>\n<p>print(float(fraction))  # \u8f93\u51fa: 0.3333333333333333<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5c06\u4efb\u4f55\u5faa\u73af\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\u8868\u793a\uff0c\u8fd9\u6837\u4e0d\u4ec5\u65b9\u4fbf\u8ba1\u7b97\uff0c\u800c\u4e14\u4fdd\u6301\u4e86\u9ad8\u7cbe\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u8868\u793a\u5faa\u73af\u5c0f\u6570<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u6765\u8868\u793a\u5faa\u73af\u5c0f\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4e0d\u5982\u76f4\u63a5\u8868\u793a\u4e3a\u5206\u6570\u90a3\u4e48\u65b9\u4fbf\uff0c\u4f46\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u662f\u5fc5\u8981\u7684\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def represent_repeating_decimal(numerator, denominator):<\/p>\n<p>    result = str(numerator \/\/ denominator) + &quot;.&quot;<\/p>\n<p>    numerator %= denominator<\/p>\n<p>    seen = {}<\/p>\n<p>    repeating_part = &quot;&quot;<\/p>\n<p>    while numerator != 0:<\/p>\n<p>        if numerator in seen:<\/p>\n<p>            repeating_part = result[seen[numerator]:]<\/p>\n<p>            result = result[:seen[numerator]] + &quot;(&quot; + repeating_part + &quot;)&quot;<\/p>\n<p>            break<\/p>\n<p>        seen[numerator] = len(result)<\/p>\n<p>        numerator *= 10<\/p>\n<p>        result += str(numerator \/\/ denominator)<\/p>\n<p>        numerator %= denominator<\/p>\n<p>    return result<\/p>\n<p>print(represent_repeating_decimal(1, 3))  # \u8f93\u51fa: &quot;0.(3)&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u8bb0\u5f55\u6bcf\u4e2a\u4f59\u6570\u7684\u51fa\u73b0\u4f4d\u7f6e\u6765\u68c0\u6d4b\u5faa\u73af\u90e8\u5206\uff0c\u5e76\u5728\u7ed3\u679c\u4e2d\u63d2\u5165\u62ec\u53f7\u8868\u793a\u5faa\u73af\u8282\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7279\u5b9a\u7684\u5e93\u6765\u5904\u7406\u5faa\u73af\u5c0f\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8fd8\u6709\u4e00\u4e9b\u4e13\u95e8\u7684\u5e93\u53ef\u4ee5\u5904\u7406\u5faa\u73af\u5c0f\u6570\uff0c\u4f8b\u5982<code>sympy<\/code>\u5e93\u3002<code>sympy<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u7b26\u53f7\u6570\u5b66\u8ba1\u7b97\u7684Python\u5e93\uff0c\u80fd\u591f\u5904\u7406\u5305\u62ec\u5faa\u73af\u5c0f\u6570\u5728\u5185\u7684\u591a\u79cd\u6570\u5b66\u8868\u793a\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>sympy<\/code>\u5e93\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import Rational<\/p>\n<h2><strong>\u4f7f\u7528 Rational \u7c7b\u6765\u8868\u793a\u5206\u6570<\/strong><\/h2>\n<p>rational = Rational(1, 3)<\/p>\n<p>print(rational)  # \u8f93\u51fa: 1\/3<\/p>\n<p>print(float(rational))  # \u8f93\u51fa: 0.3333333333333333<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>sympy<\/code>\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u8868\u793a\u5206\u6570\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u7b26\u53f7\u8ba1\u7b97\u548c\u5176\u4ed6\u590d\u6742\u7684\u6570\u5b66\u64cd\u4f5c\uff0c\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u624b\u52a8\u5b9e\u73b0\u5faa\u73af\u5c0f\u6570\u68c0\u6d4b\u548c\u8868\u793a<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u9ad8\u7ea7\u5e94\u7528\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u624b\u52a8\u5b9e\u73b0\u5faa\u73af\u5c0f\u6570\u7684\u68c0\u6d4b\u548c\u8868\u793a\u7b97\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u624b\u52a8\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_repeating_decimal(numerator, denominator):<\/p>\n<p>    integer_part = numerator \/\/ denominator<\/p>\n<p>    result = str(integer_part) + &quot;.&quot;<\/p>\n<p>    rem<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nder = numerator % denominator<\/p>\n<p>    remainders = {}<\/p>\n<p>    decimal_part = &quot;&quot;<\/p>\n<p>    index = 0<\/p>\n<p>    while remainder != 0:<\/p>\n<p>        if remainder in remainders:<\/p>\n<p>            start = remainders[remainder]<\/p>\n<p>            non_repeating = decimal_part[:start]<\/p>\n<p>            repeating = decimal_part[start:]<\/p>\n<p>            result += non_repeating + &quot;(&quot; + repeating + &quot;)&quot;<\/p>\n<p>            return result<\/p>\n<p>        remainders[remainder] = index<\/p>\n<p>        remainder *= 10<\/p>\n<p>        digit = remainder \/\/ denominator<\/p>\n<p>        decimal_part += str(digit)<\/p>\n<p>        remainder %= denominator<\/p>\n<p>        index += 1<\/p>\n<p>    result += decimal_part<\/p>\n<p>    return result<\/p>\n<p>print(find_repeating_decimal(1, 3))  # \u8f93\u51fa: &quot;0.(3)&quot;<\/p>\n<p>print(find_repeating_decimal(22, 7))  # \u8f93\u51fa: &quot;3.(142857)&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u51fd\u6570\u901a\u8fc7\u8bb0\u5f55\u4f59\u6570\u7684\u51fa\u73b0\u4f4d\u7f6e\u6765\u68c0\u6d4b\u5faa\u73af\u8282\uff0c\u5e76\u5728\u7ed3\u679c\u4e2d\u63d2\u5165\u62ec\u53f7\u4ee5\u8868\u793a\u5faa\u73af\u5c0f\u6570\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u65e0\u9650\u7cbe\u5ea6\u5e93<\/h3>\n<\/p>\n<p><p>Python \u4e2d\u7684 <code>decimal<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u65e0\u9650\u7cbe\u5ea6\u5c0f\u6570\u7684\u652f\u6301\uff0c\u8fd9\u5728\u5904\u7406\u5faa\u73af\u5c0f\u6570\u65f6\u975e\u5e38\u6709\u7528\u3002\u867d\u7136\u5b83\u4e0d\u80fd\u76f4\u63a5\u8868\u793a\u5faa\u73af\u5c0f\u6570\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7cbe\u5ea6\u6765\u8fd1\u4f3c\u8868\u793a\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal, getcontext<\/p>\n<h2><strong>\u8bbe\u7f6e\u7cbe\u5ea6<\/strong><\/h2>\n<p>getcontext().prec = 50<\/p>\n<h2><strong>\u8868\u793a\u5faa\u73af\u5c0f\u6570<\/strong><\/h2>\n<p>decimal_value = Decimal(1) \/ Decimal(3)<\/p>\n<p>print(decimal_value)  # \u8f93\u51fa: 0.33333333333333333333333333333333333333333333333333<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u83b7\u5f97\u5c3d\u53ef\u80fd\u9ad8\u7684\u7cbe\u5ea6\uff0c\u4ece\u800c\u8fd1\u4f3c\u8868\u793a\u5faa\u73af\u5c0f\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8868\u793a\u5faa\u73af\u5c0f\u6570\u6709\u591a\u79cd\u65b9\u6cd5\uff0c<strong>\u76f4\u63a5\u8868\u793a\u4e3a\u5206\u6570\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u8868\u793a\u3001\u4f7f\u7528\u7279\u5b9a\u7684\u5e93\u6765\u5904\u7406\u5faa\u73af\u5c0f\u6570<\/strong>\u3002\u5176\u4e2d\uff0c\u76f4\u63a5\u8868\u793a\u4e3a\u5206\u6570\u662f\u6700\u4e3a\u5e38\u89c1\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5faa\u73af\u5c0f\u6570\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5206\u6570\u8868\u793a\uff0c\u8fd8\u662f\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5904\u7406\u548c\u5e93\u51fd\u6570\u5e94\u7528\uff0c\u90fd\u80fd\u5e2e\u52a9\u4f60\u5728Python\u4e2d\u6709\u6548\u5730\u8868\u793a\u548c\u5904\u7406\u5faa\u73af\u5c0f\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8868\u793a\u5faa\u73af\u5c0f\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5faa\u73af\u5c0f\u6570\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u8868\u793a\u6cd5\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5faa\u73af\u90e8\u5206\u7528\u62ec\u53f7\u62ec\u8d77\u6765\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u5982\u201c0.1666&#8230;\u201d\u53ef\u4ee5\u8868\u793a\u4e3a\u201c0.1(6)\u201d\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u76f4\u89c2\u5730\u4e86\u89e3\u5c0f\u6570\u7684\u5faa\u73af\u6027\u8d28\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u6ca1\u6709\u4e13\u95e8\u7684\u5e93\u6765\u5904\u7406\u5faa\u73af\u5c0f\u6570\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u6709\u4e00\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u5faa\u73af\u5c0f\u6570\uff0c\u4f8b\u5982<code>fractions<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u5206\u6570\u5f62\u5f0f\u7684\u5c0f\u6570\u3002\u867d\u7136\u5b83\u4e0d\u76f4\u63a5\u5904\u7406\u5faa\u73af\u5c0f\u6570\uff0c\u4f46\u53ef\u4ee5\u5c06\u5faa\u73af\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\uff0c\u4ece\u800c\u7b80\u5316\u8ba1\u7b97\u548c\u8868\u793a\u7684\u8fc7\u7a0b\u3002\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5faa\u73af\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\uff1f<\/strong><br \/>\u5c06\u5faa\u73af\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\u7684\u8fc7\u7a0b\u6d89\u53ca\u4e00\u4e9b\u6570\u5b66\u6280\u5de7\u3002\u6bd4\u5982\uff0c\u5bf9\u4e8e\u5c0f\u65700.666&#8230;\uff0c\u53ef\u4ee5\u8bbex=0.666&#8230;\uff0c\u7136\u540e\u901a\u8fc7\u7b80\u5355\u7684\u4ee3\u6570\u8fd0\u7b97\u5f97\u51fax=2\/3\u3002Python\u4e2d\u53ef\u4ee5\u5229\u7528<code>fractions.Fraction<\/code>\u7c7b\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u8f6c\u6362\uff0c\u8f93\u5165\u5faa\u73af\u5c0f\u6570\u7684\u5206\u6570\u5f62\u5f0f\uff0c\u7a0b\u5e8f\u5c06\u81ea\u52a8\u7b80\u5316\u5e76\u8f93\u51fa\u7ed3\u679c\u3002\u8fd9\u79cd\u65b9\u6cd5\u80fd\u6709\u6548\u907f\u514d\u624b\u52a8\u8ba1\u7b97\u7684\u590d\u6742\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8868\u793a\u5faa\u73af\u5c0f\u6570\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u76f4\u63a5\u8868\u793a\u4e3a\u5206\u6570\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u8868\u793a\u3001\u4f7f\u7528\u7279\u5b9a\u7684\u5e93\u6765\u5904\u7406\u5faa\u73af\u5c0f\u6570\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1178843,"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\/1178836"}],"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=1178836"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178836\/revisions"}],"predecessor-version":[{"id":1178844,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178836\/revisions\/1178844"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1178843"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1178836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1178836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1178836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}