{"id":1068413,"date":"2024-12-31T16:43:15","date_gmt":"2024-12-31T08:43:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1068413.html"},"modified":"2024-12-31T16:43:18","modified_gmt":"2024-12-31T08:43:18","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e5%88%97%e8%a1%a8%e4%b8%ad%e9%87%8d%e5%a4%8d%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1068413.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5220\u9664\u5217\u8868\u4e2d\u91cd\u590d\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/d0486899-2f52-4b81-837c-c4f7e025dc4d.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u5220\u9664\u5217\u8868\u4e2d\u91cd\u590d\u5143\u7d20\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528\u5b57\u5178\u3001\u4f7f\u7528\u96c6\u5408\u7684fromkeys\u65b9\u6cd5<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5e76\u9010\u4e00\u8bb2\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u65b9\u6cd5\uff1a\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u53bb\u91cd\uff0c\u518d\u8f6c\u6362\u56de\u5217\u8868<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff08set\uff09\u662f\u5220\u9664\u5217\u8868\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u4e00\u79cd\u9ad8\u6548\u65b9\u6cd5\uff0c\u56e0\u4e3a\u96c6\u5408\u4e0d\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\u3002\u5c06\u4e00\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u65f6\uff0c\u91cd\u590d\u5143\u7d20\u5c06\u88ab\u81ea\u52a8\u5220\u9664\u3002\u5c06\u96c6\u5408\u8f6c\u6362\u56de\u5217\u8868\uff0c\u4ee5\u4fdd\u7559\u5217\u8868\u7684\u7279\u6027\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u3001\u9ad8\u6548\uff0c\u4f46\u4f1a\u6539\u53d8\u5143\u7d20\u7684\u987a\u5e8f\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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>set(my_list)<\/code>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u5220\u9664\u6240\u6709\u91cd\u590d\u7684\u5143\u7d20\uff0c\u7136\u540e\u4f7f\u7528<code>list()<\/code>\u5c06\u96c6\u5408\u8f6c\u6362\u56de\u5217\u8868\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\uff08set\uff09<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u662f\u5220\u9664\u5217\u8868\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u6700\u7b80\u5355\u548c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4e0d\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\u3002\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u53ef\u4ee5\u81ea\u52a8\u5220\u9664\u6240\u6709\u91cd\u590d\u5143\u7d20\u3002\u7136\u540e\u5c06\u96c6\u5408\u8f6c\u6362\u56de\u5217\u8868\uff0c\u4ee5\u4fdd\u7559\u5217\u8868\u7684\u6570\u636e\u7c7b\u578b\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>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u975e\u5e38\u7b80\u5355\u548c\u9ad8\u6548\uff0c\u4f46\u5b83\u4f1a\u6539\u53d8\u5143\u7d20\u7684\u987a\u5e8f\u3002\u5982\u679c\u987a\u5e8f\u5f88\u91cd\u8981\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u3002<\/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\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u5728\u5220\u9664\u91cd\u590d\u5143\u7d20\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u4e00\u4e2a\u8f85\u52a9\u96c6\u5408\u6765\u5b9e\u73b0\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>my_list = [x for x in my_list if not (x in seen or seen.add(x))]<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u8f85\u52a9\u96c6\u5408<code>seen<\/code>\u6765\u8ddf\u8e2a\u5df2\u7ecf\u9047\u5230\u7684\u5143\u7d20\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u539f\u59cb\u5217\u8868\uff0c\u5e76\u4ec5\u5c06\u5c1a\u672a\u9047\u5230\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u65b0\u7684\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u5728Python 3.7\u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u5b57\u5178\uff08dict\uff09\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u5220\u9664\u91cd\u590d\u5143\u7d20\uff0c\u540c\u65f6\u4fdd\u7559\u539f\u59cb\u987a\u5e8f\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(dict.fromkeys(my_list))<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>dict.fromkeys(my_list)<\/code>\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u503c\u4e3aNone\u3002\u7531\u4e8e\u5b57\u5178\u952e\u4e0d\u80fd\u91cd\u590d\uff0c\u91cd\u590d\u5143\u7d20\u5c06\u88ab\u81ea\u52a8\u5220\u9664\u3002\u7136\u540e\u5c06\u5b57\u5178\u7684\u952e\u8f6c\u6362\u56de\u5217\u8868\uff0c\u4ee5\u4fdd\u7559\u539f\u59cb\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u96c6\u5408\u7684fromkeys\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u96c6\u5408\u7684<code>fromkeys<\/code>\u65b9\u6cd5\u7c7b\u4f3c\u4e8e\u5b57\u5178\u7684<code>fromkeys<\/code>\u65b9\u6cd5\uff0c\u4f46\u5b83\u4ec5\u9002\u7528\u4e8e\u96c6\u5408\u3002\u4e0e\u5b57\u5178\u4e0d\u540c\uff0c\u96c6\u5408\u4e0d\u4fdd\u7559\u5143\u7d20\u7684\u63d2\u5165\u987a\u5e8f\u3002\u56e0\u6b64\uff0c\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5bf9\u987a\u5e8f\u6ca1\u6709\u8981\u6c42\u7684\u60c5\u51b5\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().fromkeys(my_list))<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u7684\u5de5\u4f5c\u539f\u7406\u4e0e\u4f7f\u7528\u5b57\u5178\u7684<code>fromkeys<\/code>\u65b9\u6cd5\u76f8\u540c\uff0c\u552f\u4e00\u7684\u533a\u522b\u662f\u4f7f\u7528\u96c6\u5408\u6765\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386<\/p>\n<\/p>\n<p><p>\u5faa\u73af\u904d\u5386\u5217\u8868\u5e76\u6784\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u540c\u65f6\u68c0\u67e5\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u4e8e\u65b0\u7684\u5217\u8868\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4fdd\u7559\u539f\u59cb\u987a\u5e8f\uff0c\u4f46\u6548\u7387\u8f83\u4f4e\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u5927\u5217\u8868\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 item in my_list:<\/p>\n<p>    if item not in new_list:<\/p>\n<p>        new_list.append(item)<\/p>\n<p>print(new_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u5b9e\u73b0\u7b80\u5355\uff0c\u7f3a\u70b9\u662f\u6548\u7387\u8f83\u4f4e\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u5927\u5217\u8868\uff0c\u56e0\u4e3a\u6bcf\u6b21\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5728\u65b0\u5217\u8868\u4e2d\u90fd\u9700\u8981\u904d\u5386\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528Pandas\u5e93<\/p>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\u3002\u4f7f\u7528Pandas\u7684<code>drop_duplicates<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5220\u9664\u91cd\u590d\u5143\u7d20\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>my_list = pd.Series(my_list).drop_duplicates().tolist()<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u5217\u8868\u8f6c\u6362\u4e3aPandas\u7684Series\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>drop_duplicates<\/code>\u65b9\u6cd5\u5220\u9664\u91cd\u590d\u5143\u7d20\u3002\u6700\u540e\u5c06\u7ed3\u679c\u8f6c\u6362\u56de\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528Numpy\u5e93<\/p>\n<\/p>\n<p><p>Numpy\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u6570\u503c\u6570\u636e\u3002\u4f7f\u7528Numpy\u7684<code>unique<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5220\u9664\u91cd\u590d\u5143\u7d20\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>my_list = np.unique(my_list).tolist()<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumpy\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528<code>unique<\/code>\u65b9\u6cd5\u5220\u9664\u91cd\u590d\u5143\u7d20\u3002\u6700\u540e\u5c06\u7ed3\u679c\u8f6c\u6362\u56de\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u4f7f\u7528\u96c6\u5408\u548c\u6392\u5e8f<\/p>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u6392\u5e8f\uff0c\u53ef\u4ee5\u7ed3\u5408\u96c6\u5408\u548c\u6392\u5e8f\u6765\u5220\u9664\u91cd\u590d\u5143\u7d20\u3002\u9996\u5148\u4f7f\u7528\u96c6\u5408\u5220\u9664\u91cd\u590d\u5143\u7d20\uff0c\u7136\u540e\u5bf9\u7ed3\u679c\u6392\u5e8f\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 = sorted(set(my_list))<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u9ad8\u6548\uff0c\u4f46\u4ec5\u9002\u7528\u4e8e\u53ef\u4ee5\u6392\u5e8f\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u4f7f\u7528\u9012\u5f52<\/p>\n<\/p>\n<p><p>\u9012\u5f52\u65b9\u6cd5\u8f83\u4e3a\u590d\u6742\uff0c\u4f46\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u5220\u9664\u91cd\u590d\u5143\u7d20\u7684\u529f\u80fd\u3002\u5728\u9012\u5f52\u8fc7\u7a0b\u4e2d\uff0c\u9010\u6b65\u6784\u5efa\u6ca1\u6709\u91cd\u590d\u5143\u7d20\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_duplicates(my_list):<\/p>\n<p>    if not my_list:<\/p>\n<p>        return []<\/p>\n<p>    elif my_list[0] in my_list[1:]:<\/p>\n<p>        return remove_duplicates(my_list[1:])<\/p>\n<p>    else:<\/p>\n<p>        return [my_list[0]] + remove_duplicates(my_list[1:])<\/p>\n<p>my_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>my_list = remove_duplicates(my_list)<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u9012\u5f52\u5b9e\u73b0\uff0c\u4f46\u7f3a\u70b9\u662f\u6548\u7387\u8f83\u4f4e\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u5927\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b<\/p>\n<\/p>\n<p><p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002\u901a\u8fc7\u4f7f\u7528<code>filter<\/code>\u548c<code>lambda<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5220\u9664\u91cd\u590d\u5143\u7d20\u7684\u529f\u80fd\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>my_list = list(filter(lambda x: not (x in seen or seen.add(x)), my_list))<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>filter<\/code>\u51fd\u6570\u548c<code>lambda<\/code>\u51fd\u6570\u6765\u904d\u5386\u539f\u59cb\u5217\u8868\uff0c\u5e76\u4ec5\u5c06\u5c1a\u672a\u9047\u5230\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u65b0\u7684\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u3002\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u4f46\u4f1a\u6539\u53d8\u5143\u7d20\u7684\u987a\u5e8f\u3002\u5982\u679c\u9700\u8981\u4fdd\u7559\u539f\u59cb\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5b57\u5178\u3001Pandas\u5e93\u6216Numpy\u5e93\u3002\u5bf9\u4e8e\u7279\u6b8a\u60c5\u51b5\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u9012\u5f52\u6216\u51fd\u6570\u5f0f\u7f16\u7a0b\u65b9\u6cd5\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u5b9e\u73b0\u6700\u4f73\u6027\u80fd\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>set()<\/code>\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u9879\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u5c06\u96c6\u5408\u8f6c\u6362\u56de\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">original_list = [1, 2, 2, 3, 4, 4, 5]\nunique_list = list(set(original_list))\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u4e14\u9ad8\u6548\uff0c\u4f46\u4f1a\u4e22\u5931\u539f\u59cb\u5217\u8868\u4e2d\u7684\u5143\u7d20\u987a\u5e8f\u3002\u5982\u679c\u4fdd\u6301\u987a\u5e8f\u662f\u4e00\u4e2a\u91cd\u8981\u8003\u8651\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u548c<code>not in<\/code>\u8bed\u53e5\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868\u3002<\/p>\n<p><strong>\u4f7f\u7528\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u4fdd\u7559\u5217\u8868\u5143\u7d20\u7684\u539f\u59cb\u987a\u5e8f\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5728\u5220\u9664\u91cd\u590d\u5143\u7d20\u7684\u540c\u65f6\u4fdd\u7559\u539f\u59cb\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7a7a\u5217\u8868\u548c<code>for<\/code>\u5faa\u73af\u6765\u904d\u5386\u539f\u59cb\u5217\u8868\u3002\u901a\u8fc7\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u4e8e\u65b0\u5217\u8868\u4e2d\uff0c\u53ef\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u5143\u7d20\u53ea\u6dfb\u52a0\u4e00\u6b21\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">original_list = [1, 2, 2, 3, 4, 4, 5]\nunique_list = []\nfor item in original_list:\n    if item not in unique_list:\n        unique_list.append(item)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u4fdd\u6301\u4e86\u5143\u7d20\u7684\u987a\u5e8f\uff0c\u8fd8\u786e\u4fdd\u4e86\u6bcf\u4e2a\u5143\u7d20\u5728\u65b0\u5217\u8868\u4e2d\u53ea\u51fa\u73b0\u4e00\u6b21\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5927\u578b\u5217\u8868\u65f6\uff0c\u6709\u4ec0\u4e48\u6548\u7387\u66f4\u9ad8\u7684\u65b9\u6cd5\u5417\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5927\u578b\u5217\u8868\uff0c\u4f7f\u7528<code>dict.fromkeys()<\/code>\u6216<code>OrderedDict<\/code>\u53ef\u4ee5\u6709\u6548\u53bb\u91cd\u5e76\u4fdd\u6301\u987a\u5e8f\u3002<code>dict.fromkeys()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u952e\u4e3a\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u503c\u5747\u4e3a<code>None<\/code>\u3002\u5b57\u5178\u7684\u952e\u662f\u552f\u4e00\u7684\uff0c\u56e0\u6b64\u91cd\u590d\u7684\u5143\u7d20\u4f1a\u88ab\u81ea\u52a8\u53bb\u9664\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">original_list = [1, 2, 2, 3, 4, 4, 5]\nunique_list = list(dict.fromkeys(original_list))\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u8868\u73b0\u826f\u597d\uff0c\u4e14\u5177\u6709\u8f83\u9ad8\u7684\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528\u5b57\u5178\u3001\u4f7f\u7528\u96c6\u5408\u7684fro [&hellip;]","protected":false},"author":3,"featured_media":1068419,"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\/1068413"}],"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=1068413"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068413\/revisions"}],"predecessor-version":[{"id":1068422,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068413\/revisions\/1068422"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1068419"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1068413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1068413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1068413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}