{"id":968825,"date":"2024-12-27T05:10:38","date_gmt":"2024-12-26T21:10:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/968825.html"},"modified":"2024-12-27T05:10:40","modified_gmt":"2024-12-26T21:10:40","slug":"%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e9%87%8d%e5%a4%8d%e5%8d%95%e8%af%8dpython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/968825.html","title":{"rendered":"\u5982\u4f55\u5220\u9664\u91cd\u590d\u5355\u8bcdpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183416\/0f56b3da-c917-44db-adb5-d1b298c523e6.webp\" alt=\"\u5982\u4f55\u5220\u9664\u91cd\u590d\u5355\u8bcdpython\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5220\u9664\u91cd\u590d\u5355\u8bcd\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528\u96c6\u5408(set)\u6570\u636e\u7ed3\u6784\u3001\u5b57\u5178(dict)\u6216\u8005\u6b63\u5219\u8868\u8fbe\u5f0f(regex)\u3002\u96c6\u5408\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u96c6\u5408\u672c\u8eab\u4e0d\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff1b\u5b57\u5178\u5219\u53ef\u4ee5\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u552f\u4e00\u6027\u6765\u53bb\u91cd\uff1b\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5904\u7406\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528\u96c6\u5408\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\u53bb\u9664\u91cd\u590d\u5355\u8bcd<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5176\u6700\u5927\u7684\u7279\u70b9\u5c31\u662f\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u3002\u5229\u7528\u8fd9\u4e00\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u91cd\u590d\u5355\u8bcd\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u96c6\u5408<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5c06\u5b57\u7b26\u4e32\u6309\u7167\u7a7a\u683c\u5206\u5272\u6210\u5355\u8bcd\u5217\u8868\uff0c\u7136\u540e\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u3002\u8fd9\u6837\uff0c\u91cd\u590d\u7684\u5355\u8bcd\u4f1a\u81ea\u52a8\u88ab\u53bb\u9664\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_duplicates_with_set(input_string):<\/p>\n<p>    words = input_string.split()<\/p>\n<p>    unique_words = set(words)<\/p>\n<p>    return &quot; &quot;.join(unique_words)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python is great and Python is dynamic&quot;<\/p>\n<p>output_string = remove_duplicates_with_set(input_string)<\/p>\n<p>print(output_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u4fdd\u6301\u539f\u6709\u987a\u5e8f<\/h4>\n<\/p>\n<p><p>\u7531\u4e8e\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u5982\u679c\u9700\u8981\u4fdd\u6301\u5355\u8bcd\u7684\u539f\u6709\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u5b9e\u73b0\u3002Python 3.7\u4e4b\u540e\uff0c\u5b57\u5178\u4e5f\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_duplicates_keep_order(input_string):<\/p>\n<p>    words = input_string.split()<\/p>\n<p>    seen = {}<\/p>\n<p>    for word in words:<\/p>\n<p>        if word not in seen:<\/p>\n<p>            seen[word] = True<\/p>\n<p>    return &quot; &quot;.join(seen.keys())<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python is great and Python is dynamic&quot;<\/p>\n<p>output_string = remove_duplicates_keep_order(input_string)<\/p>\n<p>print(output_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u53bb\u9664\u91cd\u590d\u5355\u8bcd<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u5728Python\u4e2d\u7684\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u53bb\u9664\u91cd\u590d\u5355\u8bcd\u5e76\u4fdd\u6301\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5b57\u5178\u7684\u552f\u4e00\u6027<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u952e\u5177\u6709\u552f\u4e00\u6027\uff0c\u8fd9\u53ef\u4ee5\u7528\u6765\u53bb\u9664\u91cd\u590d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_duplicates_with_dict(input_string):<\/p>\n<p>    words = input_string.split()<\/p>\n<p>    unique_words = dict.fromkeys(words)<\/p>\n<p>    return &quot; &quot;.join(unique_words)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python is great and Python is dynamic&quot;<\/p>\n<p>output_string = remove_duplicates_with_dict(input_string)<\/p>\n<p>print(output_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u9664\u91cd\u590d\u5355\u8bcd<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u6587\u672c\u5904\u7406\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5355\u8bcd<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u68c0\u6d4b\u5e76\u53bb\u9664\u91cd\u590d\u7684\u8fde\u7eed\u5355\u8bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def remove_consecutive_duplicates(input_string):<\/p>\n<p>    return re.sub(r&#39;\\b(\\w+)( \\1\\b)+&#39;, r&#39;\\1&#39;, input_string)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python is is great and and Python is dynamic&quot;<\/p>\n<p>output_string = remove_consecutive_duplicates(input_string)<\/p>\n<p>print(output_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f18\u5316\u4e0e\u6269\u5c55<\/h3>\n<\/p>\n<p><h4>4.1 \u8003\u8651\u5927\u5c0f\u5199<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c&quot;Python&quot;\u548c&quot;python&quot;\u5e94\u8be5\u88ab\u89c6\u4e3a\u76f8\u540c\u7684\u5355\u8bcd\u3002\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u7edf\u4e00\u5927\u5c0f\u5199\u6765\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_duplicates_case_insensitive(input_string):<\/p>\n<p>    words = input_string.lower().split()<\/p>\n<p>    unique_words = dict.fromkeys(words)<\/p>\n<p>    return &quot; &quot;.join(unique_words)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python is great and python is dynamic&quot;<\/p>\n<p>output_string = remove_duplicates_case_insensitive(input_string)<\/p>\n<p>print(output_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u8003\u8651\u6807\u70b9\u7b26\u53f7<\/h4>\n<\/p>\n<p><p>\u6807\u70b9\u7b26\u53f7\u53ef\u80fd\u4f1a\u5e72\u6270\u5355\u8bcd\u7684\u8bc6\u522b\uff0c\u9700\u8981\u5728\u5904\u7406\u524d\u53bb\u9664\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import string<\/p>\n<p>def remove_duplicates_ignore_punctuation(input_string):<\/p>\n<p>    translator = str.maketrans(&#39;&#39;, &#39;&#39;, string.punctuation)<\/p>\n<p>    stripped_input = input_string.translate(translator)<\/p>\n<p>    words = stripped_input.split()<\/p>\n<p>    unique_words = dict.fromkeys(words)<\/p>\n<p>    return &quot; &quot;.join(unique_words)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Python, is great! And Python is dynamic.&quot;<\/p>\n<p>output_string = remove_duplicates_ignore_punctuation(input_string)<\/p>\n<p>print(output_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6027\u80fd\u548c\u590d\u6742\u6027\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u53bb\u91cd\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u662f\u4e00\u4e2a\u8003\u8651\u56e0\u7d20\u3002\u96c6\u5408\u548c\u5b57\u5178\u5728\u53bb\u91cd\u65b9\u9762\u7684\u590d\u6742\u6027\u8f83\u4f4e\uff0c\u901a\u5e38\u4e3aO(n)\uff0c\u5176\u4e2dn\u662f\u5355\u8bcd\u6570\u3002\u8fd9\u4f7f\u5f97\u5b83\u4eec\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u662f\u7406\u60f3\u7684\u9009\u62e9\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u867d\u7136\u5f3a\u5927\uff0c\u4f46\u5728\u590d\u6742\u5ea6\u548c\u53ef\u8bfb\u6027\u4e0a\u53ef\u80fd\u4e0d\u5982\u524d\u4e24\u8005\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>6.1 \u6587\u672c\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6587\u672c\u5206\u6790\u4e2d\uff0c\u53bb\u9664\u91cd\u590d\u5355\u8bcd\u6709\u52a9\u4e8e\u63d0\u9ad8\u6587\u672c\u7684\u53ef\u8bfb\u6027\u548c\u5206\u6790\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u4efb\u52a1\u4e2d\uff0c\u9884\u5904\u7406\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><h4>6.2 \u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4ece\u7f51\u7edc\u6293\u53d6\u7684\u6570\u636e\u6216\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\uff0c\u53bb\u9664\u91cd\u590d\u5355\u8bcd\u53ef\u4ee5\u5e2e\u52a9\u63d0\u9ad8\u6570\u636e\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u53bb\u9664\u91cd\u590d\u5355\u8bcd\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u7279\u70b9\u3002<\/strong>\u65e0\u8bba\u662f\u4f7f\u7528\u96c6\u5408\u3001\u5b57\u5178\u8fd8\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u7406\u89e3\u6bcf\u79cd\u65b9\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u8df5\u4e2d\u66f4\u9ad8\u6548\u5730\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6709\u6548\u5730\u8bc6\u522b\u548c\u5220\u9664\u6587\u672c\u4e2d\u7684\u91cd\u590d\u5355\u8bcd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u8bc6\u522b\u548c\u5220\u9664\u91cd\u590d\u5355\u8bcd\u3002\u9996\u5148\uff0c\u5c06\u6587\u672c\u5206\u5272\u6210\u5355\u8bcd\u5217\u8868\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u96c6\u5408\u4ee5\u79fb\u9664\u91cd\u590d\u9879\u3002\u63a5\u7740\uff0c\u53ef\u4ee5\u5c06\u96c6\u5408\u8f6c\u56de\u5217\u8868\u5e76\u6839\u636e\u539f\u59cb\u987a\u5e8f\u6392\u5e8f\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>dict.fromkeys()<\/code>\u65b9\u6cd5\u4e5f\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u5b83\u53ef\u4ee5\u4fdd\u6301\u5355\u8bcd\u7684\u987a\u5e8f\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5927\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u9ad8\u6548\u5220\u9664\u91cd\u590d\u5355\u8bcd\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5927\u6587\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528\u96c6\u5408\u6765\u5b58\u50a8\u552f\u4e00\u5355\u8bcd\u3002\u9010\u884c\u5904\u7406\u53ef\u4ee5\u6709\u6548\u5730\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff0c\u540c\u65f6\u4f7f\u7528\u96c6\u5408\u7684\u7279\u6027\u786e\u4fdd\u6bcf\u4e2a\u5355\u8bcd\u53ea\u88ab\u5b58\u50a8\u4e00\u6b21\u3002\u5904\u7406\u5b8c\u6210\u540e\uff0c\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u5e76\u6839\u636e\u9700\u8981\u8f93\u51fa\u6216\u4fdd\u5b58\u7ed3\u679c\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u73b0\u6210\u7684Python\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5220\u9664\u6587\u672c\u4e2d\u7684\u91cd\u590d\u5355\u8bcd\uff1f<\/strong><br \/>\u786e\u5b9e\u5b58\u5728\u4e00\u4e9bPython\u5e93\uff0c\u5982<code>nltk<\/code>\u548c<code>pandas<\/code>\uff0c\u53ef\u4ee5\u7b80\u5316\u6587\u672c\u5904\u7406\u4efb\u52a1\u3002\u4f7f\u7528<code>nltk<\/code>\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u81ea\u7136\u8bed\u8a00\u4e2d\u7684\u91cd\u590d\u9879\uff0c\u800c<code>pandas<\/code>\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u5c06\u6587\u672c\u6570\u636e\u8f6c\u6362\u4e3aDataFrame\u5e76\u53bb\u9664\u91cd\u590d\u5355\u8bcd\u3002\u8fd9\u4e9b\u5e93\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u53bb\u91cd\u529f\u80fd\uff0c\u8fd8\u80fd\u8fdb\u884c\u66f4\u591a\u6587\u672c\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5220\u9664\u91cd\u590d\u5355\u8bcd\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528\u96c6\u5408(set)\u6570\u636e\u7ed3\u6784\u3001\u5b57\u5178(dict)\u6216\u8005\u6b63\u5219\u8868\u8fbe [&hellip;]","protected":false},"author":3,"featured_media":968836,"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\/968825"}],"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=968825"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968825\/revisions"}],"predecessor-version":[{"id":968838,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968825\/revisions\/968838"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/968836"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=968825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=968825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=968825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}