{"id":1165114,"date":"2025-01-15T15:16:23","date_gmt":"2025-01-15T07:16:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1165114.html"},"modified":"2025-01-15T15:16:26","modified_gmt":"2025-01-15T07:16:26","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e5%ad%97%e7%ac%a6%e9%87%8d%e5%a4%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1165114.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u5b57\u7b26\u91cd\u590d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25205533\/17a9003f-5b0f-4d13-884f-2701f2ee649e.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u5b57\u7b26\u91cd\u590d\" \/><\/p>\n<p><p> <strong>Python\u83b7\u53d6\u5b57\u7b26\u91cd\u590d\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u3001\u96c6\u5408\u548c\u5185\u7f6e\u51fd\u6570\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u5b57\u5178\u6765\u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u662f\u6700\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u5c06\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u6b21\u6570\u8bb0\u5f55\u5728\u5b57\u5178\u4e2d\uff0c\u6700\u7ec8\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u91cd\u590d\u7684\u5b57\u7b26\u53ca\u5176\u6b21\u6570\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p><strong>\u6b65\u9aa4\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5b57\u5178<\/strong>\uff0c\u7528\u4e8e\u5b58\u50a8\u5b57\u7b26\u53ca\u5176\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/li>\n<li><strong>\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26<\/strong>\uff0c\u68c0\u67e5\u8be5\u5b57\u7b26\u662f\u5426\u5728\u5b57\u5178\u4e2d\uff1a\n<ul>\n<li>\u5982\u679c\u5b58\u5728\uff0c\u5219\u5c06\u5176\u5bf9\u5e94\u7684\u503c\u52a01\u3002<\/li>\n<li>\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff0c\u5e76\u5c06\u5bf9\u5e94\u7684\u503c\u8bbe\u7f6e\u4e3a1\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u904d\u5386\u5b57\u5178<\/strong>\uff0c\u7b5b\u9009\u51fa\u51fa\u73b0\u6b21\u6570\u5927\u4e8e1\u7684\u5b57\u7b26\uff0c\u8fd9\u4e9b\u5373\u4e3a\u91cd\u590d\u7684\u5b57\u7b26\u3002<\/li>\n<\/ol>\n<p><p><strong>\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_repeated_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>    repeated_chars = {char: count for char, count in char_count.items() if count &gt; 1}<\/p>\n<p>    return repeated_chars<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>string = &quot;hello world&quot;<\/p>\n<p>repeated_chars = find_repeated_characters(string)<\/p>\n<p>print(repeated_chars)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#39;l&#39;: 3, &#39;o&#39;: 2}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5b57\u5178\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u91cd\u590d\u5b57\u7b26\u53ca\u5176\u51fa\u73b0\u6b21\u6570\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u63a2\u8ba8Python\u4e2d\u83b7\u53d6\u5b57\u7b26\u91cd\u590d\u7684\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u96c6\u5408<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u53ef\u4ee5\u6709\u6548\u5730\u8ddf\u8e2a\u5df2\u7ecf\u9047\u5230\u7684\u5b57\u7b26\uff0c\u5e76\u901a\u8fc7\u96c6\u5408\u7684\u6027\u8d28\u6765\u5224\u65ad\u5b57\u7b26\u662f\u5426\u91cd\u590d\u3002\u96c6\u5408\u7684\u7279\u70b9\u662f\u5176\u5143\u7d20\u4e0d\u5141\u8bb8\u91cd\u590d\uff0c\u56e0\u6b64\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u662f\u5426\u5df2\u7ecf\u51fa\u73b0\u8fc7\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def find_repeated_characters(s):<\/p>\n<p>    seen = set()<\/p>\n<p>    repeated = set()<\/p>\n<p>    for char in s:<\/p>\n<p>        if char in seen:<\/p>\n<p>            repeated.add(char)<\/p>\n<p>        else:<\/p>\n<p>            seen.add(char)<\/p>\n<p>    return repeated<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>string = &quot;hello world&quot;<\/p>\n<p>repeated_chars = find_repeated_characters(string)<\/p>\n<p>print(repeated_chars)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#39;l&#39;, &#39;o&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e24\u4e2a\u96c6\u5408\uff1a<code>seen<\/code> \u7528\u4e8e\u8bb0\u5f55\u5df2\u7ecf\u9047\u5230\u7684\u5b57\u7b26\uff0c<code>repeated<\/code> \u7528\u4e8e\u8bb0\u5f55\u91cd\u590d\u51fa\u73b0\u7684\u5b57\u7b26\u3002\u904d\u5386\u5b57\u7b26\u4e32\u65f6\uff0c\u5982\u679c\u5b57\u7b26\u5df2\u7ecf\u5728 <code>seen<\/code> \u4e2d\u51fa\u73b0\u8fc7\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230 <code>repeated<\/code> \u4e2d\uff0c\u5426\u5219\u6dfb\u52a0\u5230 <code>seen<\/code> \u4e2d\u3002\u6700\u7ec8\u8fd4\u56de <code>repeated<\/code> \u96c6\u5408\u5373\u53ef\u5f97\u5230\u6240\u6709\u91cd\u590d\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528Counter\u7c7b<\/h2>\n<\/p>\n<p><p>Python\u7684 <code>collections<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86 <code>Counter<\/code> \u7c7b\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u3002\u4f7f\u7528 <code>Counter<\/code> \u7c7b\uff0c\u53ef\u4ee5\u7b80\u6d01\u5730\u5b9e\u73b0\u5b57\u7b26\u91cd\u590d\u7edf\u8ba1\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def find_repeated_characters(s):<\/p>\n<p>    counter = Counter(s)<\/p>\n<p>    repeated_chars = {char: count for char, count in counter.items() if count &gt; 1}<\/p>\n<p>    return repeated_chars<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>string = &quot;hello world&quot;<\/p>\n<p>repeated_chars = find_repeated_characters(string)<\/p>\n<p>print(repeated_chars)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#39;l&#39;: 3, &#39;o&#39;: 2}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>Counter<\/code> \u7c7b\u53ef\u4ee5\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u5178\uff0c\u952e\u4e3a\u5b57\u7b26\uff0c\u503c\u4e3a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u3002\u7136\u540e\uff0c\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u7b5b\u9009\u51fa\u51fa\u73b0\u6b21\u6570\u5927\u4e8e1\u7684\u5b57\u7b26\uff0c\u5373\u53ef\u5f97\u5230\u91cd\u590d\u5b57\u7b26\u53ca\u5176\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08regex\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\u548c\u641c\u7d22\u3002\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u91cd\u590d\u7684\u7edf\u8ba1\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from collections import Counter<\/p>\n<p>def find_repeated_characters(s):<\/p>\n<p>    pattern = re.compile(r&#39;(\\w)\\1+&#39;)<\/p>\n<p>    matches = pattern.findall(s)<\/p>\n<p>    counter = Counter(matches)<\/p>\n<p>    repeated_chars = {char: count for char, count in counter.items() if count &gt; 1}<\/p>\n<p>    return repeated_chars<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>string = &quot;hello world&quot;<\/p>\n<p>repeated_chars = find_repeated_characters(string)<\/p>\n<p>print(repeated_chars)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f <code>(\\w)\\1+<\/code> \u6765\u5339\u914d\u8fde\u7eed\u91cd\u590d\u7684\u5b57\u7b26\u3002\u7136\u540e\uff0c\u5c06\u5339\u914d\u5230\u7684\u5b57\u7b26\u653e\u5165 <code>Counter<\/code> \u4e2d\u8fdb\u884c\u8ba1\u6570\u3002\u6700\u540e\uff0c\u7b5b\u9009\u51fa\u51fa\u73b0\u6b21\u6570\u5927\u4e8e1\u7684\u5b57\u7b26\u3002\u4e0d\u8fc7\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ea\u9002\u7528\u4e8e\u8fde\u7eed\u91cd\u590d\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u4e0e\u5217\u8868\u89e3\u6790<\/h2>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u51fd\u6570\u548c\u5217\u8868\u89e3\u6790\u529f\u80fd\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5b57\u7b26\u91cd\u590d\u7edf\u8ba1\u3002\u901a\u8fc7\u7ec4\u5408\u4f7f\u7528 <code>str.count()<\/code> \u51fd\u6570\u548c\u5217\u8868\u89e3\u6790\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7b80\u6d01\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def find_repeated_characters(s):<\/p>\n<p>    repeated_chars = {char: s.count(char) for char in set(s) if s.count(char) &gt; 1}<\/p>\n<p>    return repeated_chars<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>string = &quot;hello world&quot;<\/p>\n<p>repeated_chars = find_repeated_characters(string)<\/p>\n<p>print(repeated_chars)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{&#39;l&#39;: 3, &#39;o&#39;: 2}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u4ee5\u53bb\u9664\u91cd\u590d\u7684\u5b57\u7b26\u3002\u7136\u540e\uff0c\u901a\u8fc7\u5217\u8868\u89e3\u6790\u548c <code>str.count()<\/code> \u51fd\u6570\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u51fa\u73b0\u6b21\u6570\u3002\u5982\u679c\u5b57\u7b26\u51fa\u73b0\u6b21\u6570\u5927\u4e8e1\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u7ed3\u679c\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u6027\u80fd\u6bd4\u8f83\u4e0e\u9009\u62e9<\/h2>\n<\/p>\n<p><p>\u4e0d\u540c\u65b9\u6cd5\u5728\u6027\u80fd\u4e0a\u6709\u6240\u5dee\u5f02\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u9700\u8981\u8003\u8651\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u548c\u5177\u4f53\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u6027\u80fd\u6bd4\u8f83\uff1a<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5b57\u5178\u65b9\u6cd5<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\uff0c\u9002\u5408\u5904\u7406\u8f83\u957f\u5b57\u7b26\u4e32\uff0c\u6027\u80fd\u8f83\u597d\u3002<\/li>\n<li><strong>\u96c6\u5408\u65b9\u6cd5<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\uff0c\u9002\u5408\u5feb\u901f\u5224\u65ad\u5b57\u7b26\u662f\u5426\u91cd\u590d\uff0c\u4ee3\u7801\u7b80\u6d01\u3002<\/li>\n<li><strong>Counter\u7c7b<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\uff0c\u4f7f\u7528\u65b9\u4fbf\uff0c\u4ee3\u7801\u7b80\u6d01\u3002<\/li>\n<li><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u6a21\u5f0f\u5339\u914d\uff0c\u4f46\u4e0d\u9002\u5408\u957f\u5b57\u7b26\u4e32\u3002<\/li>\n<li><strong>\u5185\u7f6e\u51fd\u6570\u4e0e\u5217\u8868\u89e3\u6790<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\uff0c\u9002\u5408\u5904\u7406\u77ed\u5b57\u7b26\u4e32\uff0c\u4ee3\u7801\u7b80\u6d01\uff0c\u4f46\u6027\u80fd\u8f83\u5dee\u3002<\/li>\n<\/ol>\n<p><h3>\u9009\u62e9\u5efa\u8bae\uff1a<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5904\u7406\u957f\u5b57\u7b26\u4e32<\/strong>\uff1a\u63a8\u8350\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5\u6216 <code>Counter<\/code> \u7c7b\u3002<\/li>\n<li><strong>\u5feb\u901f\u5224\u65ad\u5b57\u7b26\u91cd\u590d<\/strong>\uff1a\u63a8\u8350\u4f7f\u7528\u96c6\u5408\u65b9\u6cd5\u3002<\/li>\n<li><strong>\u590d\u6742\u6a21\u5f0f\u5339\u914d<\/strong>\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f46\u9700\u6ce8\u610f\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<li><strong>\u5904\u7406\u77ed\u5b57\u7b26\u4e32<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u4e0e\u5217\u8868\u89e3\u6790\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<p><h2>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u5b57\u7b26\u91cd\u590d\u7edf\u8ba1\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u6587\u672c\u5206\u6790\u3001\u6570\u636e\u6e05\u6d17\u3001\u5bc6\u7801\u5b66\u7b49\u9886\u57df\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u4f53\u5e94\u7528\u573a\u666f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>\u6587\u672c\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u672c\u5206\u6790\u4e2d\uff0c\u5b57\u7b26\u91cd\u590d\u7edf\u8ba1\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u6587\u672c\u7684\u590d\u6742\u5ea6\u3001\u68c0\u6d4b\u6587\u672c\u4e2d\u7684\u566a\u97f3\u5b57\u7b26\u7b49\u3002\u901a\u8fc7\u7edf\u8ba1\u5b57\u7b26\u91cd\u590d\u60c5\u51b5\uff0c\u53ef\u4ee5\u5bf9\u6587\u672c\u8fdb\u884c\u9884\u5904\u7406\uff0c\u63d0\u5347\u5206\u6790\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u6570\u636e\u6e05\u6d17<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u6e05\u6d17\u8fc7\u7a0b\u4e2d\uff0c\u5b57\u7b26\u91cd\u590d\u7edf\u8ba1\u53ef\u4ee5\u7528\u4e8e\u8bc6\u522b\u6570\u636e\u4e2d\u7684\u5f02\u5e38\u503c\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u7edf\u8ba1\u5b57\u7b26\u91cd\u590d\u60c5\u51b5\uff0c\u8bc6\u522b\u5e76\u6e05\u7406\u65e0\u6548\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u5bc6\u7801\u5b66<\/h3>\n<\/p>\n<p><p>\u5728\u5bc6\u7801\u5b66\u4e2d\uff0c\u5b57\u7b26\u91cd\u590d\u7edf\u8ba1\u53ef\u4ee5\u7528\u4e8e\u5206\u6790\u5bc6\u7801\u7684\u5f3a\u5ea6\u3002\u901a\u8fc7\u7edf\u8ba1\u5bc6\u7801\u4e2d\u5b57\u7b26\u7684\u91cd\u590d\u60c5\u51b5\uff0c\u53ef\u4ee5\u8bc4\u4f30\u5bc6\u7801\u7684\u590d\u6742\u5ea6\uff0c\u8fdb\u800c\u5224\u65ad\u5176\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def analyze_password_strength(password):<\/p>\n<p>    repeated_chars = find_repeated_characters(password)<\/p>\n<p>    strength = &quot;Weak&quot; if repeated_chars else &quot;Strong&quot;<\/p>\n<p>    return strength<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>password = &quot;password123&quot;<\/p>\n<p>strength = analyze_password_strength(password)<\/p>\n<p>print(f&quot;Password strength: {strength}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">Password strength: Weak<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u7edf\u8ba1\u5bc6\u7801\u4e2d\u5b57\u7b26\u7684\u91cd\u590d\u60c5\u51b5\uff0c\u5224\u65ad\u5bc6\u7801\u7684\u5f3a\u5ea6\u3002\u5982\u679c\u5bc6\u7801\u4e2d\u5b58\u5728\u91cd\u590d\u5b57\u7b26\uff0c\u5219\u5224\u5b9a\u4e3a\u5f31\u5bc6\u7801\uff0c\u5426\u5219\u4e3a\u5f3a\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u83b7\u53d6\u5b57\u7b26\u91cd\u590d\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u3001\u96c6\u5408\u3001Counter\u7c7b\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5185\u7f6e\u51fd\u6570\u4e0e\u5217\u8868\u89e3\u6790\u3002\u6bcf\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u901a\u8fc7\u6027\u80fd\u6bd4\u8f83\u4e0e\u9009\u62e9\u5efa\u8bae\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u672c\u6587\u8fd8\u4ecb\u7ecd\u4e86\u5b57\u7b26\u91cd\u590d\u7edf\u8ba1\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u4e00\u4e9b\u5177\u4f53\u573a\u666f\uff0c\u5c55\u793a\u4e86\u5176\u5e7f\u6cdb\u7684\u5e94\u7528\u4ef7\u503c\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u8bfb\u8005\u5728\u5904\u7406\u5b57\u7b26\u91cd\u590d\u7edf\u8ba1\u95ee\u9898\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u91cd\u590d\u6b21\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6216<code>collections.Counter<\/code>\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u91cd\u590d\u6b21\u6570\u3002<code>Counter<\/code>\u63d0\u4f9b\u4e86\u7b80\u5355\u800c\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\ninput_string = &quot;hello&quot;\nchar_count = Counter(input_string)\nprint(char_count)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u6bcf\u4e2a\u5b57\u7b26\u53ca\u5176\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u5982<code>{&#39;h&#39;: 1, &#39;e&#39;: 1, &#39;l&#39;: 2, &#39;o&#39;: 1}<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\u4ee5\u83b7\u53d6\u91cd\u590d\u5b57\u7b26\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5ffd\u7565\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>replace()<\/code>\u65b9\u6cd5\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u6e05\u7406\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\n\ninput_string = &quot;hello world!&quot;\ncleaned_string = re.sub(r&#39;\\W+&#39;, &#39;&#39;, input_string)  # \u79fb\u9664\u975e\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\nchar_count = Counter(cleaned_string)\nprint(char_count)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u53ea\u8ba1\u7b97\u5b57\u6bcd\u548c\u6570\u5b57\u7684\u91cd\u590d\u6b21\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u627e\u5230\u5b57\u7b26\u91cd\u590d\u5417\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>Counter<\/code>\u5916\uff0c\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u4e5f\u662f\u4e00\u4e2a\u53ef\u884c\u7684\u9009\u62e9\u3002\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\uff0c\u904d\u5386\u5b57\u7b26\u4e32\u5e76\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u8ba1\u6570\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">input_string = &quot;hello&quot;\nchar_count = {}\n\nfor char in input_string:\n    if char in char_count:\n        char_count[char] += 1\n    else:\n        char_count[char] = 1\n\nprint(char_count)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9a\u5236\u5b57\u7b26\u8ba1\u6570\u7684\u903b\u8f91\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u83b7\u53d6\u5b57\u7b26\u91cd\u590d\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u3001\u96c6\u5408\u548c\u5185\u7f6e\u51fd\u6570\u7b49\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528\u5b57\u5178\u6765\u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u662f\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1165117,"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\/1165114"}],"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=1165114"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165114\/revisions"}],"predecessor-version":[{"id":1165119,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165114\/revisions\/1165119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1165117"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1165114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1165114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1165114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}