{"id":979762,"date":"2024-12-27T06:49:06","date_gmt":"2024-12-26T22:49:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/979762.html"},"modified":"2024-12-27T06:49:09","modified_gmt":"2024-12-26T22:49:09","slug":"python%e6%9e%84%e5%a6%82%e4%bd%95%e5%8e%bb%e9%99%a4%e9%87%8d%e5%a4%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/979762.html","title":{"rendered":"python\u6784\u5982\u4f55\u53bb\u9664\u91cd\u590d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205443\/c385526f-e8d8-4fe0-b46a-863e657c72ef.webp\" alt=\"python\u6784\u5982\u4f55\u53bb\u9664\u91cd\u590d\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53bb\u9664\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5b57\u5178\uff08dict\uff09\u3001\u4ee5\u53caPandas\u5e93\u7b49\u3002\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u96c6\u5408\u672c\u8eab\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u3001Python\u5b57\u5178\u7684fromkeys()\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u5feb\u901f\u53bb\u91cd<\/strong>\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u53bb\u9664\u91cd\u590d\u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\uff0c\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u518d\u8f6c\u6362\u56de\u5217\u8868\u5373\u53ef\u53bb\u9664\u91cd\u590d\u9879\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\uff08SET\uff09\u53bb\u9664\u91cd\u590d<\/p>\n<\/p>\n<p><p>\u96c6\u5408\uff08set\uff09\u662fPython\u5185\u7f6e\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u5177\u6709\u53bb\u91cd\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u518d\u8f6c\u56de\u5217\u8868<\/strong><\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u662f\u6700\u5feb\u901f\u4e14\u6700\u7b80\u5355\u7684\u53bb\u9664\u91cd\u590d\u7684\u65b9\u6cd5\u3002\u56e0\u4e3a\u96c6\u5408\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\uff0c\u6240\u6709\u91cd\u590d\u9879\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4f1a\u81ea\u52a8\u88ab\u79fb\u9664\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_list = list(set(original_list))<\/p>\n<p>print(unique_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u987a\u5e8f\u95ee\u9898<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\u53bb\u91cd\u65f6\uff0c\u4f1a\u6253\u4e71\u539f\u6709\u6570\u636e\u7684\u987a\u5e8f\u3002\u5982\u679c\u9700\u8981\u4fdd\u6301\u539f\u987a\u5e8f\uff0c\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\uff08DICT\uff09\u53bb\u9664\u91cd\u590d<\/p>\n<\/p>\n<p><p>\u5728Python 3.7\u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\uff0c\u56e0\u6b64\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\u8fdb\u884c\u53bb\u91cd\u64cd\u4f5c\u800c\u4e0d\u6539\u53d8\u5143\u7d20\u987a\u5e8f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5229\u7528\u5b57\u5178\u7684fromkeys()\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u5178\u7684fromkeys()\u65b9\u6cd5\u53ef\u4ee5\u5feb\u901f\u53bb\u9664\u91cd\u590d\u5e76\u4fdd\u6301\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_list = list(dict.fromkeys(original_list))<\/p>\n<p>print(unique_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53bb\u9664\u91cd\u590d<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u7684\u4e00\u79cd\u7b80\u6d01\u4e14\u5f3a\u5927\u7684\u8bed\u6cd5\u7ed3\u6784\uff0c\u53ef\u4ee5\u5728\u53bb\u91cd\u65f6\u4fdd\u6301\u987a\u5e8f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u6765\u53bb\u9664\u91cd\u590d\u9879\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>seen = set()<\/p>\n<p>unique_list = [x for x in original_list if not (x in seen or seen.add(x))]<\/p>\n<p>print(unique_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a\u8fd9\u91cc\u4f7f\u7528\u4e86\u4e00\u4e2a\u8f85\u52a9\u96c6\u5408seen\u6765\u8bb0\u5f55\u5df2\u7ecf\u51fa\u73b0\u8fc7\u7684\u5143\u7d20\uff0c\u53ea\u6709\u5f53\u5143\u7d20\u4e0d\u5728seen\u4e2d\u65f6\uff0c\u624d\u5c06\u5176\u6dfb\u52a0\u5230unique_list\u4e2d\uff0c\u540c\u65f6\u5c06\u5176\u52a0\u5165seen\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528Pandas\u5e93\u53bb\u9664\u91cd\u590d<\/p>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528Pandas\u53bb\u91cd<\/strong><\/p>\n<\/p>\n<p><p>Pandas\u7684<code>drop_duplicates()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u53bb\u9664\u91cd\u590d\u9879\uff0c\u9002\u7528\u4e8e\u6570\u636e\u6846\u548c\u7cfb\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>original_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_series = pd.Series(original_list).drop_duplicates()<\/p>\n<p>unique_list = unique_series.tolist()<\/p>\n<p>print(unique_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u89c4\u6a21\u548c\u5e94\u7528\u573a\u666f\uff0c\u6027\u80fd\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5c0f\u6570\u636e\u96c6<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8f83\u5c0f\u7684\u6570\u636e\u96c6\uff0c\u96c6\u5408\u8f6c\u6362\u662f\u6700\u5feb\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e2d\u7b49\u5927\u5c0f\u7684\u6570\u636e\u96c6<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u5178\u7684fromkeys()\u65b9\u6cd5\u8868\u73b0\u826f\u597d\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u4fdd\u6301\u987a\u5e8f\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5927\u578b\u6570\u636e\u96c6<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0c\u5c24\u5176\u662f\u9700\u8981\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\u65f6\uff0cPandas\u63d0\u4f9b\u4e86\u8f83\u9ad8\u7684\u6548\u7387\u548c\u66f4\u591a\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u4e4b\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\uff1a\u662f\u5426\u9700\u8981\u4fdd\u6301\u987a\u5e8f\u3001\u6570\u636e\u96c6\u7684\u5927\u5c0f\u3001\u4ee5\u53ca\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u7b49\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u548c\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u8bc6\u522b\u548c\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002\u96c6\u5408\u4f1a\u81ea\u52a8\u5ffd\u7565\u91cd\u590d\u9879\uff0c\u56e0\u6b64\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u540e\uff0c\u518d\u8f6c\u6362\u56de\u5217\u8868\u5373\u53ef\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 2, 3, 4, 4, 5]\nunique_list = list(set(my_list))\nprint(unique_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]\n<\/code><\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4fdd\u8bc1\u5143\u7d20\u7684\u539f\u59cb\u987a\u5e8f\u3002\u5982\u679c\u4fdd\u7559\u987a\u5e8f\u5f88\u91cd\u8981\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u91cd\u590d\u5b57\u7b26\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u96c6\u5408\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>join<\/code> \u65b9\u6cd5\u548c <code>set<\/code> \u6765\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u91cd\u590d\u5b57\u7b26\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_string = &quot;hello world&quot;\nunique_string = &#39;&#39;.join(sorted(set(my_string), key=my_string.index))\nprint(unique_string)  # \u8f93\u51fa: &quot;helo wrd&quot;\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4fdd\u6301\u4e86\u5b57\u7b26\u7684\u987a\u5e8f\uff0c\u540c\u65f6\u53bb\u9664\u4e86\u91cd\u590d\u7684\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\uff0c\u6709\u54ea\u4e9b\u9ad8\u6548\u7684\u53bb\u91cd\u65b9\u6cd5\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5927\u6570\u636e\u96c6\uff0c\u4f7f\u7528 pandas \u5e93\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u9009\u62e9\u3002\u901a\u8fc7 <code>drop_duplicates<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5feb\u901f\u53bb\u9664\u91cd\u590d\u884c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;Column1&#39;: [1, 2, 2, 3], &#39;Column2&#39;: [4, 5, 5, 6]}\ndf = pd.DataFrame(data)\ndf_unique = df.drop_duplicates()\nprint(df_unique)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\uff0c\u800c\u4e14\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u6027\u80fd\u4f18\u8d8a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53bb\u9664\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5b57\u5178\uff08dict\uff09\u3001\u4ee5\u53caPandas [&hellip;]","protected":false},"author":3,"featured_media":979770,"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\/979762"}],"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=979762"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/979762\/revisions"}],"predecessor-version":[{"id":979772,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/979762\/revisions\/979772"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/979770"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=979762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=979762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=979762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}