{"id":3508,"date":"2025-05-28T14:33:32","date_gmt":"2025-06-04T07:29:31","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3508"},"modified":"2025-06-04T14:33:32","modified_gmt":"2025-06-04T07:29:31","slug":"%e5%ad%b8%e7%bf%92python%e4%b8%ad%e7%9a%84set%e5%87%bd%e6%95%b8%ef%bc%9a%e6%8e%8c%e6%8f%a1%e9%9b%86%e5%90%88%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5%e8%88%87%e6%87%89%e7%94%a8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e5%ad%b8%e7%bf%92python%e4%b8%ad%e7%9a%84set%e5%87%bd%e6%95%b8%ef%bc%9a%e6%8e%8c%e6%8f%a1%e9%9b%86%e5%90%88%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5%e8%88%87%e6%87%89%e7%94%a8\/","title":{"rendered":"2025 \u5e74\u6700\u65b0 Python Set() \u51fd\u6578\u6559\u5b78\uff1a\u638c\u63e1\u96c6\u5408\u7684\u61c9\u7528\u8207\u64cd\u4f5c\u6280\u5de7"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, set(), Python \u6559\u5b78, \u96c6\u5408, \u4ea4\u96c6, \u806f\u96c6\"><\/p>\n<h1>Python \u4e2d\u7684 set() \u51fd\u6578<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`set()` \u51fd\u6578\u7528\u65bc\u5efa\u7acb\u4e00\u500b\u7121\u5e8f\u4e14\u4e0d\u91cd\u8907\u7684\u5143\u7d20\u96c6\u5408\u3002\u9019\u500b\u529f\u80fd\u975e\u5e38\u9069\u5408\u7528\u4f86\u8655\u7406\u9700\u8981\u53bb\u9664\u91cd\u8907\u5143\u7d20\u7684\u5834\u666f\u3002`set()` \u51fd\u6578\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u5e8f\u5217\u578b\u5225\uff08\u5982\u5217\u8868\u3001\u5143\u7d44\u3001\u5b57\u4e32\uff09\uff0c\u4e26\u5c07\u5176\u8f49\u63db\u70ba\u4e00\u500b\u96c6\u5408\u3002<\/p>\n<h2>\u4f7f\u7528 set() \u5efa\u7acb\u96c6\u5408<\/h2>\n<p>\u8981\u5efa\u7acb\u4e00\u500b\u96c6\u5408\uff0c\u53ef\u4ee5\u4f7f\u7528 `set()` \u51fd\u6578\uff0c\u4e26\u5c07\u5e8f\u5217\u578b\u5225\u7684\u8cc7\u6599\u50b3\u5165\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5efa\u7acb\u4e00\u500b\u5217\u8868<br \/>\nlist_data = [1, 2, 3, 4, 5]\n<p># \u5c07\u5217\u8868\u8f49\u63db\u70ba\u96c6\u5408<br \/>\nset_data = set(list_data)<\/p>\n<p># \u5370\u51fa\u96c6\u5408<br \/>\nprint(set_data)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\uff1a<\/p>\n<p>&#8220;`python<br \/>\n{1, 2, 3, 4, 5}<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c`set()` \u51fd\u6578\u6210\u529f\u5c07\u539f\u5217\u8868\u8cc7\u6599\u8f49\u63db\u70ba\u96c6\u5408\uff0c\u4e26\u81ea\u52d5\u79fb\u9664\u91cd\u8907\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u82e5\u5217\u8868\u4e2d\u6709\u91cd\u8907\u503c\uff0c\u96c6\u5408\u5c07\u53ea\u4fdd\u7559\u4e00\u500b\u3002<\/p>\n<h2>\u96c6\u5408\u7684\u57fa\u672c\u64cd\u4f5c<\/h2>\n<p>\u96c6\u5408\u7684\u64cd\u4f5c\u4e3b\u8981\u53ef\u4ee5\u5206\u70ba\u5169\u7a2e\uff1a**\u4ea4\u96c6**\uff08intersection\uff09\u548c**\u806f\u96c6**\uff08union\uff09\u3002\u9019\u4e9b\u64cd\u4f5c\u5728\u6578\u64da\u5206\u6790\u548c\u8655\u7406\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h3>\u4ea4\u96c6<\/h3>\n<p>\u4ea4\u96c6\u662f\u6307\u5169\u500b\u96c6\u5408\u4e2d\u5171\u540c\u5b58\u5728\u7684\u5143\u7d20\u3002\u53ef\u4ee5\u4f7f\u7528 `&#038;` \u904b\u7b97\u5b50\u4f86\u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u4ea4\u96c6\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5efa\u7acb\u5169\u500b\u96c6\u5408<br \/>\nset_a = set([1, 2, 3, 4, 5])<br \/>\nset_b = set([3, 4, 5, 6, 7])<\/p>\n<p># \u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u4ea4\u96c6<br \/>\nset_intersection = set_a &#038; set_b<\/p>\n<p># \u5370\u51fa\u4ea4\u96c6<br \/>\nprint(set_intersection)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\uff1a<\/p>\n<p>&#8220;`python<br \/>\n{3, 4, 5}<br \/>\n&#8220;`<\/p>\n<h3>\u806f\u96c6<\/h3>\n<p>\u806f\u96c6\u662f\u6307\u5169\u500b\u96c6\u5408\u4e2d\u6240\u6709\u7684\u5143\u7d20\u3002\u53ef\u4ee5\u4f7f\u7528 `|` \u904b\u7b97\u5b50\u4f86\u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u806f\u96c6\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5efa\u7acb\u5169\u500b\u96c6\u5408<br \/>\nset_a = set([1, 2, 3, 4, 5])<br \/>\nset_b = set([3, 4, 5, 6, 7])<\/p>\n<p># \u53d6\u5f97\u5169\u500b\u96c6\u5408\u7684\u806f\u96c6<br \/>\nset_union = set_a | set_b<\/p>\n<p># \u5370\u51fa\u806f\u96c6<br \/>\nprint(set_union)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\uff1a<\/p>\n<p>&#8220;`python<br \/>\n{1, 2, 3, 4, 5, 6, 7}<br \/>\n&#8220;`<\/p>\n<h2>\u5176\u4ed6\u96c6\u5408\u64cd\u4f5c<\/h2>\n<p>\u9664\u4e86\u4ea4\u96c6\u548c\u806f\u96c6\uff0cPython \u7684\u96c6\u5408\u9084\u652f\u6301\u5176\u4ed6\u591a\u7a2e\u64cd\u4f5c\uff0c\u4f8b\u5982\u5dee\u96c6\uff08difference\uff09\u548c\u5c0d\u7a31\u5dee\u96c6\uff08symmetric difference\uff09\u3002\u4ee5\u4e0b\u662f\u9019\u4e9b\u64cd\u4f5c\u7684\u7c21\u55ae\u5c55\u793a\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5dee\u96c6<br \/>\nset_difference = set_a &#8211; set_b  # set_a \u4e2d\u4e0d\u5728 set_b \u7684\u5143\u7d20<br \/>\nprint(set_difference)  # {1, 2}<\/p>\n<p># \u5c0d\u7a31\u5dee\u96c6<br \/>\nset_sym_diff = set_a ^ set_b  # \u5728 set_a \u6216 set_b \u4e2d\uff0c\u4f46\u4e0d\u5728\u5169\u8005\u4e2d<br \/>\nprint(set_sym_diff)  # {1, 2, 6, 7}<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `set()` \u51fd\u6578\u6642\uff0c\u5982\u679c\u50b3\u5165\u7684\u8cc7\u6599\u578b\u5225\u4e0d\u5408\u9069\uff0c\u5c07\u6703\u5f15\u767c\u932f\u8aa4\u3002\u78ba\u4fdd\u4f60\u50b3\u5165\u7684\u662f\u53ef\u8fed\u4ee3\u7684\u5c0d\u8c61\uff0c\u4f8b\u5982\u5217\u8868\u6216\u5143\u7d44\u3002\u5982\u679c\u4f60\u4e0d\u78ba\u5b9a\u8cc7\u6599\u578b\u5225\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528 `isinstance()` \u51fd\u6578\u9032\u884c\u6aa2\u67e5\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u96c6\u5408\u5728\u8a31\u591a\u5834\u666f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u53bb\u9664\u91cd\u8907\u7684\u8cc7\u6599\u3001\u8a08\u7b97\u4e0d\u540c\u8cc7\u6599\u96c6\u7684\u4ea4\u96c6\u548c\u806f\u96c6\uff0c\u6216\u8005\u5728\u9032\u884c\u6578\u64da\u5206\u6790\u6642\u5feb\u901f\u67e5\u8a62\u5143\u7d20\u7684\u5b58\u5728\u6027\u3002\u5b78\u7fd2\u5982\u4f55\u9748\u6d3b\u904b\u7528\u96c6\u5408\uff0c\u53ef\u4ee5\u8b93\u4f60\u7684 Python \u7a0b\u5f0f\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u5176\u4ed6\u529f\u80fd\u6216\u6280\u8853\uff0c\u6b61\u8fce\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/123456) \u4ee5\u64f4\u5c55\u4f60\u7684\u77e5\u8b58\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Python \u4e2d\u7684 `set()` \u51fd\u6578\u662f\u8655\u7406\u7121\u5e8f\u4e14\u4e0d\u91cd\u8907\u8cc7\u6599\u7684\u5f37\u5927\u5de5\u5177\u3002\u900f\u904e\u4ea4\u96c6\u548c\u806f\u96c6\u7b49\u64cd\u4f5c\uff0c\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u7ba1\u7406\u548c\u5206\u6790\u8cc7\u6599\u96c6\u3002\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u4f60\u638c\u63e1\u96c6\u5408\u7684\u57fa\u672c\u6982\u5ff5\u8207\u61c9\u7528\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u5982\u4f55\u5c07\u4e00\u500b\u5b57\u4e32\u8f49\u63db\u70ba\u96c6\u5408\uff1f**<br \/>\nA1: \u53ef\u4ee5\u4f7f\u7528 `set()` \u51fd\u6578\u76f4\u63a5\u5c07\u5b57\u4e32\u50b3\u5165\uff0c\u4f8b\u5982 `set(&#8220;hello&#8221;)`\uff0c\u9019\u5c07\u8fd4\u56de `{&#8216;h&#8217;, &#8216;e&#8217;, &#8216;l&#8217;, &#8216;o&#8217;}`\uff0c\u6ce8\u610f\u5b57\u6bcd\u6703\u88ab\u8996\u70ba\u7368\u7acb\u7684\u5143\u7d20\u3002<\/p>\n<p>**Q2: \u96c6\u5408\u4e2d\u7684\u5143\u7d20\u662f\u5426\u53ef\u4ee5\u662f\u53ef\u8b8a\u8cc7\u6599\u578b\u5225\uff1f**<br \/>\nA2: \u4e0d\u53ef\u4ee5\u3002\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u5fc5\u9808\u662f\u4e0d\u53ef\u8b8a\u7684\u8cc7\u6599\u578b\u5225\uff0c\u4f8b\u5982\u6574\u6578\u3001\u5b57\u4e32\u548c\u5143\u7d44\uff0c\u800c\u5217\u8868\u548c\u5b57\u5178\u5247\u4e0d\u80fd\u4f5c\u70ba\u96c6\u5408\u7684\u5143\u7d20\u3002<\/p>\n<p>**Q3: \u5728 Python \u4e2d\u5982\u4f55\u6aa2\u67e5\u4e00\u500b\u5143\u7d20\u662f\u5426\u5728\u96c6\u5408\u4e2d\uff1f**<br \/>\nA3: \u53ef\u4ee5\u4f7f\u7528 `in` \u95dc\u9375\u5b57\uff0c\u4f8b\u5982 `if element in my_set:`\uff0c\u9019\u6703\u8fd4\u56de\u5e03\u6797\u503c `True` \u6216 `False`\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u4ee5\u4e0a\u70ba\u512a\u5316\u5f8c\u7684\u5167\u5bb9\uff0c\u78ba\u4fdd\u4e86\u8a9e\u6cd5\u66f4\u65b0\u8207 SEO \u6700\u4f73\u5be6\u8e10\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u4ecb\u7d39Python\u4e2d\u7684Set()\u51fd\u6578\uff0c\u8a73\u7d30\u89e3\u91cb\u96c6\u5408\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u61c9\u7528\u6280\u5de7\uff0c\u8b93\u4f60\u80fd\u5920\u66f4\u6709\u6548\u5730\u4f7f\u7528Set()\u51fd\u6578\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3508","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-UA","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3508"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3508\/revisions"}],"predecessor-version":[{"id":12813,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3508\/revisions\/12813"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}