{"id":1010174,"date":"2024-12-27T11:18:36","date_gmt":"2024-12-27T03:18:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1010174.html"},"modified":"2024-12-27T11:18:38","modified_gmt":"2024-12-27T03:18:38","slug":"python%e8%af%ad%e8%a8%80%e5%a6%82%e4%bd%95%e5%8e%bb%e9%99%a4%e9%87%8d%e5%a4%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1010174.html","title":{"rendered":"python\u8bed\u8a00\u5982\u4f55\u53bb\u9664\u91cd\u590d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084824\/d1115dd7-5d9d-4799-8179-fb7808c4ef34.webp\" alt=\"python\u8bed\u8a00\u5982\u4f55\u53bb\u9664\u91cd\u590d\" \/><\/p>\n<p><p> <strong>Python\u8bed\u8a00\u53bb\u9664\u91cd\u590d\u7684\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u96c6\u5408\u3001\u5217\u8868\u63a8\u5bfc\u3001\u5b57\u5178\u548cPandas\u5e93\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\u662f\u6700\u7b80\u5355\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u96c6\u5408\u6570\u636e\u7ed3\u6784\u672c\u8eab\u5c31\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u5b58\u5728\uff0c\u8fd9\u6837\u53ef\u4ee5\u8f7b\u677e\u53bb\u9664\u91cd\u590d\u9879\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u548c\u5b57\u5178\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u53bb\u91cd\uff0c\u540c\u65f6\u4fdd\u7559\u539f\u59cb\u6570\u636e\u987a\u5e8f\u3002\u800cPandas\u5e93\u5219\u662f\u5904\u7406\u6570\u636e\u6846\u4e2d\u91cd\u590d\u6570\u636e\u7684\u5229\u5668\uff0c\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002\u63a5\u4e0b\u6765\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\u53bb\u9664\u91cd\u590d<\/p>\n<\/p>\n<p><p>\u96c6\u5408\uff08set\uff09\u662fPython\u4e2d\u4e00\u79cd\u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5177\u6709\u552f\u4e00\u6027\u7279\u6027\uff0c\u8fd9\u4f7f\u5f97\u5b83\u6210\u4e3a\u53bb\u9664\u91cd\u590d\u5143\u7d20\u7684\u7edd\u4f73\u9009\u62e9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u64cd\u4f5c<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\u53bb\u9664\u91cd\u590d\u5143\u7d20\u7684\u57fa\u672c\u6b65\u9aa4\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u8fd9\u6837\u4fbf\u53ef\u4ee5\u81ea\u52a8\u53bb\u9664\u5176\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002\u63a5\u7740\uff0c\u5982\u679c\u9700\u8981\u4fdd\u6301\u539f\u6709\u7684\u5217\u8868\u7ed3\u6784\uff0c\u53ef\u4ee5\u5c06\u96c6\u5408\u518d\u8f6c\u6362\u56de\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_list = list(set(original_list))<\/p>\n<p>print(unique_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\u53bb\u91cd\u7684\u4e00\u4e2a\u6ce8\u610f\u4e8b\u9879\u662f\uff0c\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u6240\u4ee5\u5982\u679c\u9700\u8981\u4fdd\u6301\u539f\u6709\u5217\u8868\u7684\u987a\u5e8f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u5e76\u4e0d\u9002\u5408\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u548c\u5b57\u5178\u53bb\u9664\u91cd\u590d<\/p>\n<\/p>\n<p><p>\u5728Python 3.7\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u7684\u63d2\u5165\u987a\u5e8f\u662f\u6709\u5e8f\u7684\u3002\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u548c\u5b57\u5178\u53ef\u4ee5\u5b9e\u73b0\u53bb\u91cd\u5e76\u4fdd\u6301\u539f\u6709\u987a\u5e8f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5217\u8868\u63a8\u5bfc\u548c\u5b57\u5178<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_list = list(dict.fromkeys(original_list))<\/p>\n<p>print(unique_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u539f\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u7684\u539f\u7406\u662f\u5229\u7528\u5b57\u5178\u7684\u952e\u552f\u4e00\u6027\u6765\u53bb\u9664\u91cd\u590d\u5143\u7d20\uff0c\u540c\u65f6\u5b57\u5178\u4fdd\u6301\u4e86\u63d2\u5165\u987a\u5e8f\uff0c\u4ece\u800c\u4f7f\u5f97\u6700\u7ec8\u7684\u5217\u8868\u662f\u6309\u539f\u59cb\u987a\u5e8f\u6392\u5217\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528Pandas\u5e93\u53bb\u9664\u91cd\u590d<\/p>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002\u5b83\u63d0\u4f9b\u4e86\u4e13\u95e8\u7684\u65b9\u6cd5\u6765\u53bb\u9664\u6570\u636e\u6846\u4e2d\u7684\u91cd\u590d\u884c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u5bfc\u5165Pandas\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\u786e\u4fddPandas\u5e93\u5df2\u5b89\u88c5\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165Pandas\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u53bb\u9664\u91cd\u590d\u884c<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u53bb\u9664\u91cd\u590d\u884c\u53ef\u4ee5\u901a\u8fc7<code>drop_duplicates<\/code>\u65b9\u6cd5\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&#39;Name&#39;: [&#39;Tom&#39;, &#39;Jerry&#39;, &#39;Tom&#39;, &#39;Jerry&#39;, &#39;Spike&#39;],<\/p>\n<p>        &#39;Age&#39;: [20, 21, 20, 22, 23]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df_unique = df.drop_duplicates()<\/p>\n<p>print(df_unique)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u7559\u7279\u5b9a\u5217\u7684\u552f\u4e00\u503c<\/strong><\/p>\n<\/p>\n<p><p><code>drop_duplicates<\/code>\u8fd8\u53ef\u4ee5\u901a\u8fc7<code>subset<\/code>\u53c2\u6570\u6307\u5b9a\u53ea\u5bf9\u7279\u5b9a\u7684\u5217\u8fdb\u884c\u53bb\u91cd\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df_unique_name = df.drop_duplicates(subset=&#39;Name&#39;)<\/p>\n<p>print(df_unique_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528\u5faa\u73af\u53bb\u9664\u91cd\u590d<\/p>\n<\/p>\n<p><p>\u867d\u7136\u4e0a\u9762\u7684\u65b9\u6cd5\u5df2\u7ecf\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u53bb\u9664\u91cd\u590d\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u624b\u52a8\u5b9e\u73b0\u53bb\u91cd\u8fc7\u7a0b\uff0c\u6bd4\u5982\u4e3a\u4e86\u4e86\u89e3\u53bb\u91cd\u7684\u5e95\u5c42\u539f\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u5faa\u73af\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\uff0c\u624b\u52a8\u5b9e\u73b0\u53bb\u91cd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_list = []<\/p>\n<p>for item in original_list:<\/p>\n<p>    if item not in unique_list:<\/p>\n<p>        unique_list.append(item)<\/p>\n<p>print(unique_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u590d\u6742\u60c5\u51b5\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982\u5d4c\u5957\u5217\u8868\u6216\u5b57\u5178\uff09\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u4e00\u6b65\u5d4c\u5957\u5faa\u73af\u6216\u9012\u5f52\u6765\u5b9e\u73b0\u53bb\u91cd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53bb\u9664\u91cd\u590d\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u7ed3\u6784\u3002<strong>\u96c6\u5408\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\uff0c\u5b57\u5178\u65b9\u6cd5\u4fdd\u7559\u987a\u5e8f\uff0cPandas\u65b9\u6cd5\u9002\u5408\u6570\u636e\u5206\u6790\u3002<\/strong> \u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u7406\u89e3\u5176\u5de5\u4f5c\u539f\u7406\u548c\u9002\u7528\u573a\u666f\u90fd\u975e\u5e38\u91cd\u8981\u3002\u901a\u8fc7\u719f\u7ec3\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\u5e76\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u91cd\u590d\u9879\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u8bc6\u522b\u91cd\u590d\u9879\u3002\u96c6\u5408\u4f1a\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u5143\u7d20\uff0c\u56e0\u6b64\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u540e\uff0c\u518d\u5c06\u5176\u8f6c\u6362\u56de\u5217\u8868\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u53bb\u9664\u91cd\u590d\u9879\u3002\u4f8b\u5982\uff1a<code>unique_list = list(set(original_list))<\/code>\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>drop_duplicates()<\/code>\u65b9\u6cd5\u4e5f\u80fd\u8f7b\u677e\u5904\u7406\u91cd\u590d\u6570\u636e\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u79cd\u6570\u636e\u7ed3\u6784\u6700\u9002\u5408\u53bb\u9664\u91cd\u590d\u9879\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u96c6\u5408\u662f\u53bb\u9664\u91cd\u590d\u9879\u7684\u6700\u4f73\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u4fdd\u7559\u91cd\u590d\u5143\u7d20\u3002\u5982\u679c\u9700\u8981\u4fdd\u6301\u5143\u7d20\u7684\u987a\u5e8f\uff0c\u4f7f\u7528<code>collections.OrderedDict<\/code>\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u96c6\u5408\u53ef\u4ee5\u8fbe\u5230\u76f8\u540c\u7684\u6548\u679c\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u539f\u5217\u8868\u5e76\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5728\u65b0\u5217\u8868\u4e2d\u6765\u4fdd\u7559\u552f\u4e00\u9879\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5927\u578b\u6570\u636e\u96c6\u4e2d\u53bb\u9664\u91cd\u590d\u9879\uff1f<\/strong><br \/>\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>DataFrame<\/code>\u6765\u7ba1\u7406\u6570\u636e\u3002\u8c03\u7528<code>drop_duplicates()<\/code>\u65b9\u6cd5\u80fd\u591f\u9ad8\u6548\u53bb\u9664\u91cd\u590d\u884c\u3002\u6b64\u5916\uff0c\u5229\u7528<code>groupby()<\/code>\u51fd\u6570\u4e5f\u80fd\u5728\u5206\u6790\u6570\u636e\u65f6\u66f4\u7075\u6d3b\u5730\u5904\u7406\u91cd\u590d\u9879\uff0c\u8fd9\u5728\u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406\u9636\u6bb5\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bed\u8a00\u53bb\u9664\u91cd\u590d\u7684\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u96c6\u5408\u3001\u5217\u8868\u63a8\u5bfc\u3001\u5b57\u5178\u548cPandas\u5e93\u7b49\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\u662f\u6700\u7b80\u5355\u4e14\u9ad8 [&hellip;]","protected":false},"author":3,"featured_media":1010176,"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\/1010174"}],"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=1010174"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010174\/revisions"}],"predecessor-version":[{"id":1010179,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010174\/revisions\/1010179"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1010176"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1010174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1010174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1010174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}