{"id":1133244,"date":"2025-01-08T21:04:40","date_gmt":"2025-01-08T13:04:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133244.html"},"modified":"2025-01-08T21:04:43","modified_gmt":"2025-01-08T13:04:43","slug":"%e5%a6%82%e4%bd%95%e7%a7%bb%e9%99%a4python%e5%88%97%e8%a1%a8%e9%87%8c%e9%9d%a2%e7%9a%84%e9%87%8d%e5%a4%8d%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1133244.html","title":{"rendered":"\u5982\u4f55\u79fb\u9664python\u5217\u8868\u91cc\u9762\u7684\u91cd\u590d\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102546\/cfe4ef06-1635-48dd-8832-31f733bd7496.webp\" alt=\"\u5982\u4f55\u79fb\u9664python\u5217\u8868\u91cc\u9762\u7684\u91cd\u590d\u5143\u7d20\" \/><\/p>\n<p><p> <strong>\u79fb\u9664Python\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u96c6\u5408\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5b57\u5178\u7b49\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u96c6\u5408\uff08set\uff09\uff0c\u56e0\u4e3a\u96c6\u5408\u81ea\u52a8\u53bb\u91cd\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7\u904d\u5386\u5217\u8868\u5e76\u68c0\u67e5\u662f\u5426\u5df2\u5b58\u5728\u4e8e\u65b0\u7684\u5217\u8868\u4e2d\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u8bb2\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\uff08set\uff09<\/p>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u6570\u636e\u7ed3\u6784\uff0c\u56e0\u6b64\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u53ef\u4ee5\u81ea\u52a8\u53bb\u91cd\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_list = list(set(my_list))<\/p>\n<p>print(unique_list)  # [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a\u5feb\u901f\u3001\u7b80\u6d01\uff0c\u4e0d\u9700\u8981\u989d\u5916\u7684\u5faa\u73af\u3002<br \/>\u7f3a\u70b9\uff1a\u6539\u53d8\u4e86\u539f\u6709\u5217\u8868\u7684\u987a\u5e8f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b83\u6765\u5b9e\u73b0\u53bb\u91cd\u3002\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u8f85\u52a9\u96c6\u5408\u6765\u8bb0\u5f55\u5df2\u7ecf\u9047\u5230\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>seen = set()<\/p>\n<p>unique_list = [x for x in my_list if x not in seen and (seen.add(x) or True)]<\/p>\n<p>print(unique_list)  # [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a\u4fdd\u6301\u539f\u6709\u987a\u5e8f\u3002<br \/>\u7f3a\u70b9\uff1a\u4ee3\u7801\u7a0d\u5fae\u590d\u6742\uff0c\u9700\u8981\u7406\u89e3\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u96c6\u5408\u7684\u7528\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5b57\u5178\uff08dict\uff09<\/p>\n<\/p>\n<p><p>\u4ecePython 3.7\u5f00\u59cb\uff0c\u5b57\u5178\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\uff0c\u56e0\u6b64\u53ef\u4ee5\u5229\u7528\u5b57\u5178\u7684\u952e\u4e0d\u91cd\u590d\u7279\u6027\u6765\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_list = list(dict.fromkeys(my_list))<\/p>\n<p>print(unique_list)  # [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a\u4fdd\u6301\u539f\u6709\u987a\u5e8f\uff0c\u4ee3\u7801\u7b80\u6d01\u3002<br \/>\u7f3a\u70b9\uff1a\u9700\u8981Python 3.7\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386<\/p>\n<\/p>\n<p><p>\u8fd9\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u904d\u5386\u5217\u8868\u5e76\u68c0\u67e5\u662f\u5426\u5df2\u5b58\u5728\u4e8e\u65b0\u7684\u5217\u8868\u4e2d\u6765\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_list = []<\/p>\n<p>for item in my_list:<\/p>\n<p>    if item not in unique_list:<\/p>\n<p>        unique_list.append(item)<\/p>\n<p>print(unique_list)  # [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a\u4fdd\u6301\u539f\u6709\u987a\u5e8f\uff0c\u4ee3\u7801\u7b80\u5355\u6613\u61c2\u3002<br \/>\u7f3a\u70b9\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u5bf9\u4e8e\u5927\u5217\u8868\u6548\u7387\u8f83\u4f4e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528Pandas\u5e93<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5df2\u7ecf\u5728\u4f7f\u7528Pandas\u5e93\u5904\u7406\u6570\u636e\uff0c\u53ef\u4ee5\u5229\u7528Pandas\u7684\u53bb\u91cd\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>my_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_list = pd.Series(my_list).drop_duplicates().tolist()<\/p>\n<p>print(unique_list)  # [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002<br \/>\u7f3a\u70b9\uff1a\u9700\u8981\u5b89\u88c5Pandas\u5e93\uff0c\u4ee3\u7801\u4f9d\u8d56\u6027\u8f83\u9ad8\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53bb\u91cd\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u4e0a\u4e0b\u6587\u3002\u5982\u679c\u4f60\u9700\u8981\u5feb\u901f\u53bb\u91cd\u4e14\u4e0d\u5728\u4e4e\u987a\u5e8f\uff0c\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u662f\u6700\u7b80\u5355\u7684\u3002\u5982\u679c\u4f60\u9700\u8981\u4fdd\u6301\u539f\u6709\u987a\u5e8f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5b57\u5178\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\uff0cPandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u79fb\u9664Python\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5185\u7f6e\u51fd\u6570\u79fb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>set()<\/code>\u51fd\u6570\u6765\u79fb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002<code>set<\/code>\u662f\u4e00\u4e2a\u65e0\u5e8f\u4e0d\u91cd\u590d\u5143\u7d20\u96c6\uff0c\u56e0\u6b64\u5c06\u5217\u8868\u8f6c\u6362\u4e3a<code>set<\/code>\uff0c\u518d\u5c06\u5176\u8f6c\u6362\u56de\u5217\u8868\uff0c\u53ef\u4ee5\u8f7b\u677e\u53bb\u9664\u91cd\u590d\u9879\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 2, 3, 4, 4, 5]\nunique_list = list(set(my_list))\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>unique_list<\/code>\u5c06\u53d8\u4e3a<code>[1, 2, 3, 4, 5]<\/code>\u3002<\/p>\n<p><strong>\u5728\u79fb\u9664\u91cd\u590d\u5143\u7d20\u7684\u540c\u65f6\u4fdd\u6301\u539f\u6709\u987a\u5e8f\uff0c\u8be5\u5982\u4f55\u64cd\u4f5c\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5728\u53bb\u9664\u91cd\u590d\u5143\u7d20\u65f6\u4fdd\u7559\u539f\u5217\u8868\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u5229\u7528\u5b57\u5178\u7684\u7279\u6027\u3002\u5728Python 3.7\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 2, 3, 4, 4, 5]\nunique_list = list(dict.fromkeys(my_list))\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u987a\u5e8f\u4e0d\u53d8\u7684\u552f\u4e00\u5143\u7d20\u5217\u8868\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5927\u578b\u5217\u8868\u65f6\uff0c\u79fb\u9664\u91cd\u590d\u5143\u7d20\u7684\u6548\u7387\u5982\u4f55\uff1f<\/strong><br \/>\u5904\u7406\u5927\u578b\u5217\u8868\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u4f7f\u7528<code>set()<\/code>\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff0c\u8fd9\u4f7f\u5f97\u5b83\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u975e\u5e38\u9ad8\u6548\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u53bb\u91cd\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n^2)\uff0c\u5728\u6570\u636e\u91cf\u5927\u65f6\u4f1a\u5bfc\u81f4\u660e\u663e\u7684\u6027\u80fd\u4e0b\u964d\u3002\u56e0\u6b64\uff0c\u4f18\u5148\u8003\u8651\u4f7f\u7528<code>set()<\/code>\u6216\u5b57\u5178\u7684\u65b9\u6cd5\u6765\u786e\u4fdd\u4ee3\u7801\u7684\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u79fb\u9664Python\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u96c6\u5408\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5b57\u5178\u7b49\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u96c6\u5408\uff08set\uff09 [&hellip;]","protected":false},"author":3,"featured_media":1133252,"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\/1133244"}],"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=1133244"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133244\/revisions"}],"predecessor-version":[{"id":1133254,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133244\/revisions\/1133254"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1133252"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1133244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1133244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1133244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}