{"id":1006330,"date":"2024-12-27T10:43:26","date_gmt":"2024-12-27T02:43:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1006330.html"},"modified":"2024-12-27T10:43:29","modified_gmt":"2024-12-27T02:43:29","slug":"python%e5%88%97%e8%a1%a8%e5%a6%82%e4%bd%95%e5%8f%96%e4%ba%a4%e9%9b%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1006330.html","title":{"rendered":"python\u5217\u8868\u5982\u4f55\u53d6\u4ea4\u96c6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082504\/3ff688d5-8d79-4046-b7ea-3996df52e979.webp\" alt=\"python\u5217\u8868\u5982\u4f55\u53d6\u4ea4\u96c6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53d6\u5217\u8868\u4ea4\u96c6\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u96c6\u5408(set)\u8f6c\u6362\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684Counter\u7c7b\u3002\u4f7f\u7528\u96c6\u5408\u8f6c\u6362\u65b9\u6cd5\u6700\u4e3a\u76f4\u63a5\u548c\u9ad8\u6548\uff0c\u56e0\u4e3a\u96c6\u5408\u5929\u7136\u652f\u6301\u4ea4\u96c6\u64cd\u4f5c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u96c6\u5408(set)\u8f6c\u6362\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408(set)\u8f6c\u6362\u662fPython\u4e2d\u5904\u7406\u5217\u8868\u4ea4\u96c6\u7684\u5e38\u7528\u6280\u5de7\u3002\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u5229\u7528\u96c6\u5408\u7684\u4ea4\u96c6\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5feb\u901f\u83b7\u53d6\u4e24\u4e2a\u5217\u8868\u7684\u516c\u5171\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u8868\u73b0\u51fa\u8272\uff0c\u56e0\u4e3a\u96c6\u5408\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u4f4e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u96c6\u5408\u8f6c\u6362\u65b9\u6cd5\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5c06\u4e24\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u3002\u96c6\u5408\u662fPython\u4e2d\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5177\u6709\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u7279\u6027\u3002\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u53ef\u4ee5\u81ea\u52a8\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff0c\u5e76\u4e3a\u540e\u7eed\u7684\u4ea4\u96c6\u64cd\u4f5c\u505a\u51c6\u5907\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528\u96c6\u5408\u7684\u4ea4\u96c6\u64cd\u4f5c\u6765\u83b7\u53d6\u4e24\u4e2a\u96c6\u5408\u7684\u516c\u5171\u5143\u7d20\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>&amp;<\/code>\u8fd0\u7b97\u7b26\u6216\u8005\u8c03\u7528<code>intersection()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u96c6\u5408\u7684\u4ea4\u96c6\u64cd\u4f5c\u3002\u8fd9\u4e00\u6b65\u5c06\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u96c6\u5408\uff0c\u5305\u542b\u4e24\u4e2a\u539f\u59cb\u96c6\u5408\u4e2d\u5171\u6709\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5982\u679c\u9700\u8981\u5c06\u7ed3\u679c\u8f6c\u6362\u56de\u5217\u8868\u5f62\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5bf9\u4ea4\u96c6\u7ed3\u679c\u8fdb\u884c\u8f6c\u6362\u3002\u8fd9\u4e00\u6b65\u5728\u9700\u8981\u8fd4\u56de\u5217\u8868\u683c\u5f0f\u7684\u6570\u636e\u65f6\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u5c31\u53ef\u4ee5\u5feb\u901f\u4e14\u9ad8\u6548\u5730\u83b7\u53d6\u4e24\u4e2a\u5217\u8868\u7684\u4ea4\u96c6\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8Python\u4e2d\u53d6\u5217\u8868\u4ea4\u96c6\u7684\u4e0d\u540c\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\u8f6c\u6362\u662f\u5b9e\u73b0\u5217\u8868\u4ea4\u96c6\u7684\u6700\u5e38\u7528\u65b9\u6cd5\u4e4b\u4e00\u3002\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u6570\u636e\u7ed3\u6784\uff0cPython\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u96c6\u5408\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u8f6c\u6362\u4e3a\u96c6\u5408<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5c06\u4e24\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u3002\u96c6\u5408\u7684\u7279\u70b9\u662f\u53bb\u91cd\uff0c\u8fd9\u4e00\u6b65\u53ef\u4ee5\u5e2e\u52a9\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 2, 3, 4]<\/p>\n<p>list2 = [3, 4, 4, 5, 6]<\/p>\n<p>set1 = set(list1)<\/p>\n<p>set2 = set(list2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u83b7\u53d6\u4ea4\u96c6<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u96c6\u5408\u7684\u4ea4\u96c6\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5feb\u901f\u83b7\u53d6\u5217\u8868\u7684\u516c\u5171\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">intersection = set1 &amp; set2<\/p>\n<h2><strong>\u6216\u8005\u4f7f\u7528<\/strong><\/h2>\n<h2><strong>intersection = set1.intersection(set2)<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8f6c\u6362\u4e3a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5c06\u4ea4\u96c6\u7ed3\u679c\u8f6c\u6362\u56de\u5217\u8868\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = list(intersection)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cdPython\u7279\u6709\u7684\u7b80\u6d01\u8bed\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u590d\u6742\u7684\u5217\u8868\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u5217\u8868\u89e3\u6790\u5b9e\u73b0\u4ea4\u96c6<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5217\u8868\u89e3\u6790\uff0c\u53ef\u4ee5\u5728\u4e0d\u8f6c\u6362\u4e3a\u96c6\u5408\u7684\u60c5\u51b5\u4e0b\u76f4\u63a5\u83b7\u53d6\u4ea4\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 2, 3, 4]<\/p>\n<p>list2 = [3, 4, 4, 5, 6]<\/p>\n<p>intersection = [item for item in list1 if item in list2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u7279\u522b\u662f\u5728\u5217\u8868\u8f83\u5927\u65f6\u3002\u56e0\u6b64\uff0c\u5217\u8868\u89e3\u6790\u9002\u5408\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\u7684\u4ea4\u96c6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528collections\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684collections\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u7684Counter\u7c7b\u53ef\u4ee5\u7528\u4e8e\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\uff0c\u5e76\u5b9e\u73b0\u4ea4\u96c6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528Counter\u7c7b<\/h4>\n<\/p>\n<p><p>Counter\u7c7b\u662fcollections\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5b57\u5178\u5b50\u7c7b\uff0c\u7528\u4e8e\u7edf\u8ba1\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>list1 = [1, 2, 2, 3, 4]<\/p>\n<p>list2 = [3, 4, 4, 5, 6]<\/p>\n<p>counter1 = Counter(list1)<\/p>\n<p>counter2 = Counter(list2)<\/p>\n<p>intersection = counter1 &amp; counter2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u83b7\u53d6\u4ea4\u96c6\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7Counter\u7c7b\u7684\u4ea4\u96c6\u64cd\u4f5c\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u4ecd\u7136\u662f\u4e00\u4e2aCounter\u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528<code>elements()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = list(intersection.elements())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u51fd\u6570\u5c01\u88c5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u590d\u7528\u6027\uff0c\u53ef\u4ee5\u5c06\u4ea4\u96c6\u64cd\u4f5c\u5c01\u88c5\u4e3a\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def list_intersection(list1, list2):<\/p>\n<p>    return list(set(list1) &amp; set(list2))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u8be5\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u4e24\u4e2a\u5217\u8868\u7684\u4ea4\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = list_intersection([1, 2, 3], [2, 3, 4])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5217\u8868\u4ea4\u96c6\u64cd\u4f5c\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u8fc7\u6ee4<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u591a\u4e2a\u6570\u636e\u96c6\u8fdb\u884c\u4ea4\u96c6\u64cd\u4f5c\uff0c\u4ee5\u83b7\u53d6\u6ee1\u8db3\u6761\u4ef6\u7684\u516c\u5171\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2. \u6587\u672c\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6587\u672c\u5206\u6790\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ea4\u96c6\u64cd\u4f5c\u8bc6\u522b\u591a\u4e2a\u6587\u672c\u4e2d\u51fa\u73b0\u7684\u5171\u540c\u8bcd\u6c47\u3002<\/p>\n<\/p>\n<p><h4>3. \u96c6\u5408\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u5b66\u548c\u7edf\u8ba1\u5b66\u4e2d\uff0c\u96c6\u5408\u8fd0\u7b97\u662f\u57fa\u672c\u64cd\u4f5c\u4e4b\u4e00\u3002Python\u63d0\u4f9b\u7684\u96c6\u5408\u4ea4\u96c6\u529f\u80fd\u53ef\u4ee5\u7528\u4e8e\u5feb\u901f\u5b9e\u73b0\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u7684\u91cd\u8981\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f18\u5316\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u5c3d\u91cf\u4f7f\u7528\u96c6\u5408\u8fd9\u79cd\u5929\u7136\u652f\u6301\u4ea4\u96c6\u64cd\u4f5c\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4ee5\u964d\u4f4e\u65f6\u95f4\u590d\u6742\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>2. \u51cf\u5c11\u91cd\u590d\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u591a\u4e2a\u4ea4\u96c6\u64cd\u4f5c\u65f6\uff0c\u5c3d\u91cf\u51cf\u5c11\u5bf9\u540c\u4e00\u6570\u636e\u7684\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u9ad8\u6548\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\uff0c\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u6027\u80fd\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b9e\u73b0\u5217\u8868\u4ea4\u96c6\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u96c6\u5408\u8f6c\u6362\u3001\u5217\u8868\u89e3\u6790\u3001collections\u6a21\u5757\u7b49\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u9ad8\u6548\u5730\u5b9e\u73b0\u4ea4\u96c6\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u6570\u636e\u5904\u7406\u3001\u6587\u672c\u5206\u6790\uff0c\u8fd8\u662f\u96c6\u5408\u8fd0\u7b97\uff0c\u5217\u8868\u4ea4\u96c6\u90fd\u662f\u4e00\u4e2a\u5e38\u7528\u4e14\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\uff0c\u8fd8\u80fd\u589e\u5f3a\u5bf9Python\u6570\u636e\u5904\u7406\u80fd\u529b\u7684\u7406\u89e3\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u627e\u5230\u4e24\u4e2a\u5217\u8868\u7684\u4ea4\u96c6\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u627e\u5230\u4e24\u4e2a\u5217\u8868\u7684\u4ea4\u96c6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u5b9e\u73b0\u3002\u5c06\u4e24\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u4f7f\u7528\u96c6\u5408\u7684\u4ea4\u96c6\u8fd0\u7b97\u7b26\uff08&amp;\uff09\u6216<code>intersection()<\/code>\u65b9\u6cd5\uff0c\u5373\u53ef\u5f97\u5230\u5b83\u4eec\u7684\u4ea4\u96c6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">list1 = [1, 2, 3, 4]\nlist2 = [3, 4, 5, 6]\nintersection = list(set(list1) &amp; set(list2))\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>intersection<\/code>\u5c06\u4f1a\u662f<code>[3, 4]<\/code>\uff0c\u5373\u4e24\u4e2a\u5217\u8868\u7684\u5171\u540c\u5143\u7d20\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u5217\u8868\u7684\u4ea4\u96c6\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u96c6\u5408\u4ee5\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u901a\u8fc7\u904d\u5386\u4e00\u4e2a\u5217\u8868\uff0c\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u5728\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u4ece\u800c\u521b\u5efa\u4ea4\u96c6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">list1 = [1, 2, 3, 4]\nlist2 = [3, 4, 5, 6]\nintersection = [item for item in list1 if item in list2]\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u80fd\u5f97\u5230\u76f8\u540c\u7684\u7ed3\u679c\uff0c<code>intersection<\/code>\u5c06\u662f<code>[3, 4]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u5217\u8868\u4ee5\u83b7\u53d6\u4ea4\u96c6\uff1f<\/strong><br \/>\u5982\u679c\u4e24\u4e2a\u5217\u8868\u4e2d\u90fd\u6709\u91cd\u590d\u5143\u7d20\uff0c\u5e76\u4e14\u60a8\u5e0c\u671b\u4fdd\u7559\u8fd9\u4e9b\u91cd\u590d\u9879\uff0c\u53ef\u4ee5\u4f7f\u7528<code>collections.Counter<\/code>\u6765\u8ba1\u7b97\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u7136\u540e\u901a\u8fc7\u6700\u5c0f\u503c\u6765\u51b3\u5b9a\u4ea4\u96c6\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\nlist1 = [1, 2, 2, 3, 4]\nlist2 = [2, 2, 3, 5, 6]\ncounter1 = Counter(list1)\ncounter2 = Counter(list2)\nintersection = list((counter1 &amp; counter2).elements())\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5f97\u5230\u7684<code>intersection<\/code>\u5c06\u4f1a\u662f<code>[2, 2, 3]<\/code>\uff0c\u4fdd\u7559\u4e86\u91cd\u590d\u7684\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53d6\u5217\u8868\u4ea4\u96c6\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u96c6\u5408(set)\u8f6c\u6362\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528collections\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1006340,"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\/1006330"}],"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=1006330"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1006330\/revisions"}],"predecessor-version":[{"id":1006341,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1006330\/revisions\/1006341"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1006340"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1006330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1006330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1006330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}