{"id":1073258,"date":"2025-01-08T11:23:50","date_gmt":"2025-01-08T03:23:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073258.html"},"modified":"2025-01-08T11:23:52","modified_gmt":"2025-01-08T03:23:52","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%bb%9f%e8%ae%a1%e6%95%b0%e7%9a%84%e4%b8%aa%e6%95%b0%e5%ad%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1073258.html","title":{"rendered":"\u5982\u4f55\u7528python\u7edf\u8ba1\u6570\u7684\u4e2a\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103239\/818eff46-9ad1-4c52-9c02-a20c8c91bd8b.webp\" alt=\"\u5982\u4f55\u7528python\u7edf\u8ba1\u6570\u7684\u4e2a\u6570\u5b57\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u7edf\u8ba1\u6570\u7684\u4e2a\u6570\u5b57<\/strong><\/p>\n<\/p>\n<p><p><strong>\u7528Python\u7edf\u8ba1\u4e00\u4e2a\u6570\u7684\u5404\u4e2a\u6570\u5b57\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6570\u5b66\u65b9\u6cd5\u3001\u5faa\u73af\u65b9\u6cd5\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5c06\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u6570\u5b66\u8fd0\u7b97\u7b26\u3001\u9012\u5f52\u65b9\u6cd5\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u5c06\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u904d\u5386\uff0c\u7edf\u8ba1\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u7edf\u8ba1\u6570\u7684\u4e2a\u6570\u5b57<\/p>\n<\/p>\n<p><p>\u5c06\u4e00\u4e2a\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u7edf\u8ba1\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u884c\uff0c\u9002\u7528\u4e8e\u7edd\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_digits(number):<\/p>\n<p>    # \u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\n<p>    num_str = str(number)<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u6765\u5b58\u50a8\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570<\/p>\n<p>    digit_count = {}<\/p>\n<p>    # \u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26<\/p>\n<p>    for digit in num_str:<\/p>\n<p>        if digit in digit_count:<\/p>\n<p>            digit_count[digit] += 1<\/p>\n<p>        else:<\/p>\n<p>            digit_count[digit] = 1<\/p>\n<p>    return digit_count<\/p>\n<h2><strong>\u6d4b\u8bd5\u4f8b\u5b50<\/strong><\/h2>\n<p>number = 1234567890123456789<\/p>\n<p>print(count_digits(number))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u6765\u5b58\u50a8\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570\u3002\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u7edf\u8ba1\u6bcf\u4e2a\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\u3002\u6700\u7ec8\u8fd4\u56de\u8fd9\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u6570\u5b66\u65b9\u6cd5\u7edf\u8ba1\u6570\u7684\u4e2a\u6570\u5b57<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u7b26\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bf9\u6570\u5b57\u8fdb\u884c\u64cd\u4f5c\uff0c\u7edf\u8ba1\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u60c5\u51b5\uff0c\u4f46\u9700\u8981\u4e00\u5b9a\u7684\u6570\u5b66\u57fa\u7840\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_digits(number):<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u6765\u5b58\u50a8\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570<\/p>\n<p>    digit_count = {str(i): 0 for i in range(10)}<\/p>\n<p>    # \u4f7f\u7528\u7edd\u5bf9\u503c\u786e\u4fdd\u5904\u7406\u8d1f\u6570<\/p>\n<p>    number = abs(number)<\/p>\n<p>    # \u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u7b26\u904d\u5386\u6bcf\u4e00\u4f4d\u6570\u5b57<\/p>\n<p>    while number &gt; 0:<\/p>\n<p>        digit = number % 10<\/p>\n<p>        digit_count[str(digit)] += 1<\/p>\n<p>        number \/\/= 10<\/p>\n<p>    return digit_count<\/p>\n<h2><strong>\u6d4b\u8bd5\u4f8b\u5b50<\/strong><\/h2>\n<p>number = 1234567890123456789<\/p>\n<p>print(count_digits(number))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u7b26\u76f4\u63a5\u5bf9\u6570\u5b57\u8fdb\u884c\u64cd\u4f5c\uff0c\u9996\u5148\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u7136\u540e\u901a\u8fc7\u53d6\u6a21\u8fd0\u7b97\u548c\u6574\u9664\u8fd0\u7b97\uff0c\u904d\u5386\u6bcf\u4e00\u4f4d\u6570\u5b57\u5e76\u7edf\u8ba1\u5176\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u7edf\u8ba1\u6570\u7684\u4e2a\u6570\u5b57<\/p>\n<\/p>\n<p><p>\u9012\u5f52\u65b9\u6cd5\u662f\u4e00\u79cd\u66f4\u4e3a\u9ad8\u7ea7\u7684\u6280\u672f\uff0c\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u51fd\u6570\u6765\u7edf\u8ba1\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u60c5\u51b5\uff0c\u4f46\u4ee3\u7801\u76f8\u5bf9\u8f83\u96be\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_digits(number, digit_count=None):<\/p>\n<p>    if digit_count is None:<\/p>\n<p>        digit_count = {str(i): 0 for i in range(10)}<\/p>\n<p>    # \u4f7f\u7528\u7edd\u5bf9\u503c\u786e\u4fdd\u5904\u7406\u8d1f\u6570<\/p>\n<p>    number = abs(number)<\/p>\n<p>    # \u9012\u5f52\u7ec8\u6b62\u6761\u4ef6<\/p>\n<p>    if number == 0:<\/p>\n<p>        return digit_count<\/p>\n<p>    digit = number % 10<\/p>\n<p>    digit_count[str(digit)] += 1<\/p>\n<p>    return count_digits(number \/\/ 10, digit_count)<\/p>\n<h2><strong>\u6d4b\u8bd5\u4f8b\u5b50<\/strong><\/h2>\n<p>number = 1234567890123456789<\/p>\n<p>print(count_digits(number))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u7edf\u8ba1\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570\u3002\u9996\u5148\u521d\u59cb\u5316\u4e00\u4e2a\u5b57\u5178\u6765\u5b58\u50a8\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u7136\u540e\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u51fd\u6570\uff0c\u9010\u4f4d\u7edf\u8ba1\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u76f4\u5230\u6570\u5b57\u4e3a0\u4e3a\u6b62\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7efc\u5408\u8fd0\u7528\u591a\u79cd\u65b9\u6cd5\u63d0\u9ad8\u4ee3\u7801\u6548\u7387<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7efc\u5408\u8fd0\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u8fdb\u4e00\u6b65\u4f18\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u7edf\u8ba1\u6570\u5b57\u51fa\u73b0\u6b21\u6570\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7ed3\u5408\u5b57\u7b26\u4e32\u65b9\u6cd5\u548c\u6570\u5b66\u65b9\u6cd5\uff0c\u5229\u7528Python\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u6765\u7b80\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u8fd0\u884c\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def count_digits(number):<\/p>\n<p>    # \u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\n<p>    num_str = str(number)<\/p>\n<p>    # \u4f7f\u7528Counter\u7edf\u8ba1\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570<\/p>\n<p>    digit_count = Counter(num_str)<\/p>\n<p>    return digit_count<\/p>\n<h2><strong>\u6d4b\u8bd5\u4f8b\u5b50<\/strong><\/h2>\n<p>number = 1234567890123456789<\/p>\n<p>print(count_digits(number))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5229\u7528Python\u7684collections\u5e93\u4e2d\u7684Counter\u7c7b\uff0c\u53ef\u4ee5\u5feb\u901f\u7edf\u8ba1\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u7b80\u5316\u4e86\u4ee3\u7801\uff0c\u63d0\u9ad8\u4e86\u8fd0\u884c\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6269\u5c55\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u7edf\u8ba1\u4e00\u4e2a\u6570\u7684\u5404\u4e2a\u6570\u5b57\u51fa\u73b0\u6b21\u6570\u5916\uff0c\u8fd8\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u65b9\u6cd5\u5e94\u7528\u4e8e\u5176\u4ed6\u573a\u666f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7edf\u8ba1\u4e00\u4e2a\u6587\u4ef6\u4e2d\u7684\u5b57\u7b26\u51fa\u73b0\u6b21\u6570\u3001\u7edf\u8ba1\u4e00\u6bb5\u6587\u672c\u4e2d\u7684\u5355\u8bcd\u51fa\u73b0\u6b21\u6570\u3001\u7edf\u8ba1\u67d0\u4e2a\u8303\u56f4\u5185\u6240\u6709\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def count_digits_in_range(start, end):<\/p>\n<p>    digit_count = Counter()<\/p>\n<p>    for number in range(start, end + 1):<\/p>\n<p>        num_str = str(number)<\/p>\n<p>        digit_count.update(num_str)<\/p>\n<p>    return digit_count<\/p>\n<h2><strong>\u6d4b\u8bd5\u4f8b\u5b50<\/strong><\/h2>\n<p>start, end = 1, 100<\/p>\n<p>print(count_digits_in_range(start, end))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u904d\u5386\u67d0\u4e2a\u8303\u56f4\u5185\u7684\u6240\u6709\u6570\u5b57\uff0c\u7edf\u8ba1\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u7edf\u8ba1\u4e00\u5b9a\u8303\u56f4\u5185\u7684\u6570\u5b57\u5206\u5e03\u60c5\u51b5\uff0c\u4e3a\u6570\u636e\u5206\u6790\u548c\u7814\u7a76\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u7279\u6b8a\u60c5\u51b5<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd8\u9700\u8981\u8003\u8651\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u4f8b\u5982\u5904\u7406\u8d1f\u6570\u3001\u5904\u7406\u6d6e\u70b9\u6570\u3001\u5904\u7406\u5927\u6570\u7b49\u3002\u8fd9\u4e9b\u7279\u6b8a\u60c5\u51b5\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u7edf\u8ba1\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_digits(number):<\/p>\n<p>    # \u5904\u7406\u8d1f\u6570<\/p>\n<p>    number = abs(number)<\/p>\n<p>    # \u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\n<p>    num_str = str(number)<\/p>\n<p>    # \u5904\u7406\u6d6e\u70b9\u6570<\/p>\n<p>    if &#39;.&#39; in num_str:<\/p>\n<p>        num_str = num_str.replace(&#39;.&#39;, &#39;&#39;)<\/p>\n<p>    # \u4f7f\u7528Counter\u7edf\u8ba1\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570<\/p>\n<p>    digit_count = Counter(num_str)<\/p>\n<p>    return digit_count<\/p>\n<h2><strong>\u6d4b\u8bd5\u4f8b\u5b50<\/strong><\/h2>\n<p>number = -12345.67890<\/p>\n<p>print(count_digits(number))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7abs\u51fd\u6570\u5904\u7406\u8d1f\u6570\uff0c\u901a\u8fc7replace\u51fd\u6570\u5904\u7406\u6d6e\u70b9\u6570\uff0c\u786e\u4fdd\u7edf\u8ba1\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u7edf\u8ba1\u4e00\u4e2a\u6570\u7684\u5404\u4e2a\u6570\u5b57\u51fa\u73b0\u6b21\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6570\u5b66\u65b9\u6cd5\u3001\u9012\u5f52\u65b9\u6cd5\u7b49\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408Python\u5185\u7f6e\u51fd\u6570\u548c\u5e93\uff0c\u8fdb\u4e00\u6b65\u4f18\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u8fd0\u884c\u6548\u7387\u3002\u540c\u65f6\uff0c\u8fd8\u9700\u8981\u8003\u8651\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u786e\u4fdd\u7edf\u8ba1\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0c\u4e3a\u5b9e\u9645\u5de5\u4f5c\u548c\u7814\u7a76\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u7edf\u8ba1\u4e00\u4e2a\u6570\u5b57\u4e2d\u5404\u4e2a\u6570\u5b57\u7684\u4e2a\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528\u5b57\u5178\u6765\u7edf\u8ba1\u6bcf\u4e2a\u6570\u5b57\u51fa\u73b0\u7684\u6b21\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">def count_digits(number):\n    digit_count = {}\n    for digit in str(number):\n        if digit in digit_count:\n            digit_count[digit] += 1\n        else:\n            digit_count[digit] = 1\n    return digit_count\n\nresult = count_digits(123321)\nprint(result)  # \u8f93\u51fa\uff1a{&#39;1&#39;: 1, &#39;2&#39;: 2, &#39;3&#39;: 2}\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5404\u7c7b\u6570\u5b57\u7edf\u8ba1\u9700\u6c42\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48Python\u5e93\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u6570\u5b57\u51fa\u73b0\u7684\u9891\u7387\uff1f<\/strong><br \/>Python\u7684<code>collections<\/code>\u6a21\u5757\u4e2d\u7684<code>Counter<\/code>\u7c7b\u975e\u5e38\u9002\u5408\u7528\u4e8e\u7edf\u8ba1\u5143\u7d20\u9891\u7387\u3002\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\ndef count_digits_with_counter(number):\n    return Counter(str(number))\n\nresult = count_digits_with_counter(112233)\nprint(result)  # \u8f93\u51fa\uff1aCounter({&#39;1&#39;: 2, &#39;2&#39;: 2, &#39;3&#39;: 2})\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u529f\u80fd\u5f3a\u5927\uff0c\u80fd\u591f\u76f4\u63a5\u83b7\u5f97\u6570\u5b57\u7684\u9891\u7387\u5206\u5e03\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8d1f\u6570\u548c\u5c0f\u6570\u7684\u6570\u5b57\u7edf\u8ba1\uff1f<\/strong><br \/>\u5728\u5904\u7406\u8d1f\u6570\u548c\u5c0f\u6570\u65f6\uff0c\u53ef\u4ee5\u5148\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\u7edd\u5bf9\u503c\u5e76\u53bb\u6389\u5c0f\u6570\u70b9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">def count_digits_float(number):\n    number_str = str(abs(number)).replace(&#39;.&#39;, &#39;&#39;)\n    return count_digits(number_str)\n\nresult = count_digits_float(-12.345)\nprint(result)  # \u8f93\u51fa\uff1a{&#39;1&#39;: 1, &#39;2&#39;: 1, &#39;3&#39;: 1, &#39;4&#39;: 1, &#39;5&#39;: 1}\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u5904\u7406\u65b9\u5f0f\u786e\u4fdd\u6240\u6709\u6570\u5b57\u90fd\u80fd\u88ab\u6b63\u786e\u7edf\u8ba1\uff0c\u65e0\u8bba\u662f\u6b63\u6570\u3001\u8d1f\u6570\u8fd8\u662f\u5c0f\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u7edf\u8ba1\u6570\u7684\u4e2a\u6570\u5b57 \u7528Python\u7edf\u8ba1\u4e00\u4e2a\u6570\u7684\u5404\u4e2a\u6570\u5b57\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6570\u5b66\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1073270,"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\/1073258"}],"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=1073258"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073258\/revisions"}],"predecessor-version":[{"id":1073273,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073258\/revisions\/1073273"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073270"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}