{"id":1070997,"date":"2025-01-08T11:03:16","date_gmt":"2025-01-08T03:03:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1070997.html"},"modified":"2025-01-08T11:03:18","modified_gmt":"2025-01-08T03:03:18","slug":"python%e5%a6%82%e4%bd%95%e5%8e%bb%e6%8e%89%e5%88%97%e8%a1%a8%e4%b8%ad%e9%87%8d%e5%a4%8d%e7%9a%84%e5%85%83%e7%b4%a0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1070997.html","title":{"rendered":"python\u5982\u4f55\u53bb\u6389\u5217\u8868\u4e2d\u91cd\u590d\u7684\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101620\/55de4e95-3802-4ed8-ae00-c66dbde4335c.webp\" alt=\"python\u5982\u4f55\u53bb\u6389\u5217\u8868\u4e2d\u91cd\u590d\u7684\u5143\u7d20\" \/><\/p>\n<p><p> <strong>Python\u53bb\u6389\u5217\u8868\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u5305\u62ec\uff1a\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u53bb\u91cd\u3001\u4f7f\u7528\u5b57\u5178\u4fdd\u6301\u987a\u5e8f\u53bb\u91cd\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u53bb\u91cd\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u53bb\u91cd\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u53bb\u91cd<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\uff08set\uff09\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u6570\u636e\u7c7b\u578b\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u96c6\u5408\u53bb\u91cd\u3002\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u518d\u8f6c\u6362\u56de\u5217\u8868\u5373\u53ef\u53bb\u6389\u91cd\u590d\u7684\u5143\u7d20\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u6613\u61c2\uff0c\u4e14\u6267\u884c\u6548\u7387\u8f83\u9ad8\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408<\/h4>\n<\/p>\n<p><p>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u662f\u53bb\u91cd\u7684\u7b2c\u4e00\u6b65\u3002\u96c6\u5408\u7684\u7279\u70b9\u662f\u5143\u7d20\u552f\u4e00\uff0c\u56e0\u6b64\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 1, 2, 4, 5]<\/p>\n<p>unique_set = set(original_list)<\/p>\n<p>print(unique_set)  # \u8f93\u51fa\uff1a{1, 2, 3, 4, 5}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5c06\u96c6\u5408\u8f6c\u6362\u56de\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fdd\u6301\u539f\u5217\u8868\u7684\u6570\u636e\u7c7b\u578b\uff0c\u901a\u5e38\u4f1a\u5c06\u96c6\u5408\u518d\u8f6c\u6362\u56de\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">unique_list = list(unique_set)<\/p>\n<p>print(unique_list)  # \u8f93\u51fa\uff1a[1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 1, 2, 4, 5]<\/p>\n<p>unique_list = list(set(original_list))<\/p>\n<p>print(unique_list)  # \u8f93\u51fa\uff1a[1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u4fdd\u6301\u987a\u5e8f\u53bb\u91cd<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u53bb\u91cd\u7684\u65b9\u6cd5\u53ef\u80fd\u4f1a\u6539\u53d8\u539f\u5217\u8868\u7684\u987a\u5e8f\u3002\u5982\u679c\u9700\u8981\u4fdd\u6301\u5143\u7d20\u7684\u539f\u59cb\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u5b9e\u73b0\u3002Python 3.7\u4ee5\u540e\u7684\u5b57\u5178\u662f\u6709\u5e8f\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u5229\u7528\u5b57\u5178\u7684\u952e\u552f\u4e00\u6027\u6765\u53bb\u91cd\uff0c\u540c\u65f6\u4fdd\u6301\u539f\u5217\u8868\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u5b57\u5178\u7684fromkeys\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u7684fromkeys\u65b9\u6cd5\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u952e\u662f\u539f\u5217\u8868\u7684\u5143\u7d20\uff0c\u503c\u662fNone\u3002\u7531\u4e8e\u5b57\u5178\u7684\u952e\u662f\u552f\u4e00\u7684\uff0c\u56e0\u6b64\u81ea\u52a8\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 1, 2, 4, 5]<\/p>\n<p>unique_list = list(dict.fromkeys(original_list))<\/p>\n<p>print(unique_list)  # \u8f93\u51fa\uff1a[1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 1, 2, 4, 5]<\/p>\n<p>unique_list = list(dict.fromkeys(original_list))<\/p>\n<p>print(unique_list)  # \u8f93\u51fa\uff1a[1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53bb\u91cd<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5217\u8868\u3002\u5728\u53bb\u91cd\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528\u4e00\u4e2a\u8f85\u52a9\u96c6\u5408\u6765\u8bb0\u5f55\u5df2\u7ecf\u9047\u5230\u7684\u5143\u7d20\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5143\u7d20\u53ea\u51fa\u73b0\u4e00\u6b21\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u8f85\u52a9\u96c6\u5408<\/h4>\n<\/p>\n<p><p>\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u4f7f\u7528\u4e00\u4e2a\u8f85\u52a9\u96c6\u5408\uff08seen\uff09\u6765\u8bb0\u5f55\u5df2\u7ecf\u9047\u5230\u7684\u5143\u7d20\u3002\u5982\u679c\u5f53\u524d\u5143\u7d20\u4e0d\u5728\u96c6\u5408\u4e2d\uff0c\u5c31\u5c06\u5176\u6dfb\u52a0\u5230\u7ed3\u679c\u5217\u8868\u548c\u96c6\u5408\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 1, 2, 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\uff1a[1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 1, 2, 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\uff1a[1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u53bb\u91cd<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u7b80\u5355\u3001\u9ad8\u6548\uff0c\u4ee3\u7801\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u65e0\u6cd5\u4fdd\u6301\u539f\u5217\u8868\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5b57\u5178\u4fdd\u6301\u987a\u5e8f\u53bb\u91cd<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u4fdd\u6301\u539f\u5217\u8868\u7684\u987a\u5e8f\uff0c\u4ee3\u7801\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u9700\u8981Python 3.7\u53ca\u4ee5\u4e0a\u7248\u672c\uff08\u5b57\u5178\u6709\u5e8f\uff09\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53bb\u91cd<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u4fdd\u6301\u539f\u5217\u8868\u7684\u987a\u5e8f\uff0c\u4ee3\u7801\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u4ee3\u7801\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff0c\u76f8\u5bf9\u4e8e\u96c6\u5408\u53bb\u91cd\u6548\u7387\u7565\u4f4e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u9009\u62e9<\/h3>\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\u3002\u5982\u679c\u4e0d\u5173\u5fc3\u5143\u7d20\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u9009\u62e9\u96c6\u5408\u53bb\u91cd\uff0c\u56e0\u4e3a\u8fd9\u79cd\u65b9\u6cd5\u6700\u4e3a\u7b80\u5355\u548c\u9ad8\u6548\u3002\u5982\u679c\u9700\u8981\u4fdd\u6301\u539f\u5217\u8868\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u9009\u62e9\u5b57\u5178\u53bb\u91cd\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u53bb\u91cd\uff0c\u524d\u8005\u4ee3\u7801\u7b80\u6d01\uff0c\u540e\u8005\u66f4\u5177\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u8d77\u6765\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u53bb\u6389\u5217\u8868\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u53bb\u91cd\u3001\u4f7f\u7528\u5b57\u5178\u4fdd\u6301\u987a\u5e8f\u53bb\u91cd\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u53bb\u91cd\u7b49\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u53bb\u9664\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\u5728Python\u4e2d\u5feb\u901f\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\u5feb\u901f\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u540e\uff0c\u518d\u5c06\u5176\u8f6c\u6362\u56de\u5217\u8868\u5373\u53ef\u3002\u793a\u4f8b\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>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\uff0c\u4f46\u6ce8\u610f\u96c6\u5408\u4f1a\u6253\u4e71\u5143\u7d20\u7684\u539f\u59cb\u987a\u5e8f\u3002<\/p>\n<p><strong>\u5982\u4f55\u4fdd\u7559\u539f\u59cb\u987a\u5e8f\u540c\u65f6\u53bb\u6389\u91cd\u590d\u5143\u7d20\uff1f<\/strong><br \/>\u4e3a\u4e86\u5728\u53bb\u91cd\u7684\u540c\u65f6\u4fdd\u7559\u5143\u7d20\u7684\u539f\u59cb\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 2, 3, 4, 4, 5]\nunique_list = []\n[unique_list.append(x) for x in my_list if x not in unique_list]\nprint(unique_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7565\u663e\u7e41\u7410\uff0c\u4f46\u80fd\u591f\u4fdd\u6301\u5217\u8868\u4e2d\u5143\u7d20\u7684\u987a\u5e8f\u4e0d\u53d8\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u73b0\u6210\u7684Python\u5e93\u53ef\u4ee5\u53bb\u91cd\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u4e2d\u7684Pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u5305\u62ec\u53bb\u91cd\u3002\u4f7f\u7528Pandas\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u53bb\u6389\u91cd\u590d\u5143\u7d20\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u4fdd\u7559\u539f\u59cb\u987a\u5e8f\u7684\u9009\u9879\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\nmy_list = [1, 2, 2, 3, 4, 4, 5]\nunique_list = pd.Series(my_list).drop_duplicates().tolist()\nprint(unique_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]\n<\/code><\/pre>\n<p>\u4f7f\u7528Pandas\u7684drop_duplicates\u65b9\u6cd5\uff0c\u4e0d\u4ec5\u7b80\u5316\u4e86\u53bb\u91cd\u7684\u8fc7\u7a0b\uff0c\u8fd8\u80fd\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53bb\u6389\u5217\u8868\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u5305\u62ec\uff1a\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u53bb\u91cd\u3001\u4f7f\u7528\u5b57\u5178\u4fdd\u6301\u987a\u5e8f\u53bb\u91cd\u3001\u5217\u8868\u63a8\u5bfc [&hellip;]","protected":false},"author":3,"featured_media":1071003,"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\/1070997"}],"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=1070997"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070997\/revisions"}],"predecessor-version":[{"id":1071005,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070997\/revisions\/1071005"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1071003"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1070997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1070997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1070997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}