{"id":1068231,"date":"2024-12-31T16:41:36","date_gmt":"2024-12-31T08:41:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1068231.html"},"modified":"2024-12-31T16:41:38","modified_gmt":"2024-12-31T08:41:38","slug":"python%e5%a6%82%e4%bd%95%e5%bb%ba%e7%ab%8b%e4%b8%80%e4%b8%aa%e7%a9%ba%e9%9b%86%e5%90%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1068231.html","title":{"rendered":"python\u5982\u4f55\u5efa\u7acb\u4e00\u4e2a\u7a7a\u96c6\u5408"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/7fa8d6e4-495d-409f-87c2-372b2dea0cf9.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5efa\u7acb\u4e00\u4e2a\u7a7a\u96c6\u5408\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>set()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u7a7a\u96c6\u5408\u3001\u521b\u5efa\u4e00\u4e2a\u7a7a\u96c6\u5408\u7684\u8bed\u6cd5\u662f\u7b80\u5355\u76f4\u63a5\u7684\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u96c6\u5408\uff08set\uff09\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u96c6\u5408\u3002\u8981\u521b\u5efa\u4e00\u4e2a\u7a7a\u96c6\u5408\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>set()<\/code>\u51fd\u6570\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528<code>{}<\/code>\u521b\u5efa\u7684\u662f\u4e00\u4e2a\u7a7a\u5b57\u5178\uff0c\u800c\u4e0d\u662f\u96c6\u5408<\/strong>\u3002\u56e0\u6b64\uff0c\u786e\u4fdd\u4f7f\u7528<code>set()<\/code>\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u96c6\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u7a7a\u96c6\u5408<\/p>\n<p>empty_set = set()<\/p>\n<p>print(empty_set)  # \u8f93\u51fa: set()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u96c6\u5408\u3001\u96c6\u5408\u7684\u57fa\u672c\u64cd\u4f5c\u548c\u5e38\u89c1\u5e94\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u96c6\u5408\u7684\u57fa\u672c\u64cd\u4f5c<\/h2>\n<\/p>\n<p><h3>1\u3001\u521b\u5efa\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u7684\u521b\u5efa\u53ef\u4ee5\u901a\u8fc7<code>set()<\/code>\u51fd\u6570\u6216\u4f7f\u7528\u82b1\u62ec\u53f7<code>{}<\/code>\u76f4\u63a5\u521b\u5efa\u5305\u542b\u5143\u7d20\u7684\u96c6\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 set() \u51fd\u6570\u521b\u5efa\u96c6\u5408<\/p>\n<p>my_set = set([1, 2, 3, 4, 5])<\/p>\n<p>print(my_set)  # \u8f93\u51fa: {1, 2, 3, 4, 5}<\/p>\n<h2><strong>\u4f7f\u7528\u82b1\u62ec\u53f7\u521b\u5efa\u96c6\u5408<\/strong><\/h2>\n<p>my_set = {1, 2, 3, 4, 5}<\/p>\n<p>print(my_set)  # \u8f93\u51fa: {1, 2, 3, 4, 5}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u63d0\u4f9b\u4e86<code>add()<\/code>\u65b9\u6cd5\u6765\u6dfb\u52a0\u5143\u7d20\uff0c<code>remove()<\/code>\u548c<code>discard()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {1, 2, 3}<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>my_set.add(4)<\/p>\n<p>print(my_set)  # \u8f93\u51fa: {1, 2, 3, 4}<\/p>\n<h2><strong>\u5220\u9664\u5143\u7d20<\/strong><\/h2>\n<p>my_set.remove(3)<\/p>\n<p>print(my_set)  # \u8f93\u51fa: {1, 2, 4}<\/p>\n<h2><strong>\u5220\u9664\u4e0d\u5b58\u5728\u7684\u5143\u7d20\u4e0d\u4f1a\u5f15\u53d1\u9519\u8bef<\/strong><\/h2>\n<p>my_set.discard(5)<\/p>\n<p>print(my_set)  # \u8f93\u51fa: {1, 2, 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u96c6\u5408\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u5177\u6709\u4e00\u7cfb\u5217\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u5982\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6\u548c\u5bf9\u79f0\u5dee\u96c6\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">set1 = {1, 2, 3}<\/p>\n<p>set2 = {3, 4, 5}<\/p>\n<h2><strong>\u4ea4\u96c6<\/strong><\/h2>\n<p>print(set1 &amp; set2)  # \u8f93\u51fa: {3}<\/p>\n<h2><strong>\u5e76\u96c6<\/strong><\/h2>\n<p>print(set1 | set2)  # \u8f93\u51fa: {1, 2, 3, 4, 5}<\/p>\n<h2><strong>\u5dee\u96c6<\/strong><\/h2>\n<p>print(set1 - set2)  # \u8f93\u51fa: {1, 2}<\/p>\n<h2><strong>\u5bf9\u79f0\u5dee\u96c6<\/strong><\/h2>\n<p>print(set1 ^ set2)  # \u8f93\u51fa: {1, 2, 4, 5}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u96c6\u5408\u7684\u5e94\u7528<\/h2>\n<\/p>\n<p><h3>1\u3001\u96c6\u5408\u53bb\u91cd<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u7684\u4e00\u4e2a\u91cd\u8981\u5e94\u7528\u662f\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_list = list(set(my_list))<\/p>\n<p>print(unique_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5143\u7d20\u5b58\u5728\u6027\u68c0\u67e5<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u7684\u53e6\u4e00\u4e2a\u5e38\u89c1\u5e94\u7528\u662f\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u96c6\u5408\u4e2d\uff0c\u56e0\u4e3a\u96c6\u5408\u7684\u67e5\u627e\u6548\u7387\u975e\u5e38\u9ad8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {1, 2, 3, 4, 5}<\/p>\n<p>print(3 in my_set)  # \u8f93\u51fa: True<\/p>\n<p>print(6 in my_set)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u96c6\u5408\u8fd0\u7b97\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u8fd0\u7b97\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7528\u6765\u627e\u51fa\u4e24\u4e2a\u5217\u8868\u7684\u5171\u540c\u5143\u7d20\u3001\u5dee\u5f02\u5143\u7d20\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [4, 5, 6, 7, 8]<\/p>\n<h2><strong>\u5171\u540c\u5143\u7d20<\/strong><\/h2>\n<p>common_elements = set(list1) &amp; set(list2)<\/p>\n<p>print(common_elements)  # \u8f93\u51fa: {4, 5}<\/p>\n<h2><strong>\u5dee\u5f02\u5143\u7d20<\/strong><\/h2>\n<p>difference_elements = set(list1) - set(list2)<\/p>\n<p>print(difference_elements)  # \u8f93\u51fa: {1, 2, 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u96c6\u5408\u7684\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u6709\u7528\u7684\u65b9\u6cd5\uff0c\u5982<code>pop()<\/code>\u3001<code>clear()<\/code>\u3001<code>copy()<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {1, 2, 3, 4, 5}<\/p>\n<h2><strong>\u5f39\u51fa\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>popped_element = my_set.pop()<\/p>\n<p>print(popped_element)  # \u8f93\u51fa: 1\uff08\u5177\u4f53\u8f93\u51fa\u503c\u53ef\u80fd\u4e0d\u540c\uff09<\/p>\n<h2><strong>\u6e05\u7a7a\u96c6\u5408<\/strong><\/h2>\n<p>my_set.clear()<\/p>\n<p>print(my_set)  # \u8f93\u51fa: set()<\/p>\n<h2><strong>\u590d\u5236\u96c6\u5408<\/strong><\/h2>\n<p>new_set = my_set.copy()<\/p>\n<p>print(new_set)  # \u8f93\u51fa: set()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u96c6\u5408\u7684\u9ad8\u7ea7\u5e94\u7528<\/h2>\n<\/p>\n<p><h3>1\u3001\u96c6\u5408\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u96c6\u5408\u4e5f\u652f\u6301\u63a8\u5bfc\u5f0f\uff0c\u7528\u4e8e\u751f\u6210\u96c6\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squared_set = {x  2 for x in range(1, 6)}<\/p>\n<p>print(squared_set)  # \u8f93\u51fa: {1, 4, 9, 16, 25}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4e0d\u53ef\u53d8\u96c6\u5408\uff08frozenset\uff09<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e0d\u53ef\u53d8\u96c6\u5408\u7c7b\u578b<code>frozenset<\/code>\uff0c\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u80fd\u4fee\u6539\uff0c\u53ef\u4ee5\u7528\u4f5c\u5b57\u5178\u7684\u952e\u6216\u96c6\u5408\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">immutable_set = frozenset([1, 2, 3, 4, 5])<\/p>\n<p>print(immutable_set)  # \u8f93\u51fa: frozenset({1, 2, 3, 4, 5})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u96c6\u5408\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u7684\u7ed3\u5408<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u53ef\u4ee5\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u96c6\u5408\u4e0e\u5b57\u5178\u7ed3\u5408<\/p>\n<p>data = {<\/p>\n<p>    &#39;group1&#39;: {1, 2, 3},<\/p>\n<p>    &#39;group2&#39;: {3, 4, 5},<\/p>\n<p>    &#39;group3&#39;: {5, 6, 7}<\/p>\n<p>}<\/p>\n<h2><strong>\u627e\u51fa\u6240\u6709\u7ec4\u7684\u5171\u540c\u5143\u7d20<\/strong><\/h2>\n<p>common_elements = set.intersection(*data.values())<\/p>\n<p>print(common_elements)  # \u8f93\u51fa: {3, 5}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5177\u6709\u9ad8\u6548\u7684\u5143\u7d20\u68c0\u67e5\u3001\u53bb\u91cd\u548c\u96c6\u5408\u8fd0\u7b97\u529f\u80fd\u3002\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>set()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u4e00\u4e2a\u7a7a\u96c6\u5408\uff0c\u5e76\u901a\u8fc7\u4e00\u7cfb\u5217\u65b9\u6cd5\u5bf9\u96c6\u5408\u8fdb\u884c\u64cd\u4f5c\u548c\u5e94\u7528\u3002\u65e0\u8bba\u662f\u5728\u6570\u636e\u5904\u7406\u3001\u5206\u6790\u8fd8\u662f\u5728\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u4e2d\uff0c\u96c6\u5408\u90fd\u80fd\u53d1\u6325\u91cd\u8981\u4f5c\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u4e86\u89e3\u548c\u4f7f\u7528Python\u96c6\u5408\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7a7a\u96c6\u5408\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>set()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u7a7a\u96c6\u5408\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">empty_set = set()\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u5e76\u4e14\u786e\u4fdd\u60a8\u521b\u5efa\u7684\u662f\u4e00\u4e2a\u96c6\u5408\u800c\u4e0d\u662f\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff08\u5982\u5b57\u5178\uff09\u3002<\/p>\n<p><strong>\u7a7a\u96c6\u5408\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u6709\u4f55\u4e0d\u540c\uff1f<\/strong><br \/>\u7a7a\u96c6\u5408\u662f\u4e00\u4e2a\u4e0d\u5305\u542b\u4efb\u4f55\u5143\u7d20\u7684\u96c6\u5408\uff0c\u548c\u7a7a\u5217\u8868\uff08<code>[]<\/code>\uff09\u6216\u7a7a\u5b57\u5178\uff08<code>{}<\/code>\uff09\u4e0d\u540c\u3002\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u96c6\u5408\uff0c\u9002\u5408\u7528\u4e8e\u9700\u8981\u786e\u4fdd\u552f\u4e00\u6027\u7684\u573a\u666f\u3002\u5982\u679c\u60a8\u9700\u8981\u5b58\u50a8\u552f\u4e00\u503c\uff0c\u4f7f\u7528\u96c6\u5408\u4f1a\u6bd4\u5217\u8868\u66f4\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5411\u7a7a\u96c6\u5408\u6dfb\u52a0\u5143\u7d20\uff1f<\/strong><br \/>\u4e00\u65e6\u521b\u5efa\u4e86\u7a7a\u96c6\u5408\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>add()<\/code>\u65b9\u6cd5\u5411\u5176\u4e2d\u6dfb\u52a0\u5143\u7d20\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">empty_set.add(1)\nempty_set.add(2)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728\u7a7a\u96c6\u5408\u4e2d\u9010\u6b65\u6dfb\u52a0\u5143\u7d20\uff0c\u4fdd\u6301\u96c6\u5408\u7684\u552f\u4e00\u6027\u548c\u65e0\u5e8f\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u4e00\u4e2a\u96c6\u5408\u662f\u5426\u4e3a\u7a7a\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u96c6\u5408\u7684\u5927\u5c0f\u3002\u5982\u679c\u96c6\u5408\u7684\u957f\u5ea6\u4e3a\u96f6\uff0c\u5219\u8bf4\u660e\u5b83\u662f\u7a7a\u7684\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">if len(empty_set) == 0:\n    print(&quot;\u96c6\u5408\u662f\u7a7a\u7684&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6709\u6548\uff0c\u80fd\u591f\u5e2e\u52a9\u60a8\u5224\u65ad\u96c6\u5408\u7684\u72b6\u6001\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528set()\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u7a7a\u96c6\u5408\u3001\u521b\u5efa\u4e00\u4e2a\u7a7a\u96c6\u5408\u7684\u8bed\u6cd5\u662f\u7b80\u5355\u76f4\u63a5\u7684\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a \u5728 [&hellip;]","protected":false},"author":3,"featured_media":1068246,"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\/1068231"}],"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=1068231"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068231\/revisions"}],"predecessor-version":[{"id":1068248,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068231\/revisions\/1068248"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1068246"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1068231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1068231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1068231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}