{"id":1105663,"date":"2025-01-08T16:33:03","date_gmt":"2025-01-08T08:33:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1105663.html"},"modified":"2025-01-08T16:33:06","modified_gmt":"2025-01-08T08:33:06","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e5%88%97%e8%a1%a8%e4%b8%ad%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\/1105663.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u5217\u8868\u4e2d\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\/25070452\/161ab6b1-ad8b-4f9f-b6ab-ea032a5b2f5f.webp\" alt=\"python\u5982\u4f55\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\" \/><\/p>\n<p><p> <strong>Python\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528set()\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001collections\u6a21\u5757\u7b49\u3002<\/strong> \u4f7f\u7528set()\u65b9\u6cd5\u662f\u4e00\u79cd\u7b80\u4fbf\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u96c6\u5408\u6570\u636e\u7ed3\u6784\u672c\u8eab\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u3002\u4e3a\u4e86\u8be6\u7ec6\u89e3\u91ca\uff0c\u6211\u4eec\u5c06\u5c55\u5f00\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u5206\u6790\u5176\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528set()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5229\u7528set()\u65b9\u6cd5\u662f\u53bb\u91cd\u7684\u76f4\u63a5\u65b9\u5f0f\u3002\u96c6\u5408\uff08set\uff09\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u96c6\uff0c\u56e0\u6b64\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\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>my_list = list(set(my_list))<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u7b80\u5355\u3001\u4ee3\u7801\u91cf\u5c11\u3001\u901f\u5ea6\u5feb\u3002<br \/><strong>\u7f3a\u70b9\uff1a<\/strong> \u6539\u53d8\u4e86\u5143\u7d20\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u4fdd\u6301\u539f\u6709\u5143\u7d20\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u96c6\u5408\u6765\u5b9e\u73b0\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>seen = set()<\/p>\n<p>new_list = [x for x in my_list if x not in seen and not seen.add(x)]<\/p>\n<p>print(new_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u4fdd\u6301\u4e86\u539f\u6709\u5143\u7d20\u7684\u987a\u5e8f\u3002<br \/><strong>\u7f3a\u70b9\uff1a<\/strong> \u4ee3\u7801\u76f8\u5bf9\u590d\u6742\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528collections.OrderedDict<\/h3>\n<\/p>\n<p><p>\u5728Python 3.7\u53ca\u4ee5\u4e0a\u7248\u672c\uff0cdict\u662f\u6709\u5e8f\u7684\uff0c\u53ef\u4ee5\u5229\u7528collections.OrderedDict\u6765\u8fbe\u5230\u53bb\u91cd\u5e76\u4fdd\u6301\u987a\u5e8f\u7684\u76ee\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import OrderedDict<\/p>\n<p>my_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>new_list = list(OrderedDict.fromkeys(my_list))<\/p>\n<p>print(new_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u4fdd\u6301\u4e86\u539f\u6709\u5143\u7d20\u7684\u987a\u5e8f\uff0c\u4ee3\u7801\u7b80\u6d01\u3002<br \/><strong>\u7f3a\u70b9\uff1a<\/strong> \u9700\u8981\u5f15\u5165\u989d\u5916\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u624b\u52a8\u904d\u5386\u5217\u8868\u5e76\u6784\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u6765\u53bb\u91cd\uff0c\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u6700\u4e3a\u57fa\u7840\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u662f\u6700\u76f4\u89c2\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>new_list = []<\/p>\n<p>for x in my_list:<\/p>\n<p>    if x not in new_list:<\/p>\n<p>        new_list.append(x)<\/p>\n<p>print(new_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u6613\u4e8e\u7406\u89e3\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<br \/><strong>\u7f3a\u70b9\uff1a<\/strong> \u901f\u5ea6\u8f83\u6162\uff0c\u4ee3\u7801\u8f83\u957f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5305\u542b\u5927\u91cf\u6570\u5b57\u6570\u636e\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528Numpy\u5e93\u6765\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>new_list = np.unique(my_list).tolist()<\/p>\n<p>print(new_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u503c\u6570\u636e\u5904\u7406\u3002<br \/><strong>\u7f3a\u70b9\uff1a<\/strong> \u9700\u8981\u5f15\u5165\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u7c7b\u4f3c\u4e8eNumpy\uff0cPandas\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u53bb\u91cd\uff0c\u5c24\u5176\u662f\u5f53\u5904\u7406\u7684\u6570\u636e\u4e3aDataFrame\u65f6\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>new_list = pd.Series(my_list).drop_duplicates().tolist()<\/p>\n<p>print(new_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u573a\u666f\u3002<br \/><strong>\u7f3a\u70b9\uff1a<\/strong> \u9700\u8981\u5f15\u5165\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u4e0d\u540c\u65b9\u6cd5\u5728\u6027\u80fd\u4e0a\u6709\u6240\u5dee\u5f02\uff0c\u5177\u4f53\u9009\u62e9\u9700\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u548c\u9700\u6c42\u6765\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>Sample large list<\/strong><\/h2>\n<p>large_list = list(range(10000)) * 10<\/p>\n<h2><strong>Method 1: Using set()<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>list(set(large_list))<\/p>\n<p>print(&quot;set() method took: %s seconds&quot; % (time.time() - start_time))<\/p>\n<h2><strong>Method 2: Using list comprehension<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>seen = set()<\/p>\n<p>[x for x in large_list if x not in seen and not seen.add(x)]<\/p>\n<p>print(&quot;List comprehension method took: %s seconds&quot; % (time.time() - start_time))<\/p>\n<h2><strong>Method 3: Using collections.OrderedDict<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>list(OrderedDict.fromkeys(large_list))<\/p>\n<p>print(&quot;OrderedDict method took: %s seconds&quot; % (time.time() - start_time))<\/p>\n<h2><strong>Method 4: Using loop<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>new_list = []<\/p>\n<p>for x in large_list:<\/p>\n<p>    if x not in new_list:<\/p>\n<p>        new_list.append(x)<\/p>\n<p>print(&quot;Loop method took: %s seconds&quot; % (time.time() - start_time))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\u8fdb\u884c\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u53d1\u73b0\u5bf9\u4e8e\u8f83\u5927\u7684\u5217\u8868\uff0cset()\u65b9\u6cd5\u548cOrderedDict\u65b9\u6cd5\u8868\u73b0\u8f83\u597d\uff0c\u800c\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u624b\u52a8\u5faa\u73af\u65b9\u6cd5\u5728\u6027\u80fd\u4e0a\u7a0d\u900a\u4e00\u7b79\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5220\u9664Python\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6700\u5e38\u7528\u7684\u6709set()\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001collections.OrderedDict\u7b49\u3002<\/strong> \u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\u6765\u51b3\u5b9a\u3002\u5982\u679c\u9700\u8981\u4fdd\u6301\u5143\u7d20\u987a\u5e8f\uff0c\u63a8\u8350\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216OrderedDict\u65b9\u6cd5\uff1b\u5982\u679c\u4e0d\u9700\u8981\u4fdd\u6301\u987a\u5e8f\uff0cset()\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u9ad8\u6548\u7684\u3002\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u503c\u6570\u636e\u5904\u7406\uff0c\u53ef\u8003\u8651\u4f7f\u7528Numpy\u6216Pandas\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5feb\u901f\u8bc6\u522b\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff1f<\/strong><br \/>\u8bc6\u522b\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u96c6\u5408\uff08set\uff09\uff0c\u56e0\u4e3a\u96c6\u5408\u4e0d\u5141\u8bb8\u91cd\u590d\u503c\u3002\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u518d\u8f6c\u6362\u56de\u5217\u8868\uff0c\u8fd9\u6837\u5c31\u80fd\u5f97\u5230\u53bb\u91cd\u540e\u7684\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>list(set(your_list))<\/code>\u53ef\u4ee5\u5feb\u901f\u79fb\u9664\u91cd\u590d\u5143\u7d20\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\u6765\u624b\u52a8\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u4e8e\u65b0\u5217\u8868\u4e2d\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5185\u7f6e\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff1f<\/strong><br \/>Python\u7684\u6807\u51c6\u5e93\u6ca1\u6709\u4e13\u95e8\u7684\u5185\u7f6e\u51fd\u6570\u6765\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff0c\u4f46\u53ef\u4ee5\u5229\u7528<code>dict.fromkeys()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5b57\u5178\u7684\u952e\u662f\u5217\u8868\u7684\u5143\u7d20\uff0c\u503c\u9ed8\u8ba4\u662f<code>None<\/code>\uff0c\u7531\u4e8e\u5b57\u5178\u7684\u952e\u662f\u552f\u4e00\u7684\uff0c\u6700\u540e\u518d\u5c06\u5176\u8f6c\u56de\u5217\u8868\u5373\u53ef\u5b9e\u73b0\u53bb\u91cd\u3002\u793a\u4f8b\u4ee3\u7801\u4e3a<code>list(dict.fromkeys(your_list))<\/code>\uff0c\u8fd9\u79cd\u65b9\u5f0f\u4fdd\u6301\u4e86\u539f\u59cb\u5143\u7d20\u7684\u987a\u5e8f\u3002<\/p>\n<p><strong>\u5728\u5220\u9664\u91cd\u590d\u5143\u7d20\u65f6\uff0c\u5982\u4f55\u4fdd\u6301\u5217\u8868\u7684\u987a\u5e8f\u4e0d\u53d8\uff1f<\/strong><br \/>\u8981\u5728\u5220\u9664\u91cd\u590d\u5143\u7d20\u7684\u540c\u65f6\u4fdd\u6301\u5217\u8868\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7a7a\u5217\u8868\u548c\u4e00\u4e2a\u96c6\u5408\u6765\u8ffd\u8e2a\u5df2\u51fa\u73b0\u7684\u5143\u7d20\u3002\u904d\u5386\u539f\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u8be5\u5143\u7d20\u4e0d\u5728\u96c6\u5408\u4e2d\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u65b0\u5217\u8868\u548c\u96c6\u5408\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u5730\u5220\u9664\u91cd\u590d\u5143\u7d20\u5e76\u4fdd\u6301\u5143\u7d20\u7684\u539f\u59cb\u987a\u5e8f\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">def remove_duplicates(your_list):\n    seen = set()\n    result = []\n    for item in your_list:\n        if item not in seen:\n            seen.add(item)\n            result.append(item)\n    return result\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528set()\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001collections\u6a21\u5757\u7b49\u3002 \u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1105669,"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\/1105663"}],"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=1105663"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105663\/revisions"}],"predecessor-version":[{"id":1105671,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105663\/revisions\/1105671"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1105669"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1105663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1105663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1105663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}