{"id":1066547,"date":"2024-12-31T16:27:19","date_gmt":"2024-12-31T08:27:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1066547.html"},"modified":"2024-12-31T16:27:22","modified_gmt":"2024-12-31T08:27:22","slug":"python%e5%a6%82%e4%bd%95%e6%95%b0%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1066547.html","title":{"rendered":"Python\u5982\u4f55\u6570\u5b57\u7b26\u4e32\u7684\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/13faa70a-444c-43f4-b40c-03a75720fa41.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u5982\u4f55\u6570\u5b57\u7b26\u4e32\u7684\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u548c\u5faa\u73af\u7ed3\u6784\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u6570\u5b57\u7684\u4e2a\u6570\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ecisdigit()\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002<\/strong> \u5176\u4e2d\uff0cisdigit()\u65b9\u6cd5\u662f\u4e00\u79cd\u7b80\u5355\u76f4\u89c2\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570isdigit()<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u7b26\u4e32\u7684isdigit()\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u7684\u4e00\u79cd\u65b9\u5f0f\u3002isdigit()\u65b9\u6cd5\u7528\u4e8e\u68c0\u6d4b\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\u3002\u5982\u679c\u662f\u6570\u5b57\u5b57\u7b26\uff0c\u5219\u8fd4\u56deTrue\uff0c\u5426\u5219\u8fd4\u56deFalse\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_digits(s):<\/p>\n<p>    count = 0<\/p>\n<p>    for char in s:<\/p>\n<p>        if char.isdigit():<\/p>\n<p>            count += 1<\/p>\n<p>    return count<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python1234Programming5678&quot;<\/p>\n<p>print(f&quot;\u6570\u5b57\u7684\u4e2a\u6570\u662f: {count_digits(input_string)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5224\u65ad\u5176\u662f\u5426\u4e3a\u6570\u5b57\u5b57\u7b26\uff0c\u5982\u679c\u662f\uff0c\u5219\u8ba1\u6570\u5668\u52a01\u3002\u6700\u7ec8\u8fd4\u56de\u8ba1\u6570\u5668\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5b57\u7b26\u4e32\u5339\u914d\u548c\u641c\u7d22\u3002Python\u7684re\u6a21\u5757\u63d0\u4f9b\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u76f8\u5173\u7684\u529f\u80fd\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def count_digits_regex(s):<\/p>\n<p>    pattern = re.compile(r&#39;\\d&#39;)<\/p>\n<p>    return len(pattern.findall(s))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python1234Programming5678&quot;<\/p>\n<p>print(f&quot;\u6570\u5b57\u7684\u4e2a\u6570\u662f: {count_digits_regex(input_string)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<code>\\d<\/code>\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528findall()\u65b9\u6cd5\u627e\u5230\u6240\u6709\u5339\u914d\u7684\u6570\u5b57\u5b57\u7b26\u3002\u8fd4\u56de\u627e\u5230\u7684\u6570\u5b57\u5b57\u7b26\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u4e2a\u6570\u3002\u4e0e\u524d\u4e24\u79cd\u65b9\u6cd5\u76f8\u6bd4\uff0c\u5217\u8868\u89e3\u6790\u66f4\u52a0\u7d27\u51d1\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_digits_list_comp(s):<\/p>\n<p>    return sum(1 for char in s if char.isdigit())<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python1234Programming5678&quot;<\/p>\n<p>print(f&quot;\u6570\u5b57\u7684\u4e2a\u6570\u662f: {count_digits_list_comp(input_string)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u5224\u65ad\u5176\u662f\u5426\u4e3a\u6570\u5b57\u5b57\u7b26\u3002\u5982\u679c\u662f\uff0c\u5219\u751f\u6210\u4e00\u4e2a1\uff0c\u6700\u540e\u8ba1\u7b97\u8fd9\u4e9b1\u7684\u603b\u548c\uff0c\u5373\u4e3a\u6570\u5b57\u5b57\u7b26\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528filter()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>filter()\u51fd\u6570\u7528\u4e8e\u6784\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5176\u4e2d\u5305\u542b\u6240\u6709\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002\u7ed3\u5408isdigit()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_digits_filter(s):<\/p>\n<p>    return len(list(filter(str.isdigit, s)))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python1234Programming5678&quot;<\/p>\n<p>print(f&quot;\u6570\u5b57\u7684\u4e2a\u6570\u662f: {count_digits_filter(input_string)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528filter()\u51fd\u6570\u8fc7\u6ee4\u51fa\u6240\u6709\u6570\u5b57\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u7136\u540e\u8ba1\u7b97\u5217\u8868\u7684\u957f\u5ea6\uff0c\u5373\u4e3a\u6570\u5b57\u5b57\u7b26\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528map()\u51fd\u6570\u548clambda\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>map()\u51fd\u6570\u7528\u4e8e\u5c06\u7279\u5b9a\u51fd\u6570\u5e94\u7528\u4e8e\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u7ed3\u5408lambda\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u6570\u5b57\u4e2a\u6570\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_digits_map(s):<\/p>\n<p>    return sum(map(lambda char: char.isdigit(), s))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python1234Programming5678&quot;<\/p>\n<p>print(f&quot;\u6570\u5b57\u7684\u4e2a\u6570\u662f: {count_digits_map(input_string)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528map()\u51fd\u6570\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u5224\u65ad\u5176\u662f\u5426\u4e3a\u6570\u5b57\u5b57\u7b26\u3002\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56deTrue\uff0c\u5426\u5219\u8fd4\u56deFalse\u3002\u6700\u540e\u8ba1\u7b97\u8fd9\u4e9bTrue\u7684\u603b\u548c\uff0c\u5373\u4e3a\u6570\u5b57\u5b57\u7b26\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Counter\u7c7b<\/h3>\n<\/p>\n<p><p>Counter\u7c7b\u662fcollections\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5de5\u5177\uff0c\u7528\u4e8e\u7edf\u8ba1\u53ef\u54c8\u5e0c\u5bf9\u8c61\u7684\u51fa\u73b0\u9891\u7387\u3002\u7ed3\u5408Counter\u7c7b\uff0c\u53ef\u4ee5\u9ad8\u6548\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def count_digits_counter(s):<\/p>\n<p>    counter = Counter(s)<\/p>\n<p>    return sum(counter[char] for char in counter if char.isdigit())<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python1234Programming5678&quot;<\/p>\n<p>print(f&quot;\u6570\u5b57\u7684\u4e2a\u6570\u662f: {count_digits_counter(input_string)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Counter\u7c7b\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e00\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u9891\u7387\uff0c\u5e76\u8ba1\u7b97\u6240\u6709\u6570\u5b57\u5b57\u7b26\u7684\u603b\u548c\uff0c\u5373\u4e3a\u6570\u5b57\u5b57\u7b26\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u6765\u5b9e\u73b0\u3002\u9012\u5f52\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u4e2a\u6570\uff0c\u5c3d\u7ba1\u5728\u5b9e\u8df5\u4e2d\u4e0d\u5982\u524d\u8ff0\u65b9\u6cd5\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_digits_recursive(s):<\/p>\n<p>    if not s:<\/p>\n<p>        return 0<\/p>\n<p>    return (1 if s[0].isdigit() else 0) + count_digits_recursive(s[1:])<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python1234Programming5678&quot;<\/p>\n<p>print(f&quot;\u6570\u5b57\u7684\u4e2a\u6570\u662f: {count_digits_recursive(input_string)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u662f\u5426\u4e3a\u6570\u5b57\u5b57\u7b26\uff0c\u5e76\u9012\u5f52\u8ba1\u7b97\u5269\u4f59\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u5b57\u7b26\u7684\u4e2a\u6570\u3002\u6700\u7ec8\u8fd4\u56de\u6240\u6709\u6570\u5b57\u5b57\u7b26\u7684\u603b\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7c7b\u4f3c\u4e8e\u5217\u8868\u89e3\u6790\u7684\u8868\u8fbe\u5f0f\uff0c\u4f46\u5b83\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u5217\u8868\u3002\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u5728\u5185\u5b58\u6548\u7387\u65b9\u9762\u66f4\u4f18\uff0c\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_digits_generator(s):<\/p>\n<p>    return sum(1 for char in s if char.isdigit())<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python1234Programming5678&quot;<\/p>\n<p>print(f&quot;\u6570\u5b57\u7684\u4e2a\u6570\u662f: {count_digits_generator(input_string)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u5224\u65ad\u5176\u662f\u5426\u4e3a\u6570\u5b57\u5b57\u7b26\u3002\u5982\u679c\u662f\uff0c\u5219\u751f\u6210\u4e00\u4e2a1\uff0c\u6700\u540e\u8ba1\u7b97\u8fd9\u4e9b1\u7684\u603b\u548c\uff0c\u5373\u4e3a\u6570\u5b57\u5b57\u7b26\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u5916\u90e8\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Python\u5185\u7f6e\u7684\u5e93\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5916\u90e8\u5e93\u6765\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u4e2a\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def count_digits_pandas(s):<\/p>\n<p>    return pd.Series(list(s)).apply(lambda x: x.isdigit()).sum()<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python1234Programming5678&quot;<\/p>\n<p>print(f&quot;\u6570\u5b57\u7684\u4e2a\u6570\u662f: {count_digits_pandas(input_string)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pandas\u5e93\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aSeries\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528apply()\u65b9\u6cd5\u5224\u65ad\u6bcf\u4e00\u4e2a\u5b57\u7b26\u662f\u5426\u4e3a\u6570\u5b57\u5b57\u7b26\u3002\u6700\u540e\u8ba1\u7b97\u6240\u6709True\u7684\u603b\u548c\uff0c\u5373\u4e3a\u6570\u5b57\u5b57\u7b26\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002\u5c3d\u7ba1NumPy\u4e3b\u8981\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def count_digits_numpy(s):<\/p>\n<p>    return np.sum([char.isdigit() for char in s])<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python1234Programming5678&quot;<\/p>\n<p>print(f&quot;\u6570\u5b57\u7684\u4e2a\u6570\u662f: {count_digits_numpy(input_string)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528NumPy\u5e93\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\u662f\u5426\u4e3a\u6570\u5b57\u5b57\u7b26\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u6570\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6570\u7ec4\u4e2dTrue\u7684\u603b\u548c\uff0c\u5373\u4e3a\u6570\u5b57\u5b57\u7b26\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u4e2a\u6570\uff0c\u901a\u8fc7\u66ff\u6362\u6240\u6709\u7684\u6570\u5b57\u5b57\u7b26\uff0c\u7136\u540e\u8ba1\u7b97\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_digits_replace(s):<\/p>\n<p>    return len(s) - len(s.translate(str.maketrans(&#39;&#39;, &#39;&#39;, &#39;0123456789&#39;)))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python1234Programming5678&quot;<\/p>\n<p>print(f&quot;\u6570\u5b57\u7684\u4e2a\u6570\u662f: {count_digits_replace(input_string)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528translate()\u65b9\u6cd5\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\u5b57\u7b26\uff0c\u7136\u540e\u8ba1\u7b97\u66ff\u6362\u524d\u540e\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\u5dee\u503c\uff0c\u5373\u4e3a\u6570\u5b57\u5b57\u7b26\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u961f\u5217\uff08deque\uff09\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u53cc\u7aef\u961f\u5217\uff08deque\uff09\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u961f\u5217\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9891\u7e41\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u573a\u666f\u3002\u4f7f\u7528deque\u53ef\u4ee5\u5b9e\u73b0\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>def count_digits_deque(s):<\/p>\n<p>    d = deque(s)<\/p>\n<p>    return sum(1 for char in d if char.isdigit())<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python1234Programming5678&quot;<\/p>\n<p>print(f&quot;\u6570\u5b57\u7684\u4e2a\u6570\u662f: {count_digits_deque(input_string)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adeque\u5bf9\u8c61\uff0c\u5e76\u904d\u5386\u5176\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5224\u65ad\u5176\u662f\u5426\u4e3a\u6570\u5b57\u5b57\u7b26\u3002\u5982\u679c\u662f\uff0c\u5219\u8ba1\u6570\u5668\u52a01\u3002\u6700\u7ec8\u8fd4\u56de\u8ba1\u6570\u5668\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u4e2a\u6570\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5217\u8868\u89e3\u6790\u3001filter()\u51fd\u6570\u3001map()\u51fd\u6570\u548clambda\u8868\u8fbe\u5f0f\u3001Counter\u7c7b\u3001\u9012\u5f52\u65b9\u6cd5\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u3001\u5916\u90e8\u5e93\uff08\u5982pandas\u548cNumPy\uff09\u3001\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5\u3001\u4ee5\u53ca\u961f\u5217\uff08deque\uff09\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6570\u5b57\u7684\u6570\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528\u5b57\u7b26\u4e32\u7684\u904d\u5386\u4e0e\u6761\u4ef6\u5224\u65ad\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6570\u5b57\u7684\u6570\u91cf\u3002\u4f7f\u7528<code>isdigit()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5224\u65ad\u6bcf\u4e2a\u5b57\u7b26\u662f\u5426\u4e3a\u6570\u5b57\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">def count_digits(s):\n    return sum(c.isdigit() for c in s)\n\nstring = &quot;Hello 1234, this is a test 5678!&quot;\ndigit_count = count_digits(string)\nprint(&quot;\u6570\u5b57\u7684\u4e2a\u6570\u662f:&quot;, digit_count)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u6570\u5b57\u5b57\u7b26\u7684\u603b\u6570\u91cf\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u73b0\u6210\u7684\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\uff1f<\/strong><br \/>\u786e\u5b9e\u6709\u4e00\u4e9b\u5e93\u53ef\u4ee5\u7b80\u5316\u8fd9\u4e2a\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u6570\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\nstring = &quot;Hello 1234, this is a test 5678!&quot;\ndigit_count = len(re.findall(r&#39;\\d&#39;, string))\nprint(&quot;\u6570\u5b57\u7684\u4e2a\u6570\u662f:&quot;, digit_count)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<code>\\d<\/code>\u5339\u914d\u6240\u6709\u6570\u5b57\u5b57\u7b26\uff0c\u4ece\u800c\u8fd4\u56de\u6570\u5b57\u7684\u603b\u6570\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u7edf\u8ba1\u591a\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\uff0c\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u53ef\u4ee5\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u653e\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5b57\u7b26\u4e32\uff0c\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u6570\u91cf\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">def count_digits_in_list(strings):\n    return [count_digits(s) for s in strings]\n\nstring_list = [&quot;Hello 123&quot;, &quot;Test 4567&quot;, &quot;No digits here&quot;]\ndigit_counts = count_digits_in_list(string_list)\nprint(&quot;\u6bcf\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u4e2a\u6570:&quot;, digit_counts)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u4e2a\u5b57\u7b26\u4e32\u4e2d\u6570\u5b57\u7684\u6570\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u548c\u5faa\u73af\u7ed3\u6784\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u6570\u5b57\u7684\u4e2a\u6570\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ecisdigi [&hellip;]","protected":false},"author":3,"featured_media":1066563,"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\/1066547"}],"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=1066547"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1066547\/revisions"}],"predecessor-version":[{"id":1066564,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1066547\/revisions\/1066564"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1066563"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1066547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1066547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1066547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}