{"id":1073996,"date":"2025-01-08T11:30:38","date_gmt":"2025-01-08T03:30:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073996.html"},"modified":"2025-01-08T11:30:41","modified_gmt":"2025-01-08T03:30:41","slug":"python%e4%b8%ad%e7%bb%9f%e8%ae%a1%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%a6%82%e4%bd%95%e7%bc%96%e7%a8%8b-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073996.html","title":{"rendered":"python\u4e2d\u7edf\u8ba1\u5b57\u7b26\u4e32\u5982\u4f55\u7f16\u7a0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103736\/86be3918-f9bd-4c98-ba36-bf917065791b.webp\" alt=\"python\u4e2d\u7edf\u8ba1\u5b57\u7b26\u4e32\u5982\u4f55\u7f16\u7a0b\" \/><\/p>\n<p><p> \u4e00\u3001\u5728Python\u4e2d\u7edf\u8ba1\u5b57\u7b26\u4e32\u7684\u7f16\u7a0b\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570count()\u3001\u4f7f\u7528collections\u6a21\u5757\u7684Counter\u7c7b\u3001\u4f7f\u7528\u5b57\u5178\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528collections\u6a21\u5757\u7684Counter\u7c7b<\/strong>\u662f\u8f83\u4e3a\u5e38\u7528\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u65b9\u6cd5\u3002Counter\u7c7b\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5404\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5e76\u63d0\u4f9b\u4e30\u5bcc\u7684\u64cd\u4f5c\u51fd\u6570\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Counter\u7c7b\u8fdb\u884c\u5b57\u7b26\u4e32\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Counter\u7c7b\u8fdb\u884c\u5b57\u7b26\u4e32\u7edf\u8ba1\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5bfc\u5165collections\u6a21\u5757\u5e76\u521b\u5efa\u4e00\u4e2aCounter\u5bf9\u8c61\uff0c\u5c06\u5b57\u7b26\u4e32\u4f20\u5165\u5373\u53ef\u3002Counter\u7c7b\u4f1a\u81ea\u52a8\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5e76\u5c06\u7ed3\u679c\u4ee5\u5b57\u5178\u5f62\u5f0f\u8fd4\u56de\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def count_characters(s):<\/p>\n<p>    return Counter(s)<\/p>\n<p>string = &quot;hello world&quot;<\/p>\n<p>result = count_characters(string)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86collections\u6a21\u5757\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2aCounter\u5bf9\u8c61\uff0c\u5c06\u5b57\u7b26\u4e32\u4f20\u5165\u540e\uff0cCounter\u7c7b\u4f1a\u81ea\u52a8\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5e76\u5c06\u7ed3\u679c\u4ee5\u5b57\u5178\u5f62\u5f0f\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5185\u7f6e\u51fd\u6570count()\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>Python\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u65b9\u6cd5count()\uff0c\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u867d\u7136count()\u65b9\u6cd5\u4e0d\u80fd\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u4f46\u5bf9\u4e8e\u7edf\u8ba1\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32\u7684\u51fa\u73b0\u6b21\u6570\u975e\u5e38\u6709\u7528\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;hello world&quot;<\/p>\n<p>substring = &quot;l&quot;<\/p>\n<p>count = string.count(substring)<\/p>\n<p>print(f&quot;&#39;{substring}&#39; appears {count} times in &#39;{string}&#39;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528count()\u65b9\u6cd5\u7edf\u8ba1\u4e86\u5b50\u5b57\u7b26\u4e32&quot;l&quot;\u5728\u5b57\u7b26\u4e32&quot;hello world&quot;\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u5e76\u5c06\u7ed3\u679c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u5b57\u7b26\u4e32\u7edf\u8ba1<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528Counter\u7c7b\u548ccount()\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5404\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u8f83\u4e3a\u539f\u59cb\uff0c\u4f46\u5bf9\u4e8e\u7406\u89e3\u5b57\u7b26\u4e32\u7edf\u8ba1\u7684\u57fa\u672c\u539f\u7406\u975e\u5e38\u6709\u5e2e\u52a9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_characters(s):<\/p>\n<p>    char_count = {}<\/p>\n<p>    for char in s:<\/p>\n<p>        if char in char_count:<\/p>\n<p>            char_count[char] += 1<\/p>\n<p>        else:<\/p>\n<p>            char_count[char] = 1<\/p>\n<p>    return char_count<\/p>\n<p>string = &quot;hello world&quot;<\/p>\n<p>result = count_characters(string)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570count_characters()\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4f7f\u7528\u5b57\u5178\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u662f\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5982\u679c\u5b57\u7b26\u5df2\u7ecf\u5728\u5b57\u5178\u4e2d\uff0c\u5219\u5c06\u5176\u5bf9\u5e94\u7684\u503c\u52a01\uff1b\u5982\u679c\u5b57\u7b26\u4e0d\u5728\u5b57\u5178\u4e2d\uff0c\u5219\u5c06\u5b57\u7b26\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff0c\u5e76\u5c06\u5bf9\u5e94\u7684\u503c\u8bbe\u4e3a1\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5b57\u7b26\u4e32\u7edf\u8ba1<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u5de5\u5177\uff0c\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528re\u6a21\u5757\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u867d\u7136\u6b63\u5219\u8868\u8fbe\u5f0f\u4e3b\u8981\u7528\u4e8e\u5b57\u7b26\u4e32\u5339\u914d\u548c\u66ff\u6362\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u6a21\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def count_pattern(s, pattern):<\/p>\n<p>    matches = re.findall(pattern, s)<\/p>\n<p>    return len(matches)<\/p>\n<p>string = &quot;hello world&quot;<\/p>\n<p>pattern = &quot;l&quot;<\/p>\n<p>count = count_pattern(string, pattern)<\/p>\n<p>print(f&quot;Pattern &#39;{pattern}&#39; appears {count} times in &#39;{string}&#39;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528re.findall()\u51fd\u6570\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5339\u914d\u6a21\u5f0f\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u7edf\u8ba1\u5339\u914d\u7684\u6570\u91cf\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u7edf\u8ba1\u7279\u5b9a\u6a21\u5f0f\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u5355\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5b57\u7b26\u4e32\u7edf\u8ba1\u7684\u5b9e\u9645\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u7edf\u8ba1\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u5f88\u591a\u7528\u9014\uff0c\u4f8b\u5982\u6587\u672c\u5206\u6790\u3001\u65e5\u5fd7\u5904\u7406\u3001\u6570\u636e\u6e05\u6d17\u7b49\u3002\u901a\u8fc7\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u6216\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6587\u672c\u5206\u6790<\/li>\n<\/ol>\n<p><p>\u5728\u6587\u672c\u5206\u6790\u4e2d\uff0c\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u6216\u5355\u8bcd\u51fa\u73b0\u9891\u7387\u662f\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7edf\u8ba1\u6587\u7ae0\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u4ece\u800c\u5206\u6790\u6587\u7ae0\u7684\u4e3b\u9898\u548c\u98ce\u683c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def count_words(text):<\/p>\n<p>    words = text.split()<\/p>\n<p>    return Counter(words)<\/p>\n<p>text = &quot;this is a sample text with several words this is a test&quot;<\/p>\n<p>result = count_words(text)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570count_words()\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u6587\u672c\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u7edf\u8ba1\u6587\u672c\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u7684\u51fa\u73b0\u6b21\u6570\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u662f\u5c06\u6587\u672c\u6309\u7a7a\u683c\u5206\u5272\u6210\u5355\u8bcd\u5217\u8868\uff0c\u5e76\u4f7f\u7528Counter\u7c7b\u7edf\u8ba1\u5355\u8bcd\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u65e5\u5fd7\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u5728\u65e5\u5fd7\u5904\u7406\u4e2d\uff0c\u7edf\u8ba1\u65e5\u5fd7\u4e2d\u51fa\u73b0\u7684\u7279\u5b9a\u6a21\u5f0f\u6216\u5173\u952e\u5b57\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u53d1\u73b0\u95ee\u9898\u548c\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7edf\u8ba1\u65e5\u5fd7\u4e2d\u51fa\u73b0\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u4ece\u800c\u5b9a\u4f4d\u6545\u969c\u70b9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def count_errors(log):<\/p>\n<p>    pattern = &quot;ERROR&quot;<\/p>\n<p>    matches = re.findall(pattern, log)<\/p>\n<p>    return len(matches)<\/p>\n<p>log = &quot;&quot;&quot;<\/p>\n<p>INFO Starting application<\/p>\n<p>ERROR F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to connect to database<\/p>\n<p>INFO Application running<\/p>\n<p>ERROR Timeout occurred<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>count = count_errors(log)<\/p>\n<p>print(f&quot;Number of errors in log: {count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570count_errors()\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u65e5\u5fd7\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u7edf\u8ba1\u65e5\u5fd7\u4e2d\u51fa\u73b0\u7684\u9519\u8bef\u4fe1\u606f\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u662f\u4f7f\u7528re.findall()\u51fd\u6570\u67e5\u627e\u6240\u6709\u5339\u914d\u6a21\u5f0f&quot;ERROR&quot;\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u7edf\u8ba1\u5339\u914d\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u6570\u636e\u6e05\u6d17<\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u6e05\u6d17\u8fc7\u7a0b\u4e2d\uff0c\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u6216\u6a21\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u53d1\u73b0\u6570\u636e\u4e2d\u7684\u5f02\u5e38\u548c\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7edf\u8ba1\u6570\u636e\u4e2d\u51fa\u73b0\u7684\u7279\u6b8a\u5b57\u7b26\uff0c\u4ece\u800c\u8fdb\u884c\u6e05\u6d17\u548c\u66ff\u6362\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def count_special_characters(data):<\/p>\n<p>    pattern = r&quot;[^a-zA-Z0-9\\s]&quot;<\/p>\n<p>    matches = re.findall(pattern, data)<\/p>\n<p>    return len(matches)<\/p>\n<p>data = &quot;This is a sample data with special characters: @#$%^&amp;*()!&quot;<\/p>\n<p>count = count_special_characters(data)<\/p>\n<p>print(f&quot;Number of special characters in data: {count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570count_special_characters()\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u6570\u636e\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u7edf\u8ba1\u6570\u636e\u4e2d\u51fa\u73b0\u7684\u7279\u6b8a\u5b57\u7b26\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u662f\u4f7f\u7528re.findall()\u51fd\u6570\u67e5\u627e\u6240\u6709\u5339\u914d\u6a21\u5f0f&quot;[^a-zA-Z0-9\\s]&quot;\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u7edf\u8ba1\u5339\u914d\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f18\u5316\u5b57\u7b26\u4e32\u7edf\u8ba1\u7684\u6027\u80fd<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u5b57\u7b26\u4e32\u7edf\u8ba1\u7684\u6027\u80fd\u975e\u5e38\u91cd\u8981\u3002\u4e3a\u4e86\u63d0\u9ad8\u5b57\u7b26\u4e32\u7edf\u8ba1\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784<\/li>\n<\/ol>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5b57\u7b26\u4e32\u7edf\u8ba1\u7684\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Counter\u7c7b\u53ef\u4ee5\u9ad8\u6548\u5730\u7edf\u8ba1\u5b57\u7b26\u6216\u5355\u8bcd\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u800c\u4f7f\u7528\u5b57\u5178\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u5404\u79cd\u7edf\u8ba1\u9700\u6c42\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u91c7\u7528\u5e76\u884c\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u53ef\u4ee5\u91c7\u7528\u5e76\u884c\u5904\u7406\u7684\u65b9\u5f0f\uff0c\u5c06\u6570\u636e\u5206\u5272\u6210\u591a\u4e2a\u5b50\u4efb\u52a1\uff0c\u5e76\u884c\u6267\u884c\u7edf\u8ba1\u64cd\u4f5c\u3002Python\u7684multiprocessing\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u8fdb\u7a0b\u5904\u7406\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Pool<\/p>\n<p>from collections import Counter<\/p>\n<p>def count_characters_chunk(chunk):<\/p>\n<p>    return Counter(chunk)<\/p>\n<p>def count_characters_parallel(s, num_processes=4):<\/p>\n<p>    chunk_size = len(s) \/\/ num_processes<\/p>\n<p>    chunks = [s[i:i+chunk_size] for i in range(0, len(s), chunk_size)]<\/p>\n<p>    with Pool(num_processes) as pool:<\/p>\n<p>        results = pool.map(count_characters_chunk, chunks)<\/p>\n<p>    total_count = Counter()<\/p>\n<p>    for result in results:<\/p>\n<p>        total_count.update(result)<\/p>\n<p>    return total_count<\/p>\n<p>string = &quot;hello world&quot; * 1000000<\/p>\n<p>result = count_characters_parallel(string)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570count_characters_parallel()\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u5b57\u7b26\u4e32\u548c\u8fdb\u7a0b\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u884c\u6267\u884c\u5b57\u7b26\u4e32\u7edf\u8ba1\u64cd\u4f5c\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u662f\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u5b50\u4efb\u52a1\uff0c\u4f7f\u7528multiprocessing.Pool\u5e76\u884c\u6267\u884c\u7edf\u8ba1\u64cd\u4f5c\uff0c\u5e76\u5c06\u7ed3\u679c\u5408\u5e76\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u8d85\u5927\u89c4\u6a21\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6\uff08memory-mapped file\uff09\u8fdb\u884c\u5904\u7406\u3002\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u5141\u8bb8\u6211\u4eec\u5c06\u6587\u4ef6\u7684\u4e00\u90e8\u5206\u6620\u5c04\u5230\u5185\u5b58\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u6548\u7684\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u3002Python\u7684mmap\u6a21\u5757\u63d0\u4f9b\u4e86\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u7684\u652f\u6301\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mmap<\/p>\n<p>from collections import Counter<\/p>\n<p>def count_characters_mmap(file_path):<\/p>\n<p>    with open(file_path, &quot;r+b&quot;) as f:<\/p>\n<p>        mmapped_file = mmap.mmap(f.fileno(), 0)<\/p>\n<p>        counter = Counter(mmapped_file)<\/p>\n<p>        mmapped_file.close()<\/p>\n<p>    return counter<\/p>\n<p>file_path = &quot;large_text_file.txt&quot;<\/p>\n<p>result = count_characters_mmap(file_path)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570count_characters_mmap()\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u8fdb\u884c\u5b57\u7b26\u4e32\u7edf\u8ba1\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u662f\u6253\u5f00\u6587\u4ef6\u5e76\u521b\u5efa\u5185\u5b58\u6620\u5c04\u5bf9\u8c61\uff0c\u4f7f\u7528Counter\u7c7b\u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u6b21\u6570\uff0c\u5e76\u5173\u95ed\u5185\u5b58\u6620\u5c04\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u7edf\u8ba1\u5b57\u7b26\u4e32\u7684\u591a\u79cd\u7f16\u7a0b\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570count()\u3001\u4f7f\u7528collections\u6a21\u5757\u7684Counter\u7c7b\u3001\u4f7f\u7528\u5b57\u5178\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u901a\u8fc7\u793a\u4f8b\u5c55\u793a\u4e86\u8fd9\u4e9b\u65b9\u6cd5\u5728\u4e0d\u540c\u5e94\u7528\u573a\u666f\u4e0b\u7684\u5b9e\u9645\u4f7f\u7528\uff0c\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3\u5e76\u638c\u63e1\u5b57\u7b26\u4e32\u7edf\u8ba1\u7684\u57fa\u672c\u539f\u7406\u548c\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u672c\u6587\u8fd8\u4ecb\u7ecd\u4e86\u5b57\u7b26\u4e32\u7edf\u8ba1\u5728\u6587\u672c\u5206\u6790\u3001\u65e5\u5fd7\u5904\u7406\u3001\u6570\u636e\u6e05\u6d17\u7b49\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u91cd\u8981\u6027\uff0c\u5e76\u7ed9\u51fa\u4e86\u4f18\u5316\u5b57\u7b26\u4e32\u7edf\u8ba1\u6027\u80fd\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u3001\u91c7\u7528\u5e76\u884c\u5904\u7406\u3001\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u8bfb\u8005\u80fd\u591f\u638c\u63e1Python\u4e2d\u7edf\u8ba1\u5b57\u7b26\u4e32\u7684\u591a\u79cd\u7f16\u7a0b\u65b9\u6cd5\uff0c\u5e76\u80fd\u591f\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u6570\u636e\u5904\u7406\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u8fdb\u884c\u6587\u672c\u5206\u6790\u3001\u65e5\u5fd7\u5904\u7406\uff0c\u8fd8\u662f\u6570\u636e\u6e05\u6d17\uff0c\u5b57\u7b26\u4e32\u7edf\u8ba1\u90fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u57fa\u7840\u6280\u80fd\u3002\u638c\u63e1\u5b57\u7b26\u4e32\u7edf\u8ba1\u7684\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u4e0d\u4ec5\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u5904\u7406\u6570\u636e\uff0c\u8fd8\u80fd\u591f\u4e3a\u8fdb\u4e00\u6b65\u7684\u6570\u636e\u5206\u6790\u548c\u6316\u6398\u6253\u4e0b\u575a\u5b9e\u7684\u57fa\u7840\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\u7684\u5b57\u7b26\u9891\u7387\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>collections.Counter<\/code>\u7c7b\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u9891\u7387\u3002\u53ea\u9700\u5bfc\u5165\u8be5\u6a21\u5757\u5e76\u4f20\u5165\u5b57\u7b26\u4e32\uff0cCounter\u5c06\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u4e2a\u5b57\u7b26\u53ca\u5176\u5bf9\u5e94\u7684\u8ba1\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">from collections import Counter\nstring = &quot;hello world&quot;\nfrequency = Counter(string)\nprint(frequency)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u6bcf\u4e2a\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5355\u8bcd\u6570\u91cf\uff1f<\/strong><br \/>\u8981\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5355\u8bcd\u7684\u6570\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u7a7a\u683c\u5206\u5272\uff0c\u7136\u540e\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u8ba1\u7b97\u5206\u5272\u540e\u5217\u8868\u7684\u957f\u5ea6\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">string = &quot;Hello world, welcome to Python programming.&quot;\nword_count = len(string.split())\nprint(word_count)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u5b57\u7b26\u4e32\u4e2d\u5355\u8bcd\u7684\u603b\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u627e\u51fa\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u5b57\u7b26\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528<code>collections.Counter<\/code>\u548c<code>most_common<\/code>\u65b9\u6cd5\u6765\u627e\u5230\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u5b57\u7b26\u3002<code>most_common<\/code>\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u6309\u5b57\u7b26\u9891\u7387\u4ece\u9ad8\u5230\u4f4e\u6392\u5217\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">from collections import Counter\nstring = &quot;character frequency&quot;\nfrequency = Counter(string)\nmost_common_char = frequency.most_common(1)\nprint(most_common_char)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u5b57\u7b26\u53ca\u5176\u6570\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u5728Python\u4e2d\u7edf\u8ba1\u5b57\u7b26\u4e32\u7684\u7f16\u7a0b\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570count()\u3001\u4f7f\u7528collection [&hellip;]","protected":false},"author":3,"featured_media":1073999,"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\/1073996"}],"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=1073996"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073996\/revisions"}],"predecessor-version":[{"id":1074000,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073996\/revisions\/1074000"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073999"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}