{"id":998154,"date":"2024-12-27T09:30:18","date_gmt":"2024-12-27T01:30:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/998154.html"},"modified":"2024-12-27T09:30:20","modified_gmt":"2024-12-27T01:30:20","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e6%98%af%e5%90%a6%e9%87%8d%e5%a4%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/998154.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u662f\u5426\u91cd\u590d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073701\/6a05e61e-6e88-46f4-a4f0-32f2514076f8.webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u662f\u5426\u91cd\u590d\" \/><\/p>\n<p><p> \u4e00\u3001PYTHON\u5982\u4f55\u5224\u65ad\u662f\u5426\u91cd\u590d<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5224\u65ad\u6570\u636e\u662f\u5426\u91cd\u590d\u7684\u5e38\u7528\u65b9\u6cd5\u6709<strong>\u4f7f\u7528\u96c6\u5408\uff08Set\uff09\u3001\u4f7f\u7528\u5b57\u5178\uff08Dictionary\uff09\u3001\u4f7f\u7528\u5faa\u73af\uff08Loop\uff09<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u96c6\u5408\u662f\u4e00\u4e2a\u65e0\u5e8f\u4e14\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u7684\u6570\u636e\u7ed3\u6784\u3002\u4e3a\u4e86\u66f4\u8be6\u7ec6\u7684\u8bf4\u660e\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u6765\u5b9e\u73b0\u53bb\u91cd\u7684\u529f\u80fd\u3002\u8f6c\u6362\u540e\uff0c\u82e5\u96c6\u5408\u7684\u957f\u5ea6\u5c0f\u4e8e\u539f\u5217\u8868\uff0c\u5219\u8bf4\u660e\u5b58\u5728\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u96c6\u5408\uff08Set\uff09<\/strong><\/li>\n<\/ol>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u5316\u4e3a\u96c6\u5408\u6765\u81ea\u52a8\u53bb\u91cd\uff0c\u4ece\u800c\u5224\u65ad\u662f\u5426\u5b58\u5728\u91cd\u590d\u5143\u7d20\u3002\u4f7f\u7528\u96c6\u5408\u7684\u64cd\u4f5c\u7b80\u5355\u4e14\u9ad8\u6548\uff0c\u662f\u5224\u65ad\u91cd\u590d\u7684\u5e38\u7528\u65b9\u6cd5\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def has_duplicates(input_list):<\/p>\n<p>    return len(input_list) != len(set(input_list))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u9996\u5148\u5c06\u8f93\u5165\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u3002\u7531\u4e8e\u96c6\u5408\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\uff0c\u56e0\u6b64\u5982\u679c\u8f93\u5165\u5217\u8868\u4e2d\u6709\u91cd\u590d\u5143\u7d20\uff0c\u96c6\u5408\u7684\u957f\u5ea6\u5c06\u5c0f\u4e8e\u539f\u59cb\u5217\u8868\u7684\u957f\u5ea6\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6bd4\u8f83\u957f\u5ea6\u6765\u5224\u65ad\u5217\u8868\u4e2d\u662f\u5426\u5b58\u5728\u91cd\u590d\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\uff08Dictionary\uff09<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u662fPython\u4e2d\u7684\u4e00\u79cd\u6620\u5c04\u7c7b\u578b\uff0c\u5b83\u5c06\u952e\u548c\u503c\u5173\u8054\u5728\u4e00\u8d77\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u7edf\u8ba1\u6bcf\u4e2a\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u4ece\u800c\u5224\u65ad\u662f\u5426\u5b58\u5728\u91cd\u590d\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u5b57\u5178\u5e76\u7edf\u8ba1\u9891\u7387<\/strong><\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u4f7f\u7528\u5b57\u5178\u6765\u8bb0\u5f55\u6bcf\u4e2a\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\u3002\u5982\u679c\u67d0\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u5927\u4e8e1\uff0c\u5219\u8868\u793a\u5b58\u5728\u91cd\u590d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def has_duplicates(input_list):<\/p>\n<p>    element_count = {}<\/p>\n<p>    for element in input_list:<\/p>\n<p>        if element in element_count:<\/p>\n<p>            return True<\/p>\n<p>        element_count[element] = 1<\/p>\n<p>    return False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178 <code>element_count<\/code>\u3002\u904d\u5386\u8f93\u5165\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u8be5\u5143\u7d20\u5df2\u7ecf\u5728\u5b57\u5178\u4e2d\uff0c\u8bf4\u660e\u5b58\u5728\u91cd\u590d\uff0c\u51fd\u6570\u8fd4\u56de <code>True<\/code>\u3002\u5982\u679c\u4e0d\u5728\u5b57\u5178\u4e2d\uff0c\u5219\u5c06\u8be5\u5143\u7d20\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff0c\u5e76\u5c06\u5176\u8ba1\u6570\u8bbe\u7f6e\u4e3a1\u3002\u904d\u5386\u7ed3\u675f\u540e\uff0c\u5982\u679c\u6ca1\u6709\u53d1\u73b0\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de <code>False<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5faa\u73af\uff08Loop\uff09<\/p>\n<\/p>\n<p><p>\u867d\u7136\u4f7f\u7528\u96c6\u5408\u548c\u5b57\u5178\u662f\u66f4\u4e3a\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u6765\u5224\u65ad\u91cd\u590d\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u8f83\u5c0f\u7684\u6570\u636e\u96c6\uff0c\u56e0\u4e3a\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n^2)\uff0c\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5d4c\u5957\u5faa\u73af\u67e5\u627e\u91cd\u590d<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u53cc\u91cd\u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u68c0\u67e5\u662f\u5426\u5b58\u5728\u76f8\u540c\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def has_duplicates(input_list):<\/p>\n<p>    for i in range(len(input_list)):<\/p>\n<p>        for j in range(i + 1, len(input_list)):<\/p>\n<p>            if input_list[i] == input_list[j]:<\/p>\n<p>                return True<\/p>\n<p>    return False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e24\u4e2a\u5d4c\u5957\u5faa\u73af\u6765\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u5916\u5c42\u5faa\u73af\u4ece\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u5185\u5c42\u5faa\u73af\u4ece\u5916\u5c42\u5faa\u73af\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u68c0\u67e5\u4e24\u4e2a\u5143\u7d20\u662f\u5426\u76f8\u7b49\u3002\u5982\u679c\u53d1\u73b0\u76f8\u7b49\u7684\u5143\u7d20\uff0c\u5219\u8868\u793a\u5b58\u5728\u91cd\u590d\uff0c\u51fd\u6570\u8fd4\u56de <code>True<\/code>\u3002\u5982\u679c\u904d\u5386\u7ed3\u675f\u540e\u6ca1\u6709\u53d1\u73b0\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de <code>False<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528Counter\u7c7b<\/p>\n<\/p>\n<p><p>Python\u7684<code>collections<\/code>\u6a21\u5757\u4e2d\u63d0\u4f9b\u4e86<code>Counter<\/code>\u7c7b\uff0c\u5b83\u662f\u4e00\u4e2a\u65b9\u4fbf\u7684\u5de5\u5177\u6765\u7edf\u8ba1\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>Counter<\/code>\u6765\u5feb\u901f\u5224\u65ad\u662f\u5426\u5b58\u5728\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528Counter\u7c7b\u7edf\u8ba1\u9891\u7387<\/strong><\/li>\n<\/ol>\n<p><p><code>Counter<\/code>\u7c7b\u53ef\u4ee5\u7528\u4e8e\u7edf\u8ba1\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u82e5\u67d0\u4e2a\u5143\u7d20\u7684\u8ba1\u6570\u5927\u4e8e1\uff0c\u5219\u8868\u793a\u5b58\u5728\u91cd\u590d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def has_duplicates(input_list):<\/p>\n<p>    counter = Counter(input_list)<\/p>\n<p>    for count in counter.values():<\/p>\n<p>        if count &gt; 1:<\/p>\n<p>            return True<\/p>\n<p>    return False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>Counter<\/code>\u7c7b\u521b\u5efa\u4e00\u4e2a\u8ba1\u6570\u5668\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u7edf\u8ba1\u4e86\u8f93\u5165\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002\u7136\u540e\uff0c\u6211\u4eec\u904d\u5386\u8ba1\u6570\u5668\u7684\u503c\uff0c\u5982\u679c\u53d1\u73b0\u67d0\u4e2a\u503c\u5927\u4e8e1\uff0c\u5219\u8868\u793a\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff0c\u51fd\u6570\u8fd4\u56de <code>True<\/code>\u3002\u5982\u679c\u904d\u5386\u7ed3\u675f\u540e\u6ca1\u6709\u53d1\u73b0\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de <code>False<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528pandas\u5e93<\/p>\n<\/p>\n<p><p><code>pandas<\/code>\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528<code>pandas<\/code>\u5e93\u6765\u5224\u65ad\u6570\u636e\u662f\u5426\u91cd\u590d\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528pandas\u5224\u65ad\u91cd\u590d<\/strong><\/li>\n<\/ol>\n<p><p><code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86<code>duplicated<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u6570\u636e\u5e27\u4e2d\u7684\u91cd\u590d\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def has_duplicates(input_list):<\/p>\n<p>    df = pd.DataFrame(input_list, columns=[&#39;values&#39;])<\/p>\n<p>    return df.duplicated().any()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u8f93\u5165\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u4e00\u4e2a<code>pandas<\/code>\u6570\u636e\u5e27\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>duplicated<\/code>\u65b9\u6cd5\u68c0\u67e5\u6570\u636e\u5e27\u4e2d\u7684\u91cd\u590d\u884c\u3002\u5982\u679c\u5b58\u5728\u91cd\u590d\u884c\uff0c<code>duplicated<\/code>\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u578b\u7684\u5e8f\u5217\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u8868\u793a\u5bf9\u5e94\u884c\u662f\u5426\u91cd\u590d\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>any<\/code>\u65b9\u6cd5\u68c0\u67e5\u5e8f\u5217\u4e2d\u662f\u5426\u5b58\u5728<code>True<\/code>\u503c\uff0c\u5982\u679c\u5b58\u5728\u5219\u8868\u793a\u5b58\u5728\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528itertools\u5e93<\/p>\n<\/p>\n<p><p><code>itertools<\/code>\u662fPython\u4e2d\u7684\u4e00\u4e2a\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u7528\u4e8e\u9ad8\u6548\u5faa\u73af\u7684\u8fed\u4ee3\u5668\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528<code>itertools<\/code>\u5e93\u6765\u5224\u65ad\u6570\u636e\u662f\u5426\u91cd\u590d\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528itertools\u5224\u65ad\u91cd\u590d<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7<code>itertools<\/code>\u5e93\u4e2d\u7684<code>combinations<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u5217\u8868\u4e2d\u5143\u7d20\u7684\u6240\u6709\u7ec4\u5408\uff0c\u4ece\u800c\u5224\u65ad\u662f\u5426\u5b58\u5728\u91cd\u590d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import combinations<\/p>\n<p>def has_duplicates(input_list):<\/p>\n<p>    for a, b in combinations(input_list, 2):<\/p>\n<p>        if a == b:<\/p>\n<p>            return True<\/p>\n<p>    return False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>combinations<\/code>\u65b9\u6cd5\u751f\u6210\u8f93\u5165\u5217\u8868\u4e2d\u5143\u7d20\u7684\u6240\u6709\u4e24\u4e24\u7ec4\u5408\u3002\u7136\u540e\uff0c\u904d\u5386\u6bcf\u4e00\u4e2a\u7ec4\u5408\uff0c\u5982\u679c\u53d1\u73b0\u4e24\u4e2a\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u8868\u793a\u5b58\u5728\u91cd\u590d\uff0c\u51fd\u6570\u8fd4\u56de <code>True<\/code>\u3002\u5982\u679c\u904d\u5386\u7ed3\u675f\u540e\u6ca1\u6709\u53d1\u73b0\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de <code>False<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5224\u65ad\u6570\u636e\u662f\u5426\u91cd\u590d\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002<strong>\u4f7f\u7528\u96c6\u5408\u548c\u5b57\u5178\u662f\u5224\u65ad\u91cd\u590d\u7684\u9ad8\u6548\u65b9\u6cd5<\/strong>\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\uff1b<strong>\u4f7f\u7528\u5faa\u73af\u9002\u5408\u5c0f\u6570\u636e\u96c6<\/strong>\uff0c\u4f46\u6548\u7387\u8f83\u4f4e\uff1b<strong>\u4f7f\u7528Counter\u7c7b\u548cpandas\u5e93<\/strong>\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u7684\u5b9e\u73b0\uff1b<strong>\u4f7f\u7528itertools\u5e93<\/strong>\u53ef\u4ee5\u751f\u6210\u7ec4\u5408\u6765\u5224\u65ad\u91cd\u590d\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7684\u89c4\u6a21\u548c\u7279\u6027\uff0c\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5224\u65ad\u662f\u5426\u5b58\u5728\u91cd\u590d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u5217\u8868\u6216\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u91cd\u590d\u9879\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u5feb\u901f\u68c0\u67e5\u5217\u8868\u6216\u5b57\u7b26\u4e32\u4e2d\u7684\u91cd\u590d\u9879\u3002\u901a\u8fc7\u5c06\u539f\u59cb\u5217\u8868\u6216\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u5982\u679c\u96c6\u5408\u7684\u957f\u5ea6\u5c0f\u4e8e\u539f\u59cb\u7684\u957f\u5ea6\uff0c\u8bf4\u660e\u5b58\u5728\u91cd\u590d\u5143\u7d20\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">def has_duplicates(input_list):\n    return len(input_list) != len(set(input_list))\n<\/code><\/pre>\n<p>\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u540c\u6837\u5e94\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u53ea\u9700\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u5217\u8868\u5373\u53ef\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e9b\u5185\u7f6e\u51fd\u6570\u6216\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u91cd\u590d\u503c\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5185\u7f6e\u51fd\u6570\u548c\u6a21\u5757\uff0c\u4f8b\u5982<code>collections.Counter<\/code>\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\u3002\u8fd9\u4f7f\u5f97\u68c0\u67e5\u91cd\u590d\u9879\u53d8\u5f97\u7b80\u5355\u660e\u4e86\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\ndef find_duplicates(input_list):\n    counts = Counter(input_list)\n    return [item for item, count in counts.items() if count &gt; 1]\n<\/code><\/pre>\n<p>\u6b64\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u8bc6\u522b\u91cd\u590d\u9879\uff0c\u8fd8\u80fd\u8fd4\u56de\u6bcf\u4e2a\u9879\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u4e2d\u7684\u91cd\u590d\u9879\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>pandas<\/code>\u5e93\u3002<code>pandas<\/code>\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bc6\u522b\u548c\u5904\u7406\u91cd\u590d\u6570\u636e\u3002\u4f7f\u7528<code>DataFrame.duplicated()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u91cd\u590d\u884c\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = pd.DataFrame({&#39;A&#39;: [1, 2, 2, 3], &#39;B&#39;: [4, 5, 5, 6]})\nduplicates = data[data.duplicated()]\n<\/code><\/pre>\n<p>\u8be5\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5728\u6570\u636e\u5206\u6790\u548c\u6e05\u6d17\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001PYTHON\u5982\u4f55\u5224\u65ad\u662f\u5426\u91cd\u590d \u5728Python\u4e2d\uff0c\u5224\u65ad\u6570\u636e\u662f\u5426\u91cd\u590d\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u4f7f\u7528\u96c6\u5408\uff08Set\uff09\u3001\u4f7f\u7528\u5b57\u5178\uff08 [&hellip;]","protected":false},"author":3,"featured_media":998161,"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\/998154"}],"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=998154"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998154\/revisions"}],"predecessor-version":[{"id":998165,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998154\/revisions\/998165"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/998161"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=998154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=998154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=998154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}