{"id":965408,"date":"2024-12-27T04:37:40","date_gmt":"2024-12-26T20:37:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/965408.html"},"modified":"2024-12-27T04:37:42","modified_gmt":"2024-12-26T20:37:42","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e9%87%8d%e5%a4%8d%e9%a1%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/965408.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u91cd\u590d\u9879"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181720\/0a5d8e33-70fb-43a6-858d-e4d3dc66c0c9.webp\" alt=\"python\u5982\u4f55\u5220\u9664\u91cd\u590d\u9879\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u96c6\u5408\u3001\u5b57\u5178\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u6cd5\u6765\u5220\u9664\u91cd\u590d\u9879\uff0c\u5176\u4e2d\u96c6\u5408\u662f\u6700\u7b80\u5355\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<\/strong> \u96c6\u5408\uff08set\uff09\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u6570\u636e\u7ed3\u6784\uff0c\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u518d\u8f6c\u6362\u56de\u5217\u8868\uff0c\u5c31\u53ef\u4ee5\u8f7b\u677e\u53bb\u9664\u91cd\u590d\u9879\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\uff08Set\uff09\u5220\u9664\u91cd\u590d\u9879<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u662fPython\u4e2d\u53bb\u9664\u91cd\u590d\u9879\u7684\u6700\u76f4\u63a5\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u96c6\u5408\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u96c6\u5408\uff0c\u56e0\u6b64\uff0c\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u518d\u8f6c\u6362\u56de\u5217\u8868\uff0c\u5c31\u53ef\u4ee5\u53bb\u9664\u91cd\u590d\u9879\u3002<\/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><\/code><\/pre>\n<\/p>\n<p><h4>\u539f\u7406\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u96c6\u5408\u901a\u8fc7\u54c8\u5e0c\u8868\u5b9e\u73b0\uff0c\u63d2\u5165\u5143\u7d20\u65f6\u81ea\u52a8\u53bb\u91cd\u3002\u8f6c\u6362\u5217\u8868\u4e3a\u96c6\u5408\u65f6\uff0c\u91cd\u590d\u7684\u5143\u7d20\u4f1a\u81ea\u52a8\u88ab\u79fb\u9664\u3002\u6700\u540e\uff0c\u5c06\u96c6\u5408\u518d\u8f6c\u6362\u56de\u5217\u8868\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u5220\u9664\u91cd\u590d\u9879\uff08\u4fdd\u7559\u987a\u5e8f\uff09<\/h3>\n<\/p>\n<p><p>\u5728Python 3.7\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u4f1a\u4fdd\u7559\u63d2\u5165\u987a\u5e8f\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u5229\u7528\u5b57\u5178\u7684\u952e\u6765\u53bb\u91cd\uff0c\u5e76\u4fdd\u7559\u539f\u6709\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>unique_list = list(dict.fromkeys(my_list))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u8bf4\u660e<\/h4>\n<\/p>\n<p><p><code>dict.fromkeys()<\/code>\u65b9\u6cd5\u4f1a\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5b57\u5178\u7684\u952e\u662f\u4ece\u5217\u8868\u4e2d\u63d0\u53d6\u7684\uff0c\u503c\u4e3aNone\u3002\u7531\u4e8e\u5b57\u5178\u7684\u952e\u662f\u552f\u4e00\u7684\uff0c\u6240\u4ee5\u91cd\u590d\u7684\u5143\u7d20\u4f1a\u88ab\u81ea\u52a8\u53bb\u9664\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5220\u9664\u91cd\u590d\u9879\uff08\u4fdd\u7559\u987a\u5e8f\uff09<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u53bb\u91cd\u4e14\u4fdd\u7559\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><\/code><\/pre>\n<\/p>\n<p><h4>\u65b9\u6cd5\u89e3\u91ca<\/h4>\n<\/p>\n<ul>\n<li><code>seen<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u8bb0\u5f55\u5df2\u7ecf\u9047\u5230\u7684\u5143\u7d20\u7684\u96c6\u5408\u3002<\/li>\n<li>\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u539f\u59cb\u5217\u8868\uff0c<code>if not (x in seen or seen.add(x))<\/code>\u6761\u4ef6\u5224\u65ad\u5143\u7d20\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u4e8e<code>seen<\/code>\u96c6\u5408\u4e2d\u3002<\/li>\n<li>\u5982\u679c\u5143\u7d20\u672a\u51fa\u73b0\u8fc7\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230<code>seen<\/code>\u4e2d\u5e76\u4fdd\u7559\u5728\u65b0\u5217\u8868\u4e2d\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u7ed3\u5408Pandas\u53bb\u9664\u91cd\u590d\u9879<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7279\u522b\u662f\u6570\u636e\u5206\u6790\u573a\u666f\uff0cPandas\u5e93\u63d0\u4f9b\u4e86\u66f4\u4e3a\u5f3a\u5927\u7684\u5de5\u5177\u3002\u4f8b\u5982\uff0cDataFrame\u5bf9\u8c61\u7684<code>drop_duplicates()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u53bb\u9664\u91cd\u590d\u9879\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;values&#39;: [1, 2, 2, 3, 4, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>unique_df = df.drop_duplicates()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>Pandas\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\uff0c\u7279\u522b\u662f\u5f53\u6570\u636e\u5b58\u50a8\u5728DataFrame\u4e2d\u65f6\u3002<code>drop_duplicates()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u591a\u79cd\u9009\u9879\uff0c\u4f8b\u5982\u6839\u636e\u7279\u5b9a\u5217\u53bb\u91cd\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u53bb\u9664\u91cd\u590d\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5904\u7406\u66f4\u590d\u6742\u7684\u53bb\u91cd\u903b\u8f91\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_duplicates(input_list):<\/p>\n<p>    result = []<\/p>\n<p>    seen = set()<\/p>\n<p>    for item in input_list:<\/p>\n<p>        if item not in seen:<\/p>\n<p>            seen.add(item)<\/p>\n<p>            result.append(item)<\/p>\n<p>    return result<\/p>\n<p>my_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_list = remove_duplicates(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u9002\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u5728\u53bb\u91cd\u8fc7\u7a0b\u4e2d\u6267\u884c\u989d\u5916\u7684\u64cd\u4f5c\uff08\u4f8b\u5982\u8bb0\u5f55\u53bb\u91cd\u8fc7\u7a0b\u3001\u7edf\u8ba1\u7b49\uff09\u65f6\uff0c\u81ea\u5b9a\u4e49\u51fd\u6570\u4f1a\u66f4\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Numpy\u53bb\u91cd<\/h3>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0cNumpy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5176\u4e2d<code>numpy.unique()<\/code>\u53ef\u4ee5\u76f4\u63a5\u53bb\u9664\u91cd\u590d\u9879\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>unique_array = np.unique(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u52bf\u8bf4\u660e<\/h4>\n<\/p>\n<p><p>Numpy\u7684<code>unique()<\/code>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u53bb\u91cd\uff0c\u8fd8\u53ef\u4ee5\u8fd4\u56de\u6392\u5e8f\u540e\u7684\u7ed3\u679c\uff0c\u5e76\u4e14\u5728\u5904\u7406\u5927\u578b\u6570\u503c\u6570\u7ec4\u65f6\u6548\u7387\u5f88\u9ad8\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53bb\u9664\u91cd\u590d\u9879\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u96c6\u5408<\/strong>\u9002\u5408\u5feb\u901f\u53bb\u91cd\uff0c\u4f46\u4e0d\u4fdd\u7559\u987a\u5e8f\u3002<\/li>\n<li><strong>\u5b57\u5178\u548c\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u9002\u5408\u9700\u8981\u4fdd\u7559\u987a\u5e8f\u7684\u573a\u666f\u3002<\/li>\n<li><strong>Pandas<\/strong>\u9002\u5408\u6570\u636e\u5206\u6790\uff0c\u64cd\u4f5cDataFrame\u3002<\/li>\n<li><strong>Numpy<\/strong>\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\uff0c\u5904\u7406\u5927\u578b\u6570\u503c\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><p>\u6839\u636e\u6570\u636e\u89c4\u6a21\u548c\u7ed3\u6784\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e86\u89e3\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u548c\u5c40\u9650\u6027\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u9879\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u9879\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u5229\u7528\u96c6\u5408\uff08set\uff09\uff0c\u56e0\u4e3a\u96c6\u5408\u5929\u7136\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u518d\u8f6c\u6362\u56de\u5217\u8868\u6765\u5b9e\u73b0\u53bb\u91cd\uff1a<code>unique_list = list(set(original_list))<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5927\u6570\u636e\u96c6\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u5220\u9664\u91cd\u590d\u9879\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u4fdd\u6301\u5143\u7d20\u987a\u5e8f\u7684\u53bb\u91cd\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528\u5b57\u5178\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u5728\u904d\u5386\u539f\u59cb\u5217\u8868\u7684\u540c\u65f6\u53bb\u9664\u91cd\u590d\u9879\uff0c\u5e76\u4fdd\u7559\u5143\u7d20\u7684\u987a\u5e8f\u3002\u4f8b\u5982\uff0c<code>unique_list = list(dict.fromkeys(original_list))<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u7279\u522b\u9002\u5408\u9700\u8981\u4fdd\u6301\u539f\u59cb\u987a\u5e8f\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Pandas\u4e2d\u5904\u7406\u91cd\u590d\u6570\u636e\uff1f<\/strong><br \/>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0cPandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u5904\u7406\u91cd\u590d\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528<code>drop_duplicates()<\/code>\u65b9\u6cd5\u6765\u5220\u9664DataFrame\u4e2d\u7684\u91cd\u590d\u884c\u3002\u6b64\u65b9\u6cd5\u8fd8\u5141\u8bb8\u7528\u6237\u6307\u5b9a\u7279\u5b9a\u7684\u5217\u8fdb\u884c\u53bb\u91cd\u64cd\u4f5c\uff0c\u6bd4\u5982<code>df.drop_duplicates(subset=[&#39;column_name&#39;])<\/code>\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u5bf9\u590d\u6742\u6570\u636e\u96c6\u8fdb\u884c\u53bb\u91cd\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u96c6\u5408\u3001\u5b57\u5178\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u6cd5\u6765\u5220\u9664\u91cd\u590d\u9879\uff0c\u5176\u4e2d\u96c6\u5408\u662f\u6700\u7b80\u5355\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002 \u96c6\u5408\uff08set\uff09 [&hellip;]","protected":false},"author":3,"featured_media":965415,"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\/965408"}],"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=965408"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/965408\/revisions"}],"predecessor-version":[{"id":965417,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/965408\/revisions\/965417"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/965415"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=965408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=965408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=965408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}