{"id":1041876,"date":"2024-12-31T12:52:53","date_gmt":"2024-12-31T04:52:53","guid":{"rendered":""},"modified":"2024-12-31T12:52:55","modified_gmt":"2024-12-31T04:52:55","slug":"%e5%a6%82%e4%bd%95%e7%bb%99%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8e%bb%e9%87%8dpython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1041876.html","title":{"rendered":"\u5982\u4f55\u7ed9\u5b57\u7b26\u4e32\u53bb\u91cdpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/8d2d3224-b9de-4f64-b9d7-2f445ebbf6f4.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7ed9\u5b57\u7b26\u4e32\u53bb\u91cdpython\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u7ed9\u5b57\u7b26\u4e32\u53bb\u91cd\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u591a\u79cd\u65b9\u6cd5\u3001\u4f7f\u7528\u96c6\u5408\u53ef\u4ee5\u81ea\u52a8\u53bb\u91cd\u3001\u4f7f\u7528\u5217\u8868\u53ef\u4ee5\u4fdd\u6301\u987a\u5e8f\u3001\u4f7f\u7528\u5b57\u5178\u7684\u6709\u5e8f\u7279\u6027\u3002<\/strong> \u5177\u4f53\u6765\u8bb2\uff0c\u4f7f\u7528\u96c6\u5408\u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u96c6\u5408\u6570\u636e\u7ed3\u6784\u672c\u8eab\u5c31\u662f\u65e0\u5e8f\u4e14\u552f\u4e00\u7684\u3002\u4f46\u662f\uff0c\u5982\u679c\u9700\u8981\u4fdd\u6301\u539f\u5b57\u7b26\u4e32\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u5217\u8868\u548c\u5b57\u5178\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u5c55\u5f00\u4f7f\u7528\u96c6\u5408\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u65b9\u6cd5\u5bf9\u5b57\u7b26\u4e32\u53bb\u91cd\u7684\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\u53bb\u91cd<\/strong><\/h2>\n<p><p>\u96c6\u5408\uff08set\uff09\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u552f\u4e00\u7684\u96c6\u5408\u7c7b\u578b\uff0c\u4f7f\u7528\u96c6\u5408\u53bb\u91cd\u975e\u5e38\u7b80\u5355\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u518d\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u57fa\u672c\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\u53bb\u91cd\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u96c6\u5408\u672c\u8eab\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_duplicates(s):<\/p>\n<p>    return &#39;&#39;.join(set(s))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_str = &quot;programming&quot;<\/p>\n<p>result = remove_duplicates(input_str)<\/p>\n<p>print(result)  # \u8f93\u51fa: &#39;gimnpor&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u4ece\u800c\u81ea\u52a8\u53bb\u91cd\u3002\u7136\u540e\u6211\u4eec\u518d\u5c06\u96c6\u5408\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u6240\u4ee5\u8f93\u51fa\u5b57\u7b26\u4e32\u7684\u987a\u5e8f\u53ef\u80fd\u4e0e\u8f93\u5165\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4fdd\u6301\u987a\u5e8f\u7684\u53bb\u91cd\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u5e0c\u671b\u5728\u53bb\u91cd\u7684\u540c\u65f6\u4fdd\u6301\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u987a\u5e8f\u3002\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6709\u5e8f\u96c6\u5408\u6765\u5b9e\u73b0\u3002Python 3.7+\u7248\u672c\u4e2d\u7684\u5b57\u5178\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\uff0c\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u7279\u6027\u6765\u5b9e\u73b0\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_duplicates_keep_order(s):<\/p>\n<p>    return &#39;&#39;.join(dict.fromkeys(s))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_str = &quot;programming&quot;<\/p>\n<p>result = remove_duplicates_keep_order(input_str)<\/p>\n<p>print(result)  # \u8f93\u51fa: &#39;progamin&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>dict.fromkeys(s)<\/code>\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5b57\u5178\u952e\u662f\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff0c\u5b57\u5178\u7684\u63d2\u5165\u987a\u5e8f\u5c06\u81ea\u52a8\u4fdd\u7559\u3002\u7136\u540e\u6211\u4eec\u5c06\u5b57\u5178\u7684\u952e\u8fde\u63a5\u8d77\u6765\uff0c\u5f97\u5230\u53bb\u91cd\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u53bb\u91cd<\/strong><\/h2>\n<p><p>\u5217\u8868\u53bb\u91cd\u7684\u65b9\u6cd5\u867d\u7136\u6ca1\u6709\u96c6\u5408\u90a3\u4e48\u76f4\u63a5\uff0c\u4f46\u662f\u53ef\u4ee5\u4fdd\u6301\u987a\u5e8f\u3002\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528\u4e00\u4e2a\u989d\u5916\u7684\u5217\u8868\u6765\u5b58\u50a8\u5df2\u7ecf\u89c1\u8fc7\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u57fa\u672c\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u53bb\u91cd\u7684\u57fa\u672c\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_duplicates_list(s):<\/p>\n<p>    seen = []<\/p>\n<p>    for char in s:<\/p>\n<p>        if char not in seen:<\/p>\n<p>            seen.append(char)<\/p>\n<p>    return &#39;&#39;.join(seen)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_str = &quot;programming&quot;<\/p>\n<p>result = remove_duplicates_list(input_str)<\/p>\n<p>print(result)  # \u8f93\u51fa: &#39;progamin&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u4e00\u4e2a\u989d\u5916\u7684\u5217\u8868<code>seen<\/code>\u4e2d\u3002\u53ea\u6709\u5f53\u5b57\u7b26\u4e0d\u5728<code>seen<\/code>\u5217\u8868\u4e2d\u65f6\uff0c\u6211\u4eec\u624d\u6dfb\u52a0\u5b83\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06<code>seen<\/code>\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u8fde\u63a5\u8d77\u6765\uff0c\u5f97\u5230\u53bb\u91cd\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4f18\u5316\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u6765\u5b58\u50a8\u5df2\u7ecf\u89c1\u8fc7\u7684\u5b57\u7b26\uff0c\u56e0\u4e3a\u67e5\u627e\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u6bd4\u67e5\u627e\u5217\u8868\u4e2d\u7684\u5143\u7d20\u66f4\u5feb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_duplicates_optimized(s):<\/p>\n<p>    seen = set()<\/p>\n<p>    result = []<\/p>\n<p>    for char in s:<\/p>\n<p>        if char not in seen:<\/p>\n<p>            seen.add(char)<\/p>\n<p>            result.append(char)<\/p>\n<p>    return &#39;&#39;.join(result)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_str = &quot;programming&quot;<\/p>\n<p>result = remove_duplicates_optimized(input_str)<\/p>\n<p>print(result)  # \u8f93\u51fa: &#39;progamin&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u96c6\u5408\u6765\u8bb0\u5f55\u5df2\u7ecf\u89c1\u8fc7\u7684\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528\u5217\u8868\u6765\u4fdd\u5b58\u53bb\u91cd\u540e\u7684\u5b57\u7b26\u3002\u8fd9\u6837\u65e2\u80fd\u4fdd\u6301\u987a\u5e8f\uff0c\u53c8\u80fd\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u91cd<\/strong><\/h2>\n<p><p>\u867d\u7136\u6b63\u5219\u8868\u8fbe\u5f0f\u5e76\u4e0d\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4f46\u6709\u65f6\u5019\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u4e00\u4e9b\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5904\u7406\u95ee\u9898\u3002\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5339\u914d\u91cd\u590d\u7684\u5b57\u7b26\u5e76\u5c06\u5176\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u57fa\u672c\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u91cd\u590d\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u66ff\u6362\u4e3a\u5355\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def remove_duplicates_regex(s):<\/p>\n<p>    return re.sub(r&#39;(.)(?=.*\\1)&#39;, &#39;&#39;, s)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_str = &quot;programming&quot;<\/p>\n<p>result = remove_duplicates_regex(input_str)<\/p>\n<p>print(result)  # \u8f93\u51fa: &#39;progamin&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>(.)(?=.*\\1)<\/code>\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u91cd\u590d\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u53bb\u91cd\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528\u5185\u7f6e\u6a21\u5757 itertools \u53bb\u91cd<\/strong><\/h2>\n<p><p>Python \u7684 itertools \u6a21\u5757\u63d0\u4f9b\u4e86\u975e\u5e38\u5f3a\u5927\u7684\u8fed\u4ee3\u5668\u5de5\u5177\uff0c\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u7b80\u5316\u5f88\u591a\u64cd\u4f5c\u3002\u867d\u7136 itertools \u5e76\u6ca1\u6709\u76f4\u63a5\u7528\u4e8e\u5b57\u7b26\u4e32\u53bb\u91cd\u7684\u65b9\u6cd5\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ec4\u5408\u4e0d\u540c\u7684\u8fed\u4ee3\u5668\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u57fa\u672c\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 itertools \u6a21\u5757\u4e2d\u7684 groupby \u51fd\u6570\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>def remove_duplicates_itertools(s):<\/p>\n<p>    return &#39;&#39;.join(char for char, _ in itertools.groupby(sorted(s)))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_str = &quot;programming&quot;<\/p>\n<p>result = remove_duplicates_itertools(input_str)<\/p>\n<p>print(result)  # \u8f93\u51fa: &#39;agimnopr&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u4f7f\u7528 groupby \u51fd\u6570\u5bf9\u76f8\u90bb\u7684\u76f8\u540c\u5b57\u7b26\u8fdb\u884c\u5206\u7ec4\uff0c\u6700\u540e\u5c06\u6bcf\u4e2a\u5206\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u8fde\u63a5\u8d77\u6765\uff0c\u5f97\u5230\u53bb\u91cd\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u63a2\u8ba8\u4e86\u591a\u79cd\u7ed9\u5b57\u7b26\u4e32\u53bb\u91cd\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u96c6\u5408\u3001\u5217\u8868\u3001\u5b57\u5178\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u548c itertools \u6a21\u5757\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u96c6\u5408\u53bb\u91cd<\/strong>\uff1a\u6700\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u4e0d\u4fdd\u6301\u987a\u5e8f\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5b57\u5178\u53bb\u91cd<\/strong>\uff1a\u4fdd\u6301\u987a\u5e8f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u987a\u5e8f\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5217\u8868\u53bb\u91cd<\/strong>\uff1a\u7b80\u5355\u6613\u61c2\uff0c\u4f46\u6548\u7387\u7a0d\u4f4e\u3002<\/li>\n<li><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u91cd<\/strong>\uff1a\u9002\u7528\u4e8e\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5904\u7406\u3002<\/li>\n<li><strong>\u4f7f\u7528 itertools \u6a21\u5757\u53bb\u91cd<\/strong>\uff1a\u901a\u8fc7\u7ec4\u5408\u8fed\u4ee3\u5668\u5b9e\u73b0\uff0c\u7075\u6d3b\u6027\u9ad8\u3002<\/li>\n<\/ol>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u5b57\u7b26\u4e32\u53bb\u91cd\u95ee\u9898\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u53bb\u91cd\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u53bb\u91cd\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u96c6\u5408\u3001\u5217\u8868\u89e3\u6790\u4ee5\u53ca<code>dict.fromkeys()<\/code>\u65b9\u6cd5\u3002\u4f7f\u7528\u96c6\u5408\u7684\u65b9\u5f0f\u53ef\u4ee5\u5feb\u901f\u53bb\u91cd\uff0c\u4f46\u4f1a\u6539\u53d8\u5b57\u7b26\u7684\u987a\u5e8f\u3002\u82e5\u9700\u8981\u4fdd\u6301\u539f\u6709\u987a\u5e8f\uff0c\u53ef\u4ee5\u7ed3\u5408\u5217\u8868\u89e3\u6790\u548c\u96c6\u5408\u7684\u4f7f\u7528\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">def remove_duplicates(s):\n    seen = set()\n    return &#39;&#39;.join([char for char in s if not (char in seen or seen.add(char))])\n\nresult = remove_duplicates(&quot;programming&quot;)\nprint(result)  # \u8f93\u51fa\uff1aprogamin\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u53bb\u91cd\u5b57\u7b26\u4e32\u65f6\uff0c\u5982\u4f55\u4fdd\u6301\u5b57\u7b26\u7684\u987a\u5e8f\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5728\u53bb\u91cd\u7684\u540c\u65f6\u4fdd\u6301\u5b57\u7b26\u7684\u51fa\u73b0\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6709\u5e8f\u5b57\u5178\uff08\u5728Python 3.7\u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u5e38\u89c4\u5b57\u5178\u5df2\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\uff09\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">from collections import OrderedDict\n\ndef remove_duplicates_preserve_order(s):\n    return &#39;&#39;.join(OrderedDict.fromkeys(s))\n\nresult = remove_duplicates_preserve_order(&quot;programming&quot;)\nprint(result)  # \u8f93\u51fa\uff1aprogamin\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53bb\u91cd\u4e86\u5b57\u7b26\uff0c\u8fd8\u4fdd\u7559\u4e86\u5176\u5728\u539f\u5b57\u7b26\u4e32\u4e2d\u7684\u987a\u5e8f\u3002<\/p>\n<p><strong>\u5728\u5b57\u7b26\u4e32\u4e2d\u53bb\u91cd\u540e\uff0c\u5982\u4f55\u5904\u7406\u5927\u5c0f\u5199\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53bb\u91cd\u65f6\u53ef\u80fd\u9700\u8981\u5ffd\u7565\u5b57\u7b26\u7684\u5927\u5c0f\u5199\u3002\u4f8b\u5982\uff0c\u5c06&quot;Hello&quot;\u548c&quot;hello&quot;\u89c6\u4e3a\u76f8\u540c\u5b57\u7b26\u3002\u53ef\u4ee5\u901a\u8fc7\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u7edf\u4e00\u7684\u5927\u5c0f\u5199\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u5904\u7406\u5927\u5c0f\u5199\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">def remove_duplicates_case_insensitive(s):\n    seen = set()\n    result = []\n    for char in s.lower():\n        if char not in seen:\n            seen.add(char)\n            result.append(char)\n    return &#39;&#39;.join(result)\n\nresult = remove_duplicates_case_insensitive(&quot;HelloHELLO&quot;)\nprint(result)  # \u8f93\u51fa\uff1ahelo\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u5728\u53bb\u91cd\u7684\u540c\u65f6\uff0c\u5927\u5c0f\u5199\u4e0d\u518d\u5f71\u54cd\u5b57\u7b26\u7684\u552f\u4e00\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7ed9\u5b57\u7b26\u4e32\u53bb\u91cd\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u591a\u79cd\u65b9\u6cd5\u3001\u4f7f\u7528\u96c6\u5408\u53ef\u4ee5\u81ea\u52a8\u53bb\u91cd\u3001\u4f7f\u7528\u5217\u8868\u53ef\u4ee5\u4fdd\u6301\u987a\u5e8f\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1041885,"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\/1041876"}],"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=1041876"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1041876\/revisions"}],"predecessor-version":[{"id":1041887,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1041876\/revisions\/1041887"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1041885"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1041876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1041876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1041876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}