{"id":2744,"date":"2025-03-14T22:23:29","date_gmt":"2025-03-14T14:23:29","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2744"},"modified":"2025-03-14T22:23:29","modified_gmt":"2025-03-14T14:23:29","slug":"python-3-%e9%9b%86%e5%90%88%ef%bc%88set%ef%bc%89%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/14\/python-3-%e9%9b%86%e5%90%88%ef%bc%88set%ef%bc%89%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Python 3 \u96c6\u5408\uff08Set\uff09\u8be6\u89e3"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>1. \u96c6\u5408\uff08Set\uff09\u6982\u8ff0<\/strong><\/h2>\n\n\n\n<p>Python <strong>\u96c6\u5408\uff08Set\uff09\u662f\u4e00\u79cd\u65e0\u5e8f\u3001\u53ef\u53d8\u3001\u5143\u7d20\u552f\u4e00\u7684\u6570\u636e\u7ed3\u6784<\/strong>\uff0c\u7c7b\u4f3c\u4e8e\u6570\u5b66\u4e2d\u7684\u96c6\u5408\u3002\u96c6\u5408\u9002\u7528\u4e8e\u53bb\u91cd\u3001\u5173\u7cfb\u8fd0\u7b97\uff08\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6\u7b49\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u96c6\u5408\u7279\u70b9<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u65e0\u5e8f<\/strong>\uff1a\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u6ca1\u6709\u56fa\u5b9a\u7684\u987a\u5e8f\u3002<\/li>\n\n\n\n<li><strong>\u552f\u4e00\u6027<\/strong>\uff1a\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u4e0d\u5141\u8bb8\u91cd\u590d\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u53d8\u6027<\/strong>\uff1a\u96c6\u5408\u53ef\u4ee5\u8fdb\u884c\u589e\u5220\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\uff1a\u521b\u5efa\u4e00\u4e2a\u96c6\u5408<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5b9a\u4e49\u96c6\u5408\nnumbers = {1, 2, 3, 4, 5}\nprint(numbers)  # {1, 2, 3, 4, 5}\n\n# \u96c6\u5408\u81ea\u52a8\u53bb\u91cd\nnums = {1, 2, 2, 3, 4, 4}\nprint(nums)  # {1, 2, 3, 4}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u521b\u5efa\u96c6\u5408<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff081\uff09\u4f7f\u7528 <code>{}<\/code> \u76f4\u63a5\u521b\u5efa<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>my_set = {1, 2, 3, 4, 5}\nprint(my_set)  # {1, 2, 3, 4, 5}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff082\uff09\u4f7f\u7528 <code>set()<\/code> \u51fd\u6570<\/strong><\/h3>\n\n\n\n<p>\u53ef\u4ee5\u5c06<strong>\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49<\/strong>\u8f6c\u6362\u4e3a\u96c6\u5408\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;1, 2, 3, 3, 4]\nmy_set = set(my_list)\nprint(my_set)  # {1, 2, 3, 4}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff083\uff09\u521b\u5efa\u7a7a\u96c6\u5408<\/strong><\/h3>\n\n\n\n<p>\u26a0 <strong>\u6ce8\u610f\uff1a\u7a7a\u96c6\u5408\u4e0d\u80fd\u7528 <code>{}<\/code>\uff0c\u4f1a\u53d8\u6210\u5b57\u5178<\/strong>\uff0c\u5fc5\u987b\u7528 <code>set()<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>empty_set = set()\nprint(type(empty_set))  # &lt;class 'set'&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u8bbf\u95ee\u96c6\u5408\u5143\u7d20<\/strong><\/h2>\n\n\n\n<p>\u96c6\u5408<strong>\u4e0d\u80fd<\/strong>\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\uff0c\u56e0\u4e3a\u5b83\u662f<strong>\u65e0\u5e8f<\/strong>\u7684\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528 <strong><code>for<\/code> \u5faa\u73af<\/strong>\u904d\u5386\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_set = {1, 2, 3, 4}\nfor item in my_set:\n    print(item)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff081\uff09\u6dfb\u52a0\u5143\u7d20<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>add()<\/code>\uff1a\u5411\u96c6\u5408\u6dfb\u52a0\u5355\u4e2a\u5143\u7d20\u3002<\/li>\n\n\n\n<li><code>update()<\/code>\uff1a\u5411\u96c6\u5408\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\uff08\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>my_set = {1, 2, 3}\nmy_set.add(4)  # \u6dfb\u52a0\u5355\u4e2a\u5143\u7d20\nprint(my_set)  # {1, 2, 3, 4}\n\nmy_set.update(&#91;5, 6, 7])  # \u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\nprint(my_set)  # {1, 2, 3, 4, 5, 6, 7}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff082\uff09\u5220\u9664\u5143\u7d20<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>remove(value)<\/code>\uff1a\u5220\u9664\u6307\u5b9a\u5143\u7d20\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u62a5\u9519\u3002<\/li>\n\n\n\n<li><code>discard(value)<\/code>\uff1a\u5220\u9664\u6307\u5b9a\u5143\u7d20\uff0c\u4e0d\u5b58\u5728\u4e0d\u4f1a\u62a5\u9519\u3002<\/li>\n\n\n\n<li><code>pop()<\/code>\uff1a\u968f\u673a\u5220\u9664\u4e00\u4e2a\u5143\u7d20\uff08\u56e0\u4e3a\u96c6\u5408\u65e0\u5e8f\uff09\u3002<\/li>\n\n\n\n<li><code>clear()<\/code>\uff1a\u6e05\u7a7a\u96c6\u5408\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>my_set = {1, 2, 3, 4, 5}\n\nmy_set.remove(3)  # \u5220\u9664 3\nprint(my_set)  # {1, 2, 4, 5}\n\nmy_set.discard(6)  # \u5220\u9664 6\uff0c\u4e0d\u62a5\u9519\nprint(my_set)  # {1, 2, 4, 5}\n\nelement = my_set.pop()  # \u968f\u673a\u5220\u9664\u4e00\u4e2a\u5143\u7d20\nprint(f\"\u5220\u9664\u4e86 {element}, \u5269\u4f59\u96c6\u5408 {my_set}\")\n\nmy_set.clear()  # \u6e05\u7a7a\u96c6\u5408\nprint(my_set)  # set()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u96c6\u5408\u8fd0\u7b97<\/strong><\/h2>\n\n\n\n<p>Python \u96c6\u5408\u652f\u6301<strong>\u5e76\u96c6\u3001\u4ea4\u96c6\u3001\u5dee\u96c6\u3001\u5bf9\u79f0\u5dee\u96c6\u7b49\u64cd\u4f5c<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff081\uff09\u5e76\u96c6\uff08<code>|<\/code> \u6216 <code>union()<\/code>\uff09<\/strong><\/h3>\n\n\n\n<p>\u5305\u542b\u6240\u6709\u5143\u7d20\uff0c\u53bb\u91cd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set1 = {1, 2, 3}\nset2 = {3, 4, 5}\n\nprint(set1 | set2)  # {1, 2, 3, 4, 5}\nprint(set1.union(set2))  # {1, 2, 3, 4, 5}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff082\uff09\u4ea4\u96c6\uff08<code>&amp;<\/code> \u6216 <code>intersection()<\/code>\uff09<\/strong><\/h3>\n\n\n\n<p>\u53ea\u4fdd\u7559<strong>\u5171\u540c<\/strong>\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(set1 &amp; set2)  # {3}\nprint(set1.intersection(set2))  # {3}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff083\uff09\u5dee\u96c6\uff08<code>-<\/code> \u6216 <code>difference()<\/code>\uff09<\/strong><\/h3>\n\n\n\n<p>\u8fd4\u56de<strong>\u5728 <code>set1<\/code> \u4e2d\u4f46\u4e0d\u5728 <code>set2<\/code> \u4e2d<\/strong>\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(set1 - set2)  # {1, 2}\nprint(set1.difference(set2))  # {1, 2}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff084\uff09\u5bf9\u79f0\u5dee\u96c6\uff08<code>^<\/code> \u6216 <code>symmetric_difference()<\/code>\uff09<\/strong><\/h3>\n\n\n\n<p>\u8fd4\u56de<strong>\u4e0d\u540c\u65f6\u51fa\u73b0\u5728\u4e24\u4e2a\u96c6\u5408\u4e2d\u7684\u5143\u7d20<\/strong>\uff08\u53bb\u6389\u516c\u5171\u90e8\u5206\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(set1 ^ set2)  # {1, 2, 4, 5}\nprint(set1.symmetric_difference(set2))  # {1, 2, 4, 5}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u96c6\u5408\u6bd4\u8f83<\/strong><\/h2>\n\n\n\n<p>\u96c6\u5408\u652f\u6301<strong>\u5b50\u96c6\u3001\u8d85\u96c6\u3001\u76f8\u7b49\u6027\u6bd4\u8f83<\/strong>\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u8bf4\u660e<\/th><th>\u793a\u4f8b<\/th><\/tr><\/thead><tbody><tr><td><code>issubset()<\/code><\/td><td>\u662f\u5426\u662f\u5b50\u96c6<\/td><td><code>set1.issubset(set2)<\/code><\/td><\/tr><tr><td><code>issuperset()<\/code><\/td><td>\u662f\u5426\u662f\u8d85\u96c6<\/td><td><code>set1.issuperset(set2)<\/code><\/td><\/tr><tr><td><code>isdisjoint()<\/code><\/td><td>\u662f\u5426\u65e0\u4ea4\u96c6<\/td><td><code>set1.isdisjoint(set2)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>a = {1, 2, 3}\nb = {1, 2, 3, 4, 5}\n\nprint(a.issubset(b))  # True\uff0ca \u662f b \u7684\u5b50\u96c6\nprint(b.issuperset(a))  # True\uff0cb \u662f a \u7684\u8d85\u96c6\nprint(a.isdisjoint({4, 5}))  # True\uff0ca \u548c {4,5} \u6ca1\u6709\u4ea4\u96c6<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u96c6\u5408\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n\n\n\n<p>\u96c6\u5408\u63a8\u5bfc\u5f0f\uff08Set Comprehension\uff09\u53ef\u4ee5<strong>\u5feb\u901f\u751f\u6210\u96c6\u5408<\/strong>\u3002<\/p>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\uff1a\u521b\u5efa\u5e73\u65b9\u6570\u96c6\u5408<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>squares = {x**2 for x in range(1, 6)}\nprint(squares)  # {1, 4, 9, 16, 25}<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\uff1a\u8fc7\u6ee4\u91cd\u590d\u5143\u7d20<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers = &#91;1, 2, 2, 3, 4, 4, 5]\nunique_numbers = {x for x in numbers}\nprint(unique_numbers)  # {1, 2, 3, 4, 5}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. \u51bb\u7ed3\u96c6\u5408\uff08frozenset\uff09<\/strong><\/h2>\n\n\n\n<p><code>frozenset<\/code> \u662f<strong>\u4e0d\u53ef\u53d8\u7684\u96c6\u5408<\/strong>\uff0c\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u80fd\u4fee\u6539\uff08\u9002\u7528\u4e8e\u5b57\u5178\u7684\u952e\u7b49\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fset = frozenset(&#91;1, 2, 3, 4])\nprint(fset)  # frozenset({1, 2, 3, 4})\n\n# fset.add(5)  # \u274c \u4f1a\u62a5\u9519\uff0cfrozenset \u4e0d\u80fd\u4fee\u6539<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u2705 <strong>\u96c6\u5408\u662f\u65e0\u5e8f\u3001\u552f\u4e00\u7684\u5bb9\u5668<\/strong><br>\u2705 <strong>\u652f\u6301\u5e76\u96c6\u3001\u4ea4\u96c6\u3001\u5dee\u96c6\u3001\u5bf9\u79f0\u5dee\u96c6\u7b49\u64cd\u4f5c<\/strong><br>\u2705 <strong>\u652f\u6301 <code>add()<\/code> \u6dfb\u52a0\uff0c<code>remove()<\/code> \u6216 <code>discard()<\/code> \u5220\u9664<\/strong><br>\u2705 <strong><code>frozenset<\/code> \u662f\u4e0d\u53ef\u53d8\u96c6\u5408<\/strong><br>\u2705 <strong>\u9002\u7528\u4e8e\u53bb\u91cd\u3001\u96c6\u5408\u8fd0\u7b97\u3001\u5feb\u901f\u67e5\u627e<\/strong> \ud83d\ude80<\/p>\n\n\n\n<p><strong>\u96c6\u5408\u5728 Python \u4e2d\u975e\u5e38\u5b9e\u7528\uff0c\u7279\u522b\u662f\u6570\u636e\u53bb\u91cd\u548c\u6570\u5b66\u8fd0\u7b97\uff01<\/strong>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u96c6\u5408\uff08Set\uff09\u6982\u8ff0 Python \u96c6\u5408\uff08Set\uff09\u662f\u4e00\u79cd\u65e0\u5e8f\u3001\u53ef\u53d8\u3001\u5143\u7d20\u552f\u4e00\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8e\u6570\u5b66\u4e2d\u7684\u96c6\u5408 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2745,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2744","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2744"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2744\/revisions"}],"predecessor-version":[{"id":2746,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2744\/revisions\/2746"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2745"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}