{"id":1068634,"date":"2024-12-31T16:45:10","date_gmt":"2024-12-31T08:45:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1068634.html"},"modified":"2024-12-31T16:45:13","modified_gmt":"2024-12-31T08:45:13","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e4%b8%a4%e4%b8%aa%e7%9f%a9%e9%98%b5%e4%b8%a4%e4%ba%a4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1068634.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u4e24\u4e2a\u77e9\u9635\u4e24\u4ea4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/4e8d437f-6019-4e2f-abcc-c2849b779faa.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u4e24\u4e2a\u77e9\u9635\u4e24\u4ea4\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5224\u65ad\u4e24\u4e2a\u77e9\u9635\u662f\u5426\u76f8\u4ea4\uff0c\u5305\u62ec\u77e9\u9635\u5143\u7d20\u7684\u6bd4\u8f83\u3001\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u3001\u4ee5\u53ca\u5224\u65ad\u77e9\u9635\u7684\u5b50\u77e9\u9635\u5173\u7cfb\u7b49\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\uff1a\u76f4\u63a5\u5143\u7d20\u6bd4\u8f83\u3001\u5229\u7528NumPy\u7684\u4ea4\u96c6\u64cd\u4f5c\u3001\u901a\u8fc7\u96c6\u5408\u64cd\u4f5c\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5\uff1a\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u3002<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u5e93\u662fPython\u4e2d\u5904\u7406\u77e9\u9635\u548c\u6570\u7ec4\u7684\u5f3a\u5927\u5de5\u5177\u3002\u901a\u8fc7NumPy\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u77e9\u9635\u8fdb\u884c\u5404\u79cd\u8fd0\u7b97\uff0c\u5305\u62ec\u5224\u65ad\u4e24\u4e2a\u77e9\u9635\u662f\u5426\u76f8\u4ea4\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165NumPy\u5e93\u5e76\u521b\u5efa\u77e9\u9635<\/strong><\/li>\n<li><strong>\u4f7f\u7528NumPy\u7684\u4ea4\u96c6\u64cd\u4f5c<\/strong><\/li>\n<li><strong>\u5224\u65ad\u4ea4\u96c6\u77e9\u9635\u662f\u5426\u4e3a\u7a7a<\/strong><\/li>\n<\/ol>\n<p><h3>\u4e00\u3001\u5bfc\u5165NumPy\u5e93\u5e76\u521b\u5efa\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5bfc\u5165NumPy\u5e93\u5e76\u521b\u5efa\u4e24\u4e2a\u8981\u6bd4\u8f83\u7684\u77e9\u9635\u3002NumPy\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u521b\u5efa\u77e9\u9635\uff0c\u5982\u76f4\u63a5\u8f93\u5165\u6570\u7ec4\u3001\u4f7f\u7528\u51fd\u6570\u751f\u6210\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>matrix2 = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u7684\u4ea4\u96c6\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u51fd\u6570<code>np.intersect1d<\/code>\uff0c\u53ef\u4ee5\u7528\u6765\u627e\u51fa\u4e24\u4e2a\u6570\u7ec4\u7684\u4ea4\u96c6\u3002\u5bf9\u4e8e\u77e9\u9635\uff0c\u53ef\u4ee5\u5148\u5c06\u5176\u5c55\u5e73\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u8fdb\u884c\u4ea4\u96c6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u77e9\u9635\u5c55\u5e73\u4e3a\u4e00\u7ef4\u6570\u7ec4<\/p>\n<p>flat_matrix1 = matrix1.flatten()<\/p>\n<p>flat_matrix2 = matrix2.flatten()<\/p>\n<h2><strong>\u627e\u51fa\u4ea4\u96c6<\/strong><\/h2>\n<p>intersection = np.intersect1d(flat_matrix1, flat_matrix2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5224\u65ad\u4ea4\u96c6\u77e9\u9635\u662f\u5426\u4e3a\u7a7a<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5224\u65ad\u4ea4\u96c6\u77e9\u9635\u662f\u5426\u4e3a\u7a7a\u3002\u5982\u679c\u4ea4\u96c6\u77e9\u9635\u4e3a\u7a7a\uff0c\u8bf4\u660e\u4e24\u4e2a\u77e9\u9635\u6ca1\u6709\u76f8\u4ea4\u7684\u5143\u7d20\uff1b\u5426\u5219\uff0c\u8bf4\u660e\u4e24\u4e2a\u77e9\u9635\u5b58\u5728\u76f8\u4ea4\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5224\u65ad\u4ea4\u96c6\u662f\u5426\u4e3a\u7a7a<\/p>\n<p>if intersection.size == 0:<\/p>\n<p>    print(&quot;\u4e24\u4e2a\u77e9\u9635\u6ca1\u6709\u76f8\u4ea4\u7684\u5143\u7d20&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u4e24\u4e2a\u77e9\u9635\u5b58\u5728\u76f8\u4ea4\u7684\u5143\u7d20&quot;)<\/p>\n<p>    print(&quot;\u4ea4\u96c6\u5143\u7d20\u4e3a\uff1a&quot;, intersection)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6269\u5c55\uff1a\u5176\u4ed6\u5224\u65ad\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528NumPy\u5e93\uff0c\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u65b9\u6cd5\u6765\u5224\u65ad\u4e24\u4e2a\u77e9\u9635\u662f\u5426\u76f8\u4ea4\u3002\u4f8b\u5982\uff0c\u76f4\u63a5\u5143\u7d20\u6bd4\u8f83\u7684\u65b9\u6cd5\u3001\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u7684\u65b9\u6cd5\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u76f4\u63a5\u5143\u7d20\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>\u76f4\u63a5\u9010\u5143\u7d20\u6bd4\u8f83\u4e24\u4e2a\u77e9\u9635\uff0c\u5982\u679c\u53d1\u73b0\u76f8\u540c\u5143\u7d20\u5219\u8bf4\u660e\u77e9\u9635\u76f8\u4ea4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def are_matrices_intersect(matrix1, matrix2):<\/p>\n<p>    for i in range(matrix1.shape[0]):<\/p>\n<p>        for j in range(matrix1.shape[1]):<\/p>\n<p>            if matrix1[i, j] in matrix2:<\/p>\n<p>                return True<\/p>\n<p>    return False<\/p>\n<h2><strong>\u5224\u65ad\u77e9\u9635\u662f\u5426\u76f8\u4ea4<\/strong><\/h2>\n<p>if are_matrices_intersect(matrix1, matrix2):<\/p>\n<p>    print(&quot;\u4e24\u4e2a\u77e9\u9635\u5b58\u5728\u76f8\u4ea4\u7684\u5143\u7d20&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u4e24\u4e2a\u77e9\u9635\u6ca1\u6709\u76f8\u4ea4\u7684\u5143\u7d20&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u96c6\u5408\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u6c42\u96c6\u5408\u7684\u4ea4\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u96c6\u5408<\/p>\n<p>set_matrix1 = set(matrix1.flatten())<\/p>\n<p>set_matrix2 = set(matrix2.flatten())<\/p>\n<h2><strong>\u6c42\u96c6\u5408\u7684\u4ea4\u96c6<\/strong><\/h2>\n<p>intersection_set = set_matrix1.intersection(set_matrix2)<\/p>\n<h2><strong>\u5224\u65ad\u4ea4\u96c6\u662f\u5426\u4e3a\u7a7a<\/strong><\/h2>\n<p>if not intersection_set:<\/p>\n<p>    print(&quot;\u4e24\u4e2a\u77e9\u9635\u6ca1\u6709\u76f8\u4ea4\u7684\u5143\u7d20&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u4e24\u4e2a\u77e9\u9635\u5b58\u5728\u76f8\u4ea4\u7684\u5143\u7d20&quot;)<\/p>\n<p>    print(&quot;\u4ea4\u96c6\u5143\u7d20\u4e3a\uff1a&quot;, intersection_set)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u5224\u65ad\u4e24\u4e2a\u77e9\u9635\u662f\u5426\u76f8\u4ea4\u3002NumPy\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u8fd0\u7b97\u529f\u80fd\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u5b8c\u6210\u77e9\u9635\u6bd4\u8f83\u548c\u4ea4\u96c6\u8ba1\u7b97\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5224\u65ad\u4e24\u4e2a\u77e9\u9635\u662f\u5426\u76f8\u4ea4\uff1f<\/strong><\/p>\n<p>\u5224\u65ad\u4e24\u4e2a\u77e9\u9635\u662f\u5426\u76f8\u4ea4\u901a\u5e38\u6d89\u53ca\u5230\u5143\u7d20\u7684\u6bd4\u8f83\u3002\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u5c06\u4e24\u4e2a\u77e9\u9635\u8f6c\u5316\u4e3a\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528<code>np.intersect1d()<\/code>\u51fd\u6570\u6765\u627e\u5230\u5b83\u4eec\u7684\u4ea4\u96c6\u3002\u5982\u679c\u4ea4\u96c6\u7684\u5927\u5c0f\u5927\u4e8e\u96f6\uff0c\u5219\u8868\u793a\u8fd9\u4e24\u4e2a\u77e9\u9635\u5b58\u5728\u4ea4\u96c6\u3002<\/p>\n<p><strong>Python\u4e2d\u5224\u65ad\u77e9\u9635\u4ea4\u96c6\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528NumPy\u5e93\u662f\u5904\u7406\u77e9\u9635\u8fd0\u7b97\u7684\u9ad8\u6548\u65b9\u5f0f\u3002\u4e3a\u4e86\u5224\u65ad\u4e24\u4e2a\u77e9\u9635\u662f\u5426\u76f8\u4ea4\uff0c\u5efa\u8bae\u5c06\u77e9\u9635\u5c55\u5e73\u4e3a\u4e00\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u5e94\u7528\u4ea4\u96c6\u8fd0\u7b97\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u6548\u7387\u9ad8\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u3002<\/p>\n<p><strong>\u5982\u679c\u4e24\u4e2a\u77e9\u9635\u6ca1\u6709\u4ea4\u96c6\uff0c\u5e94\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><\/p>\n<p>\u5728\u5224\u65ad\u4e24\u4e2a\u77e9\u9635\u6ca1\u6709\u4ea4\u96c6\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u9009\u62e9\u8fd4\u56de\u4e00\u4e2a\u7279\u5b9a\u7684\u503c\u6216\u6d88\u606f\uff0c\u63d0\u793a\u7528\u6237\u8fd9\u4e24\u4e2a\u77e9\u9635\u4e0d\u76f8\u4ea4\u3002\u4e5f\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\uff0c\u4f8b\u5982\u8bb0\u5f55\u65e5\u5fd7\u6216\u8fdb\u884c\u5176\u4ed6\u7684\u8ba1\u7b97\uff0c\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316\u5224\u65ad\u77e9\u9635\u4ea4\u96c6\u7684\u6027\u80fd\uff1f<\/strong><\/p>\n<p>\u4f18\u5316\u6027\u80fd\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5148\u68c0\u67e5\u4e24\u4e2a\u77e9\u9635\u7684\u7ef4\u5ea6\uff0c\u5982\u679c\u5b83\u4eec\u7684\u5927\u5c0f\u5dee\u5f02\u8f83\u5927\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u4e0d\u76f8\u4ea4\u7684\u7ed3\u679c\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u64cd\u4f5c\u6765\u5224\u65ad\u4ea4\u96c6\u901a\u5e38\u6bd4\u76f4\u63a5\u6bd4\u8f83\u6570\u7ec4\u66f4\u9ad8\u6548\uff0c\u56e0\u4e3a\u96c6\u5408\u5728\u67e5\u627e\u65f6\u5177\u6709\u66f4\u597d\u7684\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5224\u65ad\u4e24\u4e2a\u77e9\u9635\u662f\u5426\u76f8\u4ea4\uff0c\u5305\u62ec\u77e9\u9635\u5143\u7d20\u7684\u6bd4\u8f83\u3001\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u3001\u4ee5\u53ca\u5224\u65ad [&hellip;]","protected":false},"author":3,"featured_media":1068648,"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\/1068634"}],"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=1068634"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068634\/revisions"}],"predecessor-version":[{"id":1068653,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068634\/revisions\/1068653"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1068648"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1068634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1068634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1068634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}