{"id":1125051,"date":"2025-01-08T19:47:38","date_gmt":"2025-01-08T11:47:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1125051.html"},"modified":"2025-01-08T19:47:40","modified_gmt":"2025-01-08T11:47:40","slug":"python%e5%a6%82%e4%bd%95%e6%89%be%e5%88%b0%e4%b8%a4%e4%b8%aa%e6%95%b0%e7%bb%84%e7%9b%b8%e5%90%8c%e7%9a%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1125051.html","title":{"rendered":"python\u5982\u4f55\u627e\u5230\u4e24\u4e2a\u6570\u7ec4\u76f8\u540c\u7684\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085851\/4103a4c6-47d5-4e8a-a8f4-63838763ecf8.webp\" alt=\"python\u5982\u4f55\u627e\u5230\u4e24\u4e2a\u6570\u7ec4\u76f8\u540c\u7684\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u8981\u627e\u5230\u4e24\u4e2a\u6570\u7ec4\u4e2d\u76f8\u540c\u7684\u6570\u636e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u96c6\u5408\u64cd\u4f5c\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5e93\u51fd\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u96c6\u5408\u64cd\u4f5c\u6700\u4e3a\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002\u96c6\u5408\u64cd\u4f5c\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u5728\u65f6\u95f4\u590d\u6742\u5ea6\u65b9\u9762\u8868\u73b0\u4f18\u8d8a\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u6765\u627e\u5230\u4e24\u4e2a\u6570\u7ec4\u7684\u76f8\u540c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u96c6\u5408\u64cd\u4f5c\u901a\u8fc7\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u4f7f\u7528\u96c6\u5408\u7684\u4ea4\u96c6\u64cd\u4f5c\uff08<code>intersection<\/code>\uff09\uff0c\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u4e24\u4e2a\u6570\u7ec4\u4e2d\u76f8\u540c\u7684\u5143\u7d20\u3002\u96c6\u5408\u7684\u4ea4\u96c6\u64cd\u4f5c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(min(len(a), len(b)))\uff0c\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u8868\u73b0\u5c24\u4e3a\u51fa\u8272\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u627e\u51fa\u76f8\u540c\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u64cd\u4f5c\u662f\u627e\u5230\u4e24\u4e2a\u6570\u7ec4\u76f8\u540c\u6570\u636e\u7684\u6700\u5e38\u7528\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u7684\u8be6\u7ec6\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1. \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5c06\u4e24\u4e2a\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408\u3002\u8fd9\u6837\u53ef\u4ee5\u53bb\u9664\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff0c\u540c\u65f6\u5229\u7528\u96c6\u5408\u7684\u9ad8\u6548\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array1 = [1, 2, 3, 4, 5, 6]<\/p>\n<p>array2 = [4, 5, 6, 7, 8, 9]<\/p>\n<p>set1 = set(array1)<\/p>\n<p>set2 = set(array2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u96c6\u5408\u7684\u4ea4\u96c6\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\u7684\u4ea4\u96c6\u64cd\u4f5c\uff08<code>intersection<\/code>\uff09\u6765\u627e\u5230\u4e24\u4e2a\u96c6\u5408\u7684\u4ea4\u96c6\uff0c\u5373\u4e24\u4e2a\u6570\u7ec4\u4e2d\u76f8\u540c\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">common_elements = set1.intersection(set2)<\/p>\n<p>print(common_elements)  # \u8f93\u51fa: {4, 5, 6}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u96c6\u5408\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u4f4e\uff0c\u9002\u7528\u4e8e\u5927\u6570\u636e\u96c6\u3002<\/li>\n<li><strong>\u7b80\u6d01<\/strong>\uff1a\u4ee3\u7801\u7b80\u5355\u6613\u8bfb\uff0c\u6613\u4e8e\u7ef4\u62a4\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u627e\u51fa\u76f8\u540c\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u6784\u9020\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u5b83\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4e5f\u53ef\u4ee5\u7528\u6765\u627e\u5230\u4e24\u4e2a\u6570\u7ec4\u4e2d\u7684\u76f8\u540c\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u4e00\u4e2a\u6570\u7ec4\uff0c\u5e76\u68c0\u67e5\u5176\u5143\u7d20\u662f\u5426\u5728\u53e6\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array1 = [1, 2, 3, 4, 5, 6]<\/p>\n<p>array2 = [4, 5, 6, 7, 8, 9]<\/p>\n<p>common_elements = [element for element in array1 if element in array2]<\/p>\n<p>print(common_elements)  # \u8f93\u51fa: [4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u6d01<\/strong>\uff1a\u4e00\u884c\u4ee3\u7801\u5373\u53ef\u5b9e\u73b0\u76f8\u540c\u6570\u636e\u7684\u67e5\u627e\u3002<\/li>\n<li><strong>\u76f4\u89c2<\/strong>\uff1a\u6613\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u6548\u7387\u4f4e<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n*m)\uff0c\u4e0d\u9002\u5408\u5904\u7406\u5927\u6570\u636e\u96c6\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5e93\u51fd\u6570\u627e\u51fa\u76f8\u540c\u6570\u636e<\/h3>\n<\/p>\n<p><p>Python\u7684<code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u5176\u4e2d\u7684<code>numpy.intersect1d<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u627e\u5230\u4e24\u4e2a\u6570\u7ec4\u7684\u4ea4\u96c6\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5<code>numpy<\/code>\u5e93<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u5b89\u88c5<code>numpy<\/code>\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528<code>numpy.intersect1d<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.intersect1d<\/code>\u51fd\u6570\u627e\u5230\u4e24\u4e2a\u6570\u7ec4\u7684\u4ea4\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array1 = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<p>array2 = np.array([4, 5, 6, 7, 8, 9])<\/p>\n<p>common_elements = np.intersect1d(array1, array2)<\/p>\n<p>print(common_elements)  # \u8f93\u51fa: [4 5 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a<code>numpy<\/code>\u5e93\u9488\u5bf9\u6570\u7ec4\u64cd\u4f5c\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u6548\u7387\u8f83\u9ad8\u3002<\/li>\n<li><strong>\u529f\u80fd\u5f3a\u5927<\/strong>\uff1a<code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u9002\u5408\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u9700\u6c42\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u5728\u9009\u62e9\u65f6\u9700\u8981\u8003\u8651\u5176\u6027\u80fd\u8868\u73b0\u3002\u4ee5\u4e0b\u662f\u5bf9\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\u7684\u6027\u80fd\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h4>1. \u65f6\u95f4\u590d\u6742\u5ea6<\/h4>\n<\/p>\n<ul>\n<li>\u96c6\u5408\u64cd\u4f5c\uff1aO(min(len(a), len(b)))<\/li>\n<li>\u5217\u8868\u63a8\u5bfc\u5f0f\uff1aO(n*m)<\/li>\n<li><code>numpy.intersect1d<\/code>\uff1aO(n + m)<\/li>\n<\/ul>\n<p><h4>2. \u5b9e\u9645\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import numpy as np<\/p>\n<p>array1 = list(range(100000))<\/p>\n<p>array2 = list(range(50000, 150000))<\/p>\n<h2><strong>\u96c6\u5408\u64cd\u4f5c<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>set1 = set(array1)<\/p>\n<p>set2 = set(array2)<\/p>\n<p>common_elements_set = set1.intersection(set2)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;\u96c6\u5408\u64cd\u4f5c\u65f6\u95f4\uff1a&quot;, end_time - start_time)<\/p>\n<h2><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>common_elements_list = [element for element in array1 if element in array2]<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;\u5217\u8868\u63a8\u5bfc\u5f0f\u65f6\u95f4\uff1a&quot;, end_time - start_time)<\/p>\n<h2><strong>numpy.intersect1d<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>array1_np = np.array(array1)<\/p>\n<p>array2_np = np.array(array2)<\/p>\n<p>common_elements_np = np.intersect1d(array1_np, array2_np)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;numpy.intersect1d\u65f6\u95f4\uff1a&quot;, end_time - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230\u4e0d\u540c\u65b9\u6cd5\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u7684\u6027\u80fd\u8868\u73b0\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u96c6\u5408\u64cd\u4f5c\u548c<code>numpy<\/code>\u5e93\u7684\u8868\u73b0\u8f83\u597d\uff0c\u800c\u5217\u8868\u63a8\u5bfc\u5f0f\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u4f55\u79cd\u65b9\u6cd5\u5e94\u6839\u636e\u5177\u4f53\u60c5\u51b5\u800c\u5b9a\u3002\u5982\u679c\u4f60\u7684\u6570\u636e\u96c6\u8f83\u5c0f\u4e14\u4ee3\u7801\u7b80\u6d01\u6027\u662f\u9996\u8981\u8003\u8651\u56e0\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u5982\u679c\u6570\u636e\u96c6\u8f83\u5927\u4e14\u6027\u80fd\u662f\u5173\u952e\uff0c\u53ef\u4ee5\u9009\u62e9\u96c6\u5408\u64cd\u4f5c\u6216<code>numpy<\/code>\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1. \u5c0f\u6570\u636e\u96c6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5c0f\u6570\u636e\u96c6\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u8db3\u4ee5\u6ee1\u8db3\u9700\u6c42\uff0c\u4e14\u4ee3\u7801\u7b80\u6d01\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array1 = [1, 2, 3, 4, 5, 6]<\/p>\n<p>array2 = [4, 5, 6, 7, 8, 9]<\/p>\n<p>common_elements = [element for element in array1 if element in array2]<\/p>\n<p>print(common_elements)  # \u8f93\u51fa: [4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5927\u6570\u636e\u96c6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6570\u636e\u96c6\uff0c\u96c6\u5408\u64cd\u4f5c\u6216<code>numpy<\/code>\u5e93\u53ef\u4ee5\u63d0\u4f9b\u66f4\u9ad8\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array1 = list(range(100000))<\/p>\n<p>array2 = list(range(50000, 150000))<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c<\/strong><\/h2>\n<p>set1 = set(array1)<\/p>\n<p>set2 = set(array2)<\/p>\n<p>common_elements = set1.intersection(set2)<\/p>\n<p>print(common_elements)<\/p>\n<h2><strong>\u4f7f\u7528numpy\u5e93<\/strong><\/h2>\n<p>array1_np = np.array(array1)<\/p>\n<p>array2_np = np.array(array2)<\/p>\n<p>common_elements_np = np.intersect1d(array1_np, array2_np)<\/p>\n<p>print(common_elements_np)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u627e\u5230\u4e24\u4e2a\u6570\u7ec4\u4e2d\u76f8\u540c\u6570\u636e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u9009\u62e9\uff0c\u5305\u62ec\u96c6\u5408\u64cd\u4f5c\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5e93\u51fd\u6570\u3002<strong>\u96c6\u5408\u64cd\u4f5c\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u8868\u73b0\u5c24\u4e3a\u51fa\u8272\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u4f4e\u4e14\u4ee3\u7801\u7b80\u6d01\u3002<\/strong> \u5217\u8868\u63a8\u5bfc\u5f0f\u9002\u7528\u4e8e\u5c0f\u6570\u636e\u96c6\uff0c\u4ee3\u7801\u7b80\u6d01\u76f4\u89c2\u3002<code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u9002\u5408\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u9700\u6c42\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u6027\u80fd\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u5e76\u627e\u5230\u76f8\u540c\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u8f7b\u677e\u627e\u5230\u4e24\u4e2a\u6570\u7ec4\u4e2d\u76f8\u540c\u7684\u5143\u7d20\u3002\u5c06\u4e24\u4e2a\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u4f7f\u7528\u4ea4\u96c6\u64cd\u4f5c\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u5171\u540c\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">array1 = [1, 2, 3, 4]\narray2 = [3, 4, 5, 6]\ncommon_elements = set(array1) &amp; set(array2)\nprint(common_elements)  # \u8f93\u51fa: {3, 4}\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u627e\u5230\u4e24\u4e2a\u6570\u7ec4\u7684\u5171\u540c\u5143\u7d20\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u7b5b\u9009\u51fa\u4e24\u4e2a\u6570\u7ec4\u4e2d\u7684\u76f8\u540c\u5143\u7d20\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">array1 = [1, 2, 3, 4]\narray2 = [3, 4, 5, 6]\ncommon_elements = [item for item in array1 if item in array2]\nprint(common_elements)  # \u8f93\u51fa: [3, 4]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u8f83\u5c0f\u6570\u7ec4\u65f6\u975e\u5e38\u6709\u6548\uff0c\u4f46\u5728\u9762\u5bf9\u8f83\u5927\u6570\u7ec4\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u4f1a\u4e0b\u964d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u6570\u7ec4\u4ee5\u627e\u5230\u76f8\u540c\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5f53\u6570\u7ec4\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\u65f6\uff0c\u4f7f\u7528\u96c6\u5408\u53ef\u4ee5\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u503c\u5e76\u627e\u5230\u5171\u540c\u5143\u7d20\u3002\u5982\u679c\u9700\u8981\u4fdd\u7559\u91cd\u590d\u5143\u7d20\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528\u8ba1\u6570\u5668\u6216\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\narray1 = [1, 2, 3, 4, 3]\narray2 = [3, 4, 4, 5, 6]\ncounter1 = Counter(array1)\ncounter2 = Counter(array2)\n\ncommon_elements = counter1 &amp; counter2  # \u8ba1\u7b97\u4ea4\u96c6\nprint(list(common_elements.elements()))  # \u8f93\u51fa: [3, 4]\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u83b7\u53d6\u4e24\u4e2a\u6570\u7ec4\u4e2d\u76f8\u540c\u5143\u7d20\u7684\u6240\u6709\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u627e\u5230\u4e24\u4e2a\u6570\u7ec4\u4e2d\u76f8\u540c\u7684\u6570\u636e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u96c6\u5408\u64cd\u4f5c\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5e93\u51fd\u6570\u3002 \u5176\u4e2d\uff0c\u96c6\u5408 [&hellip;]","protected":false},"author":3,"featured_media":1125060,"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\/1125051"}],"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=1125051"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125051\/revisions"}],"predecessor-version":[{"id":1125061,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125051\/revisions\/1125061"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1125060"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1125051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1125051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1125051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}