{"id":1106017,"date":"2025-01-08T16:36:14","date_gmt":"2025-01-08T08:36:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1106017.html"},"modified":"2025-01-08T16:36:14","modified_gmt":"2025-01-08T08:36:14","slug":"python%e5%a6%82%e4%bd%95%e4%b8%8d%e5%8f%96%e5%88%97%e8%a1%a8%e4%b8%ad%e9%87%8d%e5%a4%8d%e5%80%bc%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1106017.html","title":{"rendered":"python\u5982\u4f55\u4e0d\u53d6\u5217\u8868\u4e2d\u91cd\u590d\u503c\u5faa\u73af"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070644\/108c7764-28c5-40aa-ae8b-d446b58f3a6f.webp\" alt=\"python\u5982\u4f55\u4e0d\u53d6\u5217\u8868\u4e2d\u91cd\u590d\u503c\u5faa\u73af\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u901a\u8fc7\u4e0d\u53d6\u5217\u8868\u4e2d\u7684\u91cd\u590d\u503c\u6765\u8fdb\u884c\u5faa\u73af\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6570\u636e\u7ed3\u6784\u3001\u5b57\u5178\uff08dictionary\uff09\u6765\u5b9e\u73b0<\/strong>\u3002\u96c6\u5408\u548c\u5b57\u5178\u7684\u6570\u636e\u7ed3\u6784\u5177\u6709\u53bb\u91cd\u7279\u6027\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u503c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u4ece\u591a\u4e2a\u89d2\u5ea6\u8fdb\u884c\u8be6\u7ec6\u7684\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u53bb\u91cd<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u96c6\u5408\uff0c\u56e0\u6b64\u53ef\u4ee5\u5229\u7528\u96c6\u5408\u7684\u7279\u6027\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u503c\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u8f6c\u6362\u5217\u8868\u4e3a\u96c6\u5408<\/h4>\n<\/p>\n<p><p>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u5bf9\u96c6\u5408\u8fdb\u884c\u5faa\u73af\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_set = set(my_list)<\/p>\n<p>for item in unique_set:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>set(my_list)<\/code>\u4f1a\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u4ece\u800c\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u503c\u3002\u7136\u540e\u5bf9\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u5faa\u73af\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u518d\u8f6c\u6362\u56de\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u4fdd\u6301\u539f\u5217\u8868\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u5148\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u53bb\u91cd\uff0c\u518d\u8f6c\u6362\u56de\u5217\u8868\u8fdb\u884c\u5faa\u73af\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>for item in unique_list:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8fd9\u6837\u7684\u64cd\u4f5c\u8db3\u591f\u6ee1\u8db3\u9700\u6c42\u3002\u5982\u679c\u987a\u5e8f\u5f88\u91cd\u8981\uff0c\u5efa\u8bae\u4f7f\u7528\u4e0b\u4e00\u4e2a\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\uff08dictionary\uff09\u53bb\u91cd<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u952e\u5177\u6709\u552f\u4e00\u6027\uff0c\u56e0\u6b64\u53ef\u4ee5\u5229\u7528\u5b57\u5178\u7684\u952e\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528\u5b57\u5178\u952e\u53bb\u91cd<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5b57\u5178\u7684\u952e\u6765\u53bb\u9664\u91cd\u590d\u503c\uff0c\u4fdd\u6301\u539f\u5217\u8868\u987a\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_dict = dict.fromkeys(my_list)<\/p>\n<p>for item in unique_dict:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>dict.fromkeys(my_list)<\/code>\u4f1a\u521b\u5efa\u4e00\u4e2a\u952e\u4e3a\u5217\u8868\u5143\u7d20\u7684\u5b57\u5178\uff0c\u4ece\u800c\u53bb\u9664\u91cd\u590d\u503c\uff0c\u5e76\u4fdd\u6301\u539f\u5217\u8868\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53bb\u91cd<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u96c6\u5408\u6765\u53bb\u91cd\uff0c\u4fdd\u6301\u539f\u5217\u8868\u987a\u5e8f\uff1a<\/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 not (x in seen or seen.add(x))]<\/p>\n<p>for item in unique_list:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u96c6\u5408\u7684\u7ed3\u5408\uff0c\u65e2\u80fd\u53bb\u9664\u91cd\u590d\u503c\uff0c\u53c8\u80fd\u4fdd\u6301\u539f\u5217\u8868\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Pandas\u53bb\u91cd<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6b63\u5728\u5904\u7406\u7684\u6570\u636e\u6bd4\u8f83\u590d\u6742\u6216\u8005\u6570\u636e\u91cf\u8f83\u5927\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u53bb\u9664\u91cd\u590d\u503c\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528Pandas\u53bb\u91cd<\/h4>\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_series = pd.Series(my_list).drop_duplicates()<\/p>\n<p>for item in unique_series:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7<code>pandas.Series<\/code>\u5c06\u5217\u8868\u8f6c\u6362\u4e3aPandas\u7684Series\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>drop_duplicates<\/code>\u65b9\u6cd5\u53bb\u9664\u91cd\u590d\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e94\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>5.1 \u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<ul>\n<li>\u6570\u636e\u9884\u5904\u7406\uff1a\u5728\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u53bb\u9664\u91cd\u590d\u503c\u662f\u5176\u4e2d\u7684\u91cd\u8981\u6b65\u9aa4\u4e4b\u4e00\u3002<\/li>\n<li>\u6570\u636e\u5e93\u53bb\u91cd\uff1a\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\uff0c\u53bb\u9664\u91cd\u590d\u503c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u7684\u8d28\u91cf\u548c\u67e5\u8be2\u7684\u6548\u7387\u3002<\/li>\n<li>\u722c\u866b\u6570\u636e\u5904\u7406\uff1a\u5728\u8fdb\u884c\u7f51\u9875\u722c\u866b\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u91cd\u590d\u7684\u6570\u636e\uff0c\u901a\u8fc7\u53bb\u91cd\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u7684\u6709\u6548\u6027\u3002<\/li>\n<\/ul>\n<p><h4>5.2 \u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u5bf9\u4e8e\u5c0f\u89c4\u6a21\u7684\u6570\u636e\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u7684\u6027\u80fd\u5dee\u5f02\u4e0d\u5927\uff0c\u4f46\u5bf9\u4e8e\u5927\u89c4\u6a21\u7684\u6570\u636e\uff0c\u96c6\u5408\u548c\u5b57\u5178\u7684\u65b9\u6cd5\u901a\u5e38\u66f4\u9ad8\u6548\u3002<\/li>\n<li><strong>\u987a\u5e8f<\/strong>\uff1a\u5982\u679c\u9700\u8981\u4fdd\u7559\u539f\u5217\u8868\u7684\u987a\u5e8f\uff0c\u5efa\u8bae\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8005Pandas\u7684\u65b9\u6cd5\u3002<\/li>\n<li><strong>\u6570\u636e\u7c7b\u578b<\/strong>\uff1a\u96c6\u5408\u548c\u5b57\u5178\u7684\u65b9\u6cd5\u4ec5\u9002\u7528\u4e8e\u53ef\u54c8\u5e0c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u4e0d\u53ef\u54c8\u5e0c\u7684\u6570\u636e\u7c7b\u578b\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\uff09\uff0c\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u8fdb\u884c\u53bb\u91cd\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u65b9\u4fbf\u5730\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u503c\u5e76\u8fdb\u884c\u5faa\u73af\u64cd\u4f5c\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u96c6\u5408\u3001\u5b57\u5178\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548cPandas\u5e93\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7684\u89c4\u6a21\u3001\u987a\u5e8f\u8981\u6c42\u548c\u6570\u636e\u7c7b\u578b\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u53bb\u91cd\u662f\u6570\u636e\u5904\u7406\u4e2d\u7684\u5e38\u89c1\u9700\u6c42\uff0c\u901a\u8fc7\u5408\u7406\u4f7f\u7528Python\u7684\u5185\u7f6e\u6570\u636e\u7ed3\u6784\u548c\u5e93\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002\u5e0c\u671b\u672c\u6587\u80fd\u4e3a\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u53c2\u8003\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u503c\uff1f<\/strong><br \/>\u8981\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\uff0c\u56e0\u4e3a\u96c6\u5408\u672c\u8eab\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u3002\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u540e\uff0c\u518d\u5c06\u5176\u8f6c\u6362\u56de\u5217\u8868\uff0c\u5373\u53ef\u83b7\u5f97\u4e0d\u5305\u542b\u91cd\u590d\u503c\u7684\u65b0\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))\nprint(unique_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5728\u5faa\u73af\u4e2d\u5982\u4f55\u907f\u514d\u5904\u7406\u91cd\u590d\u503c\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u5faa\u73af\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u989d\u5916\u7684\u96c6\u5408\u6765\u8ddf\u8e2a\u5df2\u7ecf\u5904\u7406\u8fc7\u7684\u5143\u7d20\u3002\u6bcf\u6b21\u8fed\u4ee3\u65f6\uff0c\u5148\u68c0\u67e5\u8be5\u5143\u7d20\u662f\u5426\u5728\u96c6\u5408\u4e2d\uff0c\u5982\u679c\u4e0d\u5728\uff0c\u624d\u8fdb\u884c\u5904\u7406\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u96c6\u5408\u4e2d\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">original_list = [1, 2, 2, 3, 4, 4, 5]\nseen = set()\nfor item in original_list:\n    if item not in seen:\n        print(item)  # \u5904\u7406\u4e0d\u91cd\u590d\u7684\u5143\u7d20\n        seen.add(item)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5b57\u5178\u5982\u4f55\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u503c\uff1f<\/strong><br \/>\u5b57\u5178\u7684\u952e\u662f\u552f\u4e00\u7684\u7279\u6027\u4e5f\u53ef\u4ee5\u7528\u6765\u53bb\u9664\u91cd\u590d\u5143\u7d20\u3002\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">original_list = [1, 2, 2, 3, 4, 4, 5]\nunique_dict = {item: None for item in original_list}\nunique_list = list(unique_dict.keys())\nprint(unique_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53bb\u9664\u4e86\u91cd\u590d\u503c\uff0c\u8fd8\u4fdd\u6301\u4e86\u5143\u7d20\u7684\u63d2\u5165\u987a\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u901a\u8fc7\u4e0d\u53d6\u5217\u8868\u4e2d\u7684\u91cd\u590d\u503c\u6765\u8fdb\u884c\u5faa\u73af\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6570\u636e\u7ed3\u6784\u3001\u5b57\u5178\uff08dictionary\uff09 [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1106017"}],"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=1106017"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1106017\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1106017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1106017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1106017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}