{"id":1129756,"date":"2025-01-08T20:32:23","date_gmt":"2025-01-08T12:32:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1129756.html"},"modified":"2025-01-08T20:32:26","modified_gmt":"2025-01-08T12:32:26","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e7%9b%b8%e5%90%8c%e7%9a%84%e5%ad%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1129756.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u76f8\u540c\u7684\u5b57\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100053\/6a4ec93c-6753-45cf-8c06-38d595e47567.webp\" alt=\"python\u5982\u4f55\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u76f8\u540c\u7684\u5b57\u7b26\" \/><\/p>\n<p><p> <strong>Python\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u76f8\u540c\u5b57\u7b26\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u96c6\u5408\u3001\u5217\u8868\u89e3\u6790\u3001\u5b57\u5178\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\u7684\u65b9\u5f0f\u6700\u4e3a\u7b80\u6d01\u9ad8\u6548\uff0c\u56e0\u4e3a\u96c6\u5408\u4f1a\u81ea\u52a8\u53bb\u91cd\u3002\u4ee5\u4e0b\u662f\u5bf9\u96c6\u5408\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p>\u96c6\u5408\uff08Set\uff09\u662fPython\u4e2d\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4e0d\u5141\u8bb8\u6709\u91cd\u590d\u5143\u7d20\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u96c6\u5408\u7684\u8fd9\u4e00\u7279\u6027\u6765\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u76f8\u540c\u7684\u5b57\u7b26\u3002\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u96c6\u5408\u540e\uff0c\u518d\u5c06\u96c6\u5408\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\u5373\u53ef\u5b9e\u73b0\u53bb\u91cd\u3002\u7531\u4e8e\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u6240\u4ee5\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u6253\u4e71\u5b57\u7b26\u4e32\u7684\u539f\u6709\u987a\u5e8f\u3002\u5982\u679c\u9700\u8981\u4fdd\u6301\u539f\u6709\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u6709\u5e8f\u96c6\u5408\u6216\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\uff08Set\uff09<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u975e\u5e38\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5728\u4fdd\u6301\u552f\u4e00\u6027\u7684\u540c\u65f6\u8fdb\u884c\u5feb\u901f\u67e5\u627e\u3002\u4e0b\u9762\u662f\u4f7f\u7528\u96c6\u5408\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u76f8\u540c\u5b57\u7b26\u7684\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_duplicates(input_str):<\/p>\n<p>    return &#39;&#39;.join(set(input_str))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_str = &quot;aabbccddeeff&quot;<\/p>\n<p>result = remove_duplicates(input_str)<\/p>\n<p>print(result)  # \u7ed3\u679c\u53ef\u80fd\u662f &#39;abcdef&#39; \u6216\u5176\u4ed6\u987a\u5e8f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u4f46\u5b83\u5e76\u4e0d\u80fd\u4fdd\u6301\u5b57\u7b26\u7684\u539f\u59cb\u987a\u5e8f\u3002\u5982\u679c\u9700\u8981\u4fdd\u6301\u539f\u6709\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u6709\u5e8f\u96c6\u5408\uff08OrderedDict\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6709\u5e8f\u96c6\u5408\uff08OrderedDict\uff09<\/h3>\n<\/p>\n<p><p><code>OrderedDict<\/code> \u662f Python \u7684 <code>collections<\/code> \u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u5b83\u53ef\u4ee5\u5728\u4fdd\u7559\u5143\u7d20\u63d2\u5165\u987a\u5e8f\u7684\u540c\u65f6\u5220\u9664\u91cd\u590d\u5143\u7d20\u3002\u4e0b\u9762\u662f\u5177\u4f53\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import OrderedDict<\/p>\n<p>def remove_duplicates(input_str):<\/p>\n<p>    return &#39;&#39;.join(OrderedDict.fromkeys(input_str))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_str = &quot;aabbccddeeff&quot;<\/p>\n<p>result = remove_duplicates(input_str)<\/p>\n<p>print(result)  # \u8f93\u51fa &#39;abcdef&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53bb\u9664\u4e86\u91cd\u590d\u5b57\u7b26\uff0c\u8fd8\u4fdd\u7559\u4e86\u5b57\u7b26\u7684\u539f\u59cb\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u904d\u5386\u5b57\u7b26\u4e32\u7684\u540c\u65f6\u68c0\u67e5\u5b57\u7b26\u662f\u5426\u5df2\u5728\u7ed3\u679c\u5217\u8868\u4e2d\u5b58\u5728\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_duplicates(input_str):<\/p>\n<p>    seen = []<\/p>\n<p>    return &#39;&#39;.join([c for c in input_str if not (c in seen or seen.append(c))])<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_str = &quot;aabbccddeeff&quot;<\/p>\n<p>result = remove_duplicates(input_str)<\/p>\n<p>print(result)  # \u8f93\u51fa &#39;abcdef&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u53ef\u4ee5\u4fdd\u7559\u5b57\u7b26\u7684\u539f\u59cb\u987a\u5e8f\uff0c\u4f46\u5b9e\u73b0\u8d77\u6765\u7a0d\u663e\u590d\u6742\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\uff08Dictionary\uff09\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u8bb0\u5f55\u5b57\u7b26\u662f\u5426\u5df2\u51fa\u73b0\u8fc7\u3002\u4e0b\u9762\u662f\u5177\u4f53\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_duplicates(input_str):<\/p>\n<p>    seen = {}<\/p>\n<p>    result = []<\/p>\n<p>    for char in input_str:<\/p>\n<p>        if char not in seen:<\/p>\n<p>            seen[char] = True<\/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;aabbccddeeff&quot;<\/p>\n<p>result = remove_duplicates(input_str)<\/p>\n<p>print(result)  # \u8f93\u51fa &#39;abcdef&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0e\u6709\u5e8f\u96c6\u5408\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u4fdd\u7559\u5b57\u7b26\u7684\u539f\u59cb\u987a\u5e8f\uff0c\u5e76\u4e14\u4ee3\u7801\u4e5f\u6bd4\u8f83\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5339\u914d\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u3002\u867d\u7136\u4e0d\u5e38\u7528\u4e8e\u8fd9\u79cd\u7b80\u5355\u7684\u53bb\u91cd\u4efb\u52a1\uff0c\u4f46\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u5177\u4f53\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def remove_duplicates(input_str):<\/p>\n<p>    # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u91cd<\/p>\n<p>    return re.sub(r&#39;(.)(?=.*\\1)&#39;, &#39;&#39;, input_str)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_str = &quot;aabbccddeeff&quot;<\/p>\n<p>result = remove_duplicates(input_str)<\/p>\n<p>print(result)  # \u8f93\u51fa &#39;abcdef&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f <code>(.)(?=.*\\1)<\/code> \u5339\u914d\u6240\u6709\u91cd\u590d\u7684\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u76f8\u540c\u5b57\u7b26\u3002<strong>\u4f7f\u7528\u96c6\u5408\u65b9\u6cd5\u6700\u4e3a\u7b80\u6d01\u9ad8\u6548\uff0c\u4f46\u4f1a\u6253\u4e71\u5b57\u7b26\u987a\u5e8f\uff1b<\/strong> <strong>\u4f7f\u7528\u6709\u5e8f\u96c6\u5408\u6216\u5b57\u5178\u53ef\u4ee5\u4fdd\u7559\u5b57\u7b26\u7684\u539f\u59cb\u987a\u5e8f\uff1b<\/strong> <strong>\u5217\u8868\u89e3\u6790\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\uff0c\u4f46\u5b9e\u73b0\u8d77\u6765\u7a0d\u663e\u590d\u6742\u3002<\/strong> \u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u5b9e\u73b0\u6700\u4f73\u6027\u80fd\u548c\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u91cd\u590d\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u91cd\u590d\u5b57\u7b26\u3002\u96c6\u5408\u4f1a\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20\u3002\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u518d\u5c06\u5176\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">input_string = &quot;hello world&quot;\noutput_string = &#39;&#39;.join(set(input_string))\nprint(output_string)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u6539\u53d8\u5b57\u7b26\u7684\u987a\u5e8f\uff0c\u5982\u679c\u9700\u8981\u4fdd\u6301\u539f\u59cb\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u7ed3\u5408\u96c6\u5408\u6765\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">input_string = &quot;hello world&quot;\noutput_string = &#39;&#39;.join(dict.fromkeys(input_string))\nprint(output_string)\n<\/code><\/pre>\n<p><strong>\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u91cd\u590d\u5b57\u7b26\u7684\u540c\u65f6\u4fdd\u7559\u987a\u5e8f\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8981\u5728\u4fdd\u7559\u5b57\u7b26\u987a\u5e8f\u7684\u540c\u65f6\u5220\u9664\u91cd\u590d\u5b57\u7b26\uff0c\u4f7f\u7528<code>dict.fromkeys()<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u6548\u7684\u9009\u62e9\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u800c\u5b57\u5178\u5728Python\u4e2d\u662f\u6709\u5e8f\u7684\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">input_string = &quot;programming&quot;\noutput_string = &#39;&#39;.join(dict.fromkeys(input_string))\nprint(output_string)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u8f93\u51fa\u7684\u5b57\u7b26\u4e32\u4e0d\u4ec5\u6ca1\u6709\u91cd\u590d\u5b57\u7b26\uff0c\u800c\u4e14\u5b57\u7b26\u7684\u987a\u5e8f\u4e0e\u539f\u5b57\u7b26\u4e32\u4e00\u81f4\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u6ca1\u6709\u5185\u7f6e\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u5b57\u7b26\u4e32\u7684\u91cd\u590d\u5b57\u7b26\uff1f<\/strong><br \/>Python\u6807\u51c6\u5e93\u5e76\u6ca1\u6709\u4e13\u95e8\u7684\u5185\u7f6e\u51fd\u6570\u6765\u76f4\u63a5\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u91cd\u590d\u5b57\u7b26\u3002\u4e0d\u8fc7\uff0c\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">def remove_duplicates(input_string):\n    return &#39;&#39;.join(dict.fromkeys(input_string))\n\nresult = remove_duplicates(&quot;banana&quot;)\nprint(result)  # \u8f93\u51fa: &quot;ban&quot;\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u5229\u7528\u5b57\u5178\u7684\u7279\u6027\u6709\u6548\u5730\u53bb\u9664\u4e86\u91cd\u590d\u5b57\u7b26\uff0c\u540c\u65f6\u4fdd\u7559\u4e86\u5b57\u7b26\u7684\u987a\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u76f8\u540c\u5b57\u7b26\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u96c6\u5408\u3001\u5217\u8868\u89e3\u6790\u3001\u5b57\u5178\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\u7684\u65b9\u5f0f\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1129765,"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\/1129756"}],"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=1129756"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129756\/revisions"}],"predecessor-version":[{"id":1129767,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129756\/revisions\/1129767"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1129765"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1129756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1129756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1129756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}