{"id":1010940,"date":"2024-12-27T11:25:38","date_gmt":"2024-12-27T03:25:38","guid":{"rendered":""},"modified":"2024-12-27T11:25:40","modified_gmt":"2024-12-27T03:25:40","slug":"python%e5%88%97%e8%a1%a8%e5%a6%82%e4%bd%95%e5%8e%bb%e9%99%a4%e9%87%8d%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1010940.html","title":{"rendered":"python\u5217\u8868\u5982\u4f55\u53bb\u9664\u91cd\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085259\/387f023d-076c-48e5-af9d-14fd63f88756.webp\" alt=\"python\u5217\u8868\u5982\u4f55\u53bb\u9664\u91cd\u91cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4ee5\u53ca\u904d\u5386\u5217\u8868\u521b\u5efa\u65b0\u7684\u5217\u8868\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u96c6\u5408\u5929\u7136\u53bb\u91cd\uff0c\u4f7f\u7528\u7b80\u5355\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u53bb\u91cd\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u96c6\u5408\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u3002\u56e0\u6b64\uff0c\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u518d\u8f6c\u6362\u56de\u5217\u8868\u5373\u53ef\u53bb\u91cd\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u96c6\u5408\u53bb\u91cd<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u518d\u5c06\u96c6\u5408\u8f6c\u6362\u56de\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u4f1a\u6539\u53d8\u539f\u6709\u5217\u8868\u7684\u987a\u5e8f\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)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528\uff0c\u7f3a\u70b9\u662f\u53bb\u91cd\u540e\u65e0\u6cd5\u4fdd\u7559\u539f\u6709\u7684\u5143\u7d20\u987a\u5e8f\u3002\u5982\u679c\u4e0d\u5173\u5fc3\u5143\u7d20\u7684\u987a\u5e8f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u53bb\u91cd<\/p>\n<\/p>\n<p><p>   \u5728Python 3.7\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u5229\u7528\u5b57\u5178\u7684\u952e\u552f\u4e00\u6027\u6765\u53bb\u91cd\uff0c\u540c\u65f6\u4fdd\u6301\u539f\u6709\u987a\u5e8f\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(dict.fromkeys(original_list))<\/p>\n<p>print(unique_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>   \u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53bb\u91cd\uff0c\u8fd8\u4fdd\u7559\u4e86\u539f\u6709\u7684\u5143\u7d20\u987a\u5e8f\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u987a\u5e8f\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>   \u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u904d\u5386\u5217\u8868\uff0c\u5e76\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u6765\u53bb\u9664\u91cd\u590d\u5143\u7d20\u3002\u867d\u7136\u76f8\u5bf9\u590d\u6742\uff0c\u4f46\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u63a7\u5236\u53bb\u91cd\u8fc7\u7a0b\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 = []<\/p>\n<p>[unique_list.append(item) for item in original_list if item not in unique_list]<\/p>\n<p>print(unique_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>   \u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u53bb\u91cd\u8fc7\u7a0b\uff0c\u6bd4\u5982\u5728\u53bb\u91cd\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u5176\u4ed6\u7684\u64cd\u4f5c\u3002\u7f3a\u70b9\u662f\u4ee3\u7801\u76f8\u5bf9\u590d\u6742\uff0c\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528Numpy\u5e93<\/p>\n<\/p>\n<p><p>   \u5bf9\u4e8e\u6570\u503c\u578b\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528Numpy\u5e93\u7684unique\u51fd\u6570\u6765\u53bb\u91cd\u3002Numpy\u7684\u5904\u7406\u901f\u5ea6\u901a\u5e38\u6bd4\u539f\u751fPython\u66f4\u5feb\uff0c\u9002\u5408\u5927\u89c4\u6a21\u6570\u503c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>original_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_list = np.unique(original_list).tolist()<\/p>\n<p>print(unique_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>   \u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6570\u503c\u578b\u5217\u8868\u4e14\u5177\u6709\u9ad8\u6548\u6027\uff0c\u4f46\u9700\u8981\u5b89\u88c5Numpy\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528Pandas\u5e93<\/p>\n<\/p>\n<p><p>   Pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\uff0c\u4e5f\u53ef\u7528\u4e8e\u53bb\u91cd\u3002\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u590d\u6742\u6570\u636e\u7684\u573a\u5408\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>original_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_list = pd.Series(original_list).drop_duplicates().tolist()<\/p>\n<p>print(unique_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>   \u4f7f\u7528Pandas\u8fdb\u884c\u53bb\u91cd\u80fd\u65b9\u4fbf\u5730\u5904\u7406\u66f4\u591a\u6570\u636e\u7c7b\u578b\u548c\u7ed3\u6784\uff0c\u4f46\u9700\u8981\u5b89\u88c5Pandas\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u5982\u679c\u9700\u8981\u7b80\u5355\u9ad8\u6548\u5730\u53bb\u91cd\u4e14\u4e0d\u5173\u5fc3\u987a\u5e8f\uff0c\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u662f\u6700\u597d\u7684\u9009\u62e9\uff1b\u5982\u679c\u9700\u8981\u4fdd\u7559\u987a\u5e8f\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u5b57\u5178\u6216\u8005\u5217\u8868\u63a8\u5bfc\u5f0f\uff1b\u5bf9\u4e8e\u6570\u503c\u578b\u5217\u8868\uff0cNumpy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u53bb\u91cd\u65b9\u6cd5\uff1b\u800c\u5bf9\u4e8e\u590d\u6742\u6570\u636e\uff0cPandas\u5219\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u53bb\u91cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5217\u8868\u53bb\u9664\u91cd\u590d\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>set()<\/code>\u51fd\u6570\u6765\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u4f1a\u81ea\u52a8\u8fc7\u6ee4\u6389\u91cd\u590d\u9879\uff0c\u7136\u540e\u53ef\u4ee5\u518d\u5c06\u5176\u8f6c\u6362\u56de\u5217\u8868\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 2, 3, 4, 4, 5]\nunique_list = list(set(my_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\u4f46\u6ce8\u610f\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u82e5\u987a\u5e8f\u91cd\u8981\uff0c\u53ef\u4f7f\u7528<code>dict.fromkeys()<\/code>\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u53bb\u9664\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u82e5\u5e0c\u671b\u4ece\u5217\u8868\u4e2d\u79fb\u9664\u7279\u5b9a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u3002<code>remove()<\/code>\u65b9\u6cd5\u53ea\u4f1a\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\uff0c\u800c\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u4e0d\u5305\u542b\u6307\u5b9a\u5143\u7d20\u3002\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, 2, 4]\nmy_list.remove(2)  # \u5220\u9664\u7b2c\u4e00\u4e2a2\nprint(my_list)  # \u8f93\u51fa: [1, 3, 2, 4]\n\n# \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\nnew_list = [x for x in my_list if x != 2]\nprint(new_list)  # \u8f93\u51fa: [1, 3, 4]\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u5217\u8868\u4e2d\u53bb\u9664\u7a7a\u503c\u6216None\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u8f7b\u677e\u53bb\u9664\u7a7a\u503c\u6216<code>None<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u9ad8\u6548\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, None, 2, &#39;&#39;, 3, None, 4]\ncleaned_list = [x for x in my_list if x is not None and x != &#39;&#39;]\nprint(cleaned_list)  # \u8f93\u51fa: [1, 2, 3, 4]\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5217\u8868\u4e2d\u53ea\u4fdd\u7559\u6709\u6548\u7684\u975e\u7a7a\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4ee5\u53ca\u904d\u5386\u5217\u8868\u521b\u5efa [&hellip;]","protected":false},"author":3,"featured_media":1010947,"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\/1010940"}],"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=1010940"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010940\/revisions"}],"predecessor-version":[{"id":1010948,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010940\/revisions\/1010948"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1010947"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1010940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1010940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1010940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}