{"id":1112360,"date":"2025-01-08T17:37:43","date_gmt":"2025-01-08T09:37:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1112360.html"},"modified":"2025-01-08T17:37:59","modified_gmt":"2025-01-08T09:37:59","slug":"python%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e5%a6%82%e4%bd%95%e8%bd%ac%e6%8d%a2%e4%b8%baset","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1112360.html","title":{"rendered":"python\u4e8c\u7ef4\u6570\u7ec4\u5982\u4f55\u8f6c\u6362\u4e3aset"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074422\/73afe3b7-c491-448a-9ac0-0d717d936560.webp\" alt=\"python\u4e8c\u7ef4\u6570\u7ec4\u5982\u4f55\u8f6c\u6362\u4e3aset\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408\uff08set\uff09\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u6838\u5fc3\u601d\u8def\u662f\u5c06\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u63d0\u53d6\u51fa\u6765\uff0c\u653e\u5165\u4e00\u4e2a\u96c6\u5408\u4e2d\u3002<strong>\u76f4\u63a5\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408\u3001\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u3001\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u63d0\u53d6\u5143\u7d20<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array_2d = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u96c6\u5408<\/strong><\/h2>\n<p>result_set = set()<\/p>\n<h2><strong>\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u96c6\u5408\u4e2d<\/strong><\/h2>\n<p>for row in array_2d:<\/p>\n<p>    for element in row:<\/p>\n<p>        result_set.add(element)<\/p>\n<p>print(result_set)<\/p>\n<h2><strong>\u8f93\u51fa: {1, 2, 3, 4, 5, 6, 7, 8, 9}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408\u3002\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5728\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u552f\u4e00\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array_2d = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u76f4\u63a5\u8f6c\u6362<\/strong><\/h2>\n<p>result_set = {element for row in array_2d for element in row}<\/p>\n<p>print(result_set)<\/p>\n<h2><strong>\u8f93\u51fa: {1, 2, 3, 4, 5, 6, 7, 8, 9}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u66f4\u7ec6\u7c92\u5ea6\u63a7\u5236\u7684\u60c5\u51b5\uff0c\u6bd4\u5982\u5904\u7406\u4e0d\u89c4\u5219\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u901a\u8fc7\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\uff0c\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u5c06\u5176\u6dfb\u52a0\u5230\u96c6\u5408\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array_2d = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u96c6\u5408<\/strong><\/h2>\n<p>result_set = set()<\/p>\n<h2><strong>\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u96c6\u5408\u4e2d<\/strong><\/h2>\n<p>for row in array_2d:<\/p>\n<p>    for element in row:<\/p>\n<p>        result_set.add(element)<\/p>\n<p>print(result_set)<\/p>\n<h2><strong>\u8f93\u51fa: {1, 2, 3, 4, 5, 6, 7, 8, 9}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u63d0\u53d6\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u4f18\u96c5\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4ece\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u63d0\u53d6\u5143\u7d20\u5e76\u8f6c\u6362\u4e3a\u96c6\u5408\u7684\u60c5\u51b5\u3002\u5b83\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array_2d = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u63d0\u53d6\u5143\u7d20\u5e76\u8f6c\u6362\u4e3a\u96c6\u5408<\/strong><\/h2>\n<p>result_set = set(element for row in array_2d for element in row)<\/p>\n<p>print(result_set)<\/p>\n<h2><strong>\u8f93\u51fa: {1, 2, 3, 4, 5, 6, 7, 8, 9}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u4e0d\u89c4\u5219\u7684\u4e8c\u7ef4\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e8c\u7ef4\u6570\u7ec4\u4e0d\u603b\u662f\u89c4\u6574\u7684\u3002\u53ef\u80fd\u5b58\u5728\u4e0d\u540c\u957f\u5ea6\u7684\u884c\uff0c\u6216\u8005\u67d0\u4e9b\u884c\u4e3a\u7a7a\u3002\u4ee5\u4e0b\u65b9\u6cd5\u5c55\u793a\u4e86\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u4e0d\u89c4\u5219\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4e0d\u89c4\u5219\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array_2d = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5],<\/p>\n<p>    [6, 7, 8, 9],<\/p>\n<p>    []<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u76f4\u63a5\u8f6c\u6362<\/strong><\/h2>\n<p>result_set = {element for row in array_2d for element in row}<\/p>\n<p>print(result_set)<\/p>\n<h2><strong>\u8f93\u51fa: {1, 2, 3, 4, 5, 6, 7, 8, 9}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u4e8c\u7ef4\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\u3002\u96c6\u5408\u672c\u8eab\u7684\u6027\u8d28\u4fdd\u8bc1\u4e86\u4e0d\u4f1a\u6709\u91cd\u590d\u5143\u7d20\uff0c\u4f46\u4e86\u89e3\u5982\u4f55\u5904\u7406\u91cd\u590d\u5143\u7d20\u4ecd\u7136\u5f88\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array_2d = [<\/p>\n<p>    [1, 2, 2, 3],<\/p>\n<p>    [4, 5, 5, 6],<\/p>\n<p>    [7, 8, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u76f4\u63a5\u8f6c\u6362<\/strong><\/h2>\n<p>result_set = {element for row in array_2d for element in row}<\/p>\n<p>print(result_set)<\/p>\n<h2><strong>\u8f93\u51fa: {1, 2, 3, 4, 5, 6, 7, 8, 9}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u5305\u542b\u975e\u6570\u5b57\u5143\u7d20\u7684\u4e8c\u7ef4\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5e76\u4e0d\u603b\u662f\u6570\u5b57\uff0c\u6709\u65f6\u53ef\u80fd\u662f\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5904\u7406\u5305\u542b\u5b57\u7b26\u4e32\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5305\u542b\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array_2d = [<\/p>\n<p>    [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;],<\/p>\n<p>    [&quot;date&quot;, &quot;elderberry&quot;, &quot;fig&quot;],<\/p>\n<p>    [&quot;grape&quot;, &quot;honeydew&quot;, &quot;kiwi&quot;]<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u76f4\u63a5\u8f6c\u6362<\/strong><\/h2>\n<p>result_set = {element for row in array_2d for element in row}<\/p>\n<p>print(result_set)<\/p>\n<h2><strong>\u8f93\u51fa: {&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;, &#39;elderberry&#39;, &#39;fig&#39;, &#39;grape&#39;, &#39;honeydew&#39;, &#39;kiwi&#39;}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u5305\u542b\u6df7\u5408\u6570\u636e\u7c7b\u578b\u7684\u4e8c\u7ef4\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4e8c\u7ef4\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u591a\u79cd\u6570\u636e\u7c7b\u578b\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5904\u7406\u5305\u542b\u6df7\u5408\u6570\u636e\u7c7b\u578b\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5305\u542b\u6df7\u5408\u6570\u636e\u7c7b\u578b\u7684\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array_2d = [<\/p>\n<p>    [1, &quot;apple&quot;, 3.14],<\/p>\n<p>    [&quot;banana&quot;, 5, &quot;cherry&quot;],<\/p>\n<p>    [7.89, &quot;date&quot;, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u76f4\u63a5\u8f6c\u6362<\/strong><\/h2>\n<p>result_set = {element for row in array_2d for element in row}<\/p>\n<p>print(result_set)<\/p>\n<h2><strong>\u8f93\u51fa: {1, 3.14, 5, 7.89, 9, &#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5c06\u4e8c\u7ef4\u6570\u7ec4\u5143\u7d20\u8f6c\u6362\u4e3a\u5143\u7ec4\u5e76\u52a0\u5165\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u662f\u53ef\u53d8\u6570\u636e\u7c7b\u578b\uff08\u5982\u5217\u8868\uff09\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u4e0d\u53ef\u53d8\u6570\u636e\u7c7b\u578b\uff08\u5982\u5143\u7ec4\uff09\u624d\u80fd\u52a0\u5165\u96c6\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5305\u542b\u5217\u8868\u5143\u7d20\u7684\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array_2d = [<\/p>\n<p>    [[1, 2], [3, 4]],<\/p>\n<p>    [[5, 6], [7, 8]]<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u5c06\u5217\u8868\u5143\u7d20\u8f6c\u6362\u4e3a\u5143\u7ec4\u5e76\u52a0\u5165\u96c6\u5408<\/strong><\/h2>\n<p>result_set = {tuple(element) for row in array_2d for element in row}<\/p>\n<p>print(result_set)<\/p>\n<h2><strong>\u8f93\u51fa: {(1, 2), (3, 4), (5, 6), (7, 8)}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u5305\u542b\u5d4c\u5957\u4e8c\u7ef4\u6570\u7ec4\u7684\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u53ef\u80fd\u8fd8\u5305\u542b\u5d4c\u5957\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5305\u542b\u5d4c\u5957\u4e8c\u7ef4\u6570\u7ec4\u7684\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array_2d = [<\/p>\n<p>    [[1, 2, 3], [4, 5, 6]],<\/p>\n<p>    [[7, 8, 9], [10, 11, 12]]<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u5c06\u5d4c\u5957\u4e8c\u7ef4\u6570\u7ec4\u5143\u7d20\u63d0\u53d6\u5e76\u52a0\u5165\u96c6\u5408<\/strong><\/h2>\n<p>result_set = {element for sublist in array_2d for row in sublist for element in row}<\/p>\n<p>print(result_set)<\/p>\n<h2><strong>\u8f93\u51fa: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u6027\u80fd\u4f18\u5316\u4e0e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u7684\u4e8c\u7ef4\u6570\u7ec4\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u81f3\u5173\u91cd\u8981\u3002\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548c\u5c3d\u91cf\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5927\u89c4\u6a21\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>import random<\/p>\n<p>array_2d = [[random.randint(0, 100) for _ in range(1000)] for _ in range(1000)]<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u8fdb\u884c\u4f18\u5316<\/strong><\/h2>\n<p>result_set = set(element for row in array_2d for element in row)<\/p>\n<p>print(len(result_set))<\/p>\n<h2><strong>\u8f93\u51fa\u96c6\u5408\u7684\u5927\u5c0f\uff0c\u9a8c\u8bc1\u6027\u80fd<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5c06\u5404\u79cd\u7c7b\u578b\u548c\u7ed3\u6784\u7684\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b8c\u6210\u6570\u636e\u8f6c\u6362\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408\uff1f<\/strong><\/p>\n<p>\u8981\u5c06Python\u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\uff08\u5217\u8868\u7684\u5217\u8868\uff09\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u3002\u901a\u8fc7\u904d\u5386\u6bcf\u4e00\u4e2a\u5185\u90e8\u5217\u8868\uff0c\u5c06\u5176\u5143\u7d20\u6dfb\u52a0\u5230\u4e00\u4e2a\u65b0\u7684\u96c6\u5408\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u53bb\u9664\u91cd\u590d\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4 <code>array = [[1, 2, 2], [3, 4], [5, 1]]<\/code>\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5c06\u5176\u8f6c\u6362\u4e3a\u96c6\u5408\uff1a<\/p>\n<pre><code class=\"language-python\">array = [[1, 2, 2], [3, 4], [5, 1]]\nresult_set = {element for sublist in array for element in sublist}\n<\/code><\/pre>\n<p>\u8fd0\u884c\u6b64\u4ee3\u7801\u540e\uff0c<code>result_set<\/code> \u5c06\u5305\u542b <code>{1, 2, 3, 4, 5}<\/code>\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u4e8c\u7ef4\u6570\u7ec4\u5230\u96c6\u5408\u7684\u8f6c\u6362\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u96c6\u5408\u63a8\u5bfc\u5f0f\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>set()<\/code> \u51fd\u6570\u4e0e\u5217\u8868\u89e3\u6790\u7ed3\u5408\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u6548\u679c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">array = [[1, 2, 2], [3, 4], [5, 1]]\nresult_set = set(item for sublist in array for item in sublist)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u540c\u6837\u80fd\u591f\u53bb\u9664\u91cd\u590d\u9879\uff0c\u5e76\u5c06\u6240\u6709\u5143\u7d20\u6dfb\u52a0\u5230\u4e00\u4e2a\u96c6\u5408\u4e2d\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u7684\u96c6\u5408\u5982\u4f55\u4f7f\u7528\uff1f<\/strong><\/p>\n<p>\u8f6c\u6362\u540e\u7684\u96c6\u5408\u53ef\u4ee5\u7528\u4e8e\u591a\u79cd\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u68c0\u67e5\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u5728\u96c6\u5408\u4e2d\uff0c\u56e0\u4e3a\u96c6\u5408\u67e5\u627e\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\u3002\u6b64\u5916\uff0c\u96c6\u5408\u8fd8\u652f\u6301\u5e76\u96c6\u3001\u4ea4\u96c6\u548c\u5dee\u96c6\u7b49\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002<\/p>\n<pre><code class=\"language-python\">if 3 in result_set:\n    print(&quot;3 is in the set.&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408\u5e76\u5229\u7528\u96c6\u5408\u7684\u7279\u6027\u8fdb\u884c\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408\uff08set\uff09\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u6838\u5fc3\u601d\u8def\u662f\u5c06\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u63d0\u53d6\u51fa\u6765 [&hellip;]","protected":false},"author":3,"featured_media":1112385,"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\/1112360"}],"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=1112360"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112360\/revisions"}],"predecessor-version":[{"id":1112389,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112360\/revisions\/1112389"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1112385"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1112360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1112360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1112360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}