{"id":2848,"date":"2025-05-30T17:28:25","date_gmt":"2025-06-04T09:36:24","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2848"},"modified":"2025-06-04T17:28:25","modified_gmt":"2025-06-04T09:36:24","slug":"%e4%bd%bf%e7%94%a8python%e7%9a%84set%e5%87%bd%e6%95%b8%e5%b0%87%e5%ba%8f%e5%88%97%e8%bd%89%e6%8f%9b%e7%82%ba%e9%9b%86%e5%90%88","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%bd%bf%e7%94%a8python%e7%9a%84set%e5%87%bd%e6%95%b8%e5%b0%87%e5%ba%8f%e5%88%97%e8%bd%89%e6%8f%9b%e7%82%ba%e9%9b%86%e5%90%88\/","title":{"rendered":"2025 \u6700\u65b0\u7248 Python \u7a0b\u5f0f\u6559\u5b78\uff1a\u4f7f\u7528 set() \u51fd\u6578\u5c07\u5e8f\u5217\u8f49\u63db\u70ba\u96c6\u5408"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, set(), Python \u7a0b\u5f0f\u6559\u5b78, \u96c6\u5408\u64cd\u4f5c\"><\/p>\n<h1>2025 \u6700\u65b0\u7248 Python \u7a0b\u5f0f\u6559\u5b78\uff1a\u4f7f\u7528 set() \u51fd\u6578\u5c07\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u5e8f\u5217\u8f49\u63db\u70ba\u96c6\u5408<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`set()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5c07\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u5e8f\u5217\u8f49\u63db\u70ba\u96c6\u5408\u3002\u96c6\u5408\u662f\u4e00\u7a2e\u4e0d\u91cd\u8907\u7684\u5143\u7d20\u96c6\u5408\uff0c\u975e\u5e38\u9069\u5408\u7528\u65bc\u5132\u5b58\u548c\u64cd\u4f5c\u4e0d\u91cd\u8907\u7684\u5143\u7d20\uff0c\u4e26\u4e14\u652f\u6301\u8a31\u591a\u5f37\u5927\u7684\u96c6\u5408\u904b\u7b97\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u96c6\u5408\uff1f<\/h2>\n<p>\u96c6\u5408\u662f\u4e00\u7a2e\u4e0d\u91cd\u8907\u5143\u7d20\u7684\u96c6\u5408\uff0c\u5b83\u53ef\u4ee5\u7528\u4f86\u5132\u5b58\u4e0d\u91cd\u8907\u7684\u8cc7\u6599\u3002\u96c6\u5408\u7684\u4e3b\u8981\u7279\u6027\u5305\u62ec\uff1a<br \/>\n&#8211; \u7121\u5e8f\u6027\uff1a\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u6c92\u6709\u56fa\u5b9a\u7684\u9806\u5e8f\u3002<br \/>\n&#8211; \u4e0d\u91cd\u8907\u6027\uff1a\u96c6\u5408\u4e0d\u5141\u8a31\u91cd\u8907\u7684\u5143\u7d20\u3002<br \/>\n\u96c6\u5408\u53ef\u4ee5\u7528\u65bc\u8a31\u591a\u6709\u8da3\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u6c42\u5169\u500b\u96c6\u5408\u7684\u4ea4\u96c6\u3001\u806f\u96c6\u548c\u5dee\u96c6\u3002<\/p>\n<h2>\u4f7f\u7528 Python \u7684 set() \u51fd\u6578<\/h2>\n<p>`set()` \u51fd\u6578\u53ef\u4ee5\u5c07\u4efb\u4f55\u53ef\u8fed\u4ee3\u7684\u5c0d\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7d44\u3001\u5b57\u5178\u7b49\uff09\u8f49\u63db\u70ba\u96c6\u5408\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u9ad4\u7684\u7bc4\u4f8b\uff1a<\/p>\n<h3>\u5c07\u5217\u8868\u8f49\u63db\u70ba\u96c6\u5408<\/h3>\n<pre class=\"brush: python\">\n# \u5c07\u5217\u8868\u8f49\u63db\u70ba\u96c6\u5408\nlist1 = [1, 2, 3, 4, 5, 5]\nset1 = set(list1)\nprint(set1)  # \u8f38\u51fa\uff1a{1, 2, 3, 4, 5}\n<\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u5373\u4f7f\u5217\u8868\u4e2d\u6709\u91cd\u8907\u7684\u6578\u5b575\uff0c\u8f49\u63db\u5f8c\u7684\u96c6\u5408\u53ea\u4fdd\u7559\u4e00\u500b5\u3002<\/p>\n<h3>\u5c07\u5143\u7d44\u8f49\u63db\u70ba\u96c6\u5408<\/h3>\n<pre class=\"brush: python\">\n# \u5c07\u5143\u7d44\u8f49\u63db\u70ba\u96c6\u5408\ntuple1 = (1, 2, 3, 4, 5)\nset2 = set(tuple1)\nprint(set2)  # \u8f38\u51fa\uff1a{1, 2, 3, 4, 5}\n<\/pre>\n<h3>\u5c07\u5b57\u5178\u8f49\u63db\u70ba\u96c6\u5408<\/h3>\n<pre class=\"brush: python\">\n# \u5c07\u5b57\u5178\u8f49\u63db\u70ba\u96c6\u5408\ndict1 = {'a': 1, 'b': 2, 'c': 3}\nset3 = set(dict1)\nprint(set3)  # \u8f38\u51fa\uff1a{'a', 'b', 'c'}\n<\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u53ea\u6709\u5b57\u5178\u7684\u9375\u88ab\u8f49\u63db\u70ba\u96c6\u5408\uff0c\u503c\u5247\u88ab\u5ffd\u7565\u3002<\/p>\n<h2>\u96c6\u5408\u7684\u5e38\u898b\u64cd\u4f5c<\/h2>\n<p>\u96c6\u5408\u652f\u6301\u591a\u7a2e\u64cd\u4f5c\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f8b\u5b50\uff1a<\/p>\n<h3>\u4ea4\u96c6<\/h3>\n<pre class=\"brush: python\">\nsetA = {1, 2, 3}\nsetB = {2, 3, 4}\nintersection = setA & setB\nprint(intersection)  # \u8f38\u51fa\uff1a{2, 3}\n<\/pre>\n<h3>\u806f\u96c6<\/h3>\n<pre class=\"brush: python\">\nunion = setA | setB\nprint(union)  # \u8f38\u51fa\uff1a{1, 2, 3, 4}\n<\/pre>\n<h3>\u5dee\u96c6<\/h3>\n<pre class=\"brush: python\">\ndifference = setA - setB\nprint(difference)  # \u8f38\u51fa\uff1a{1}\n<\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `set()` \u51fd\u6578\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<br \/>\n&#8211; \u5617\u8a66\u5c07\u4e0d\u53ef\u8fed\u4ee3\u5c0d\u8c61\u50b3\u5165 `set()`\uff0c\u9019\u5c07\u5f15\u767c\u932f\u8aa4\u3002<br \/>\n&#8211; \u5ffd\u8996\u96c6\u5408\u7684\u7121\u5e8f\u6027\uff0c\u5c0e\u81f4\u8aa4\u89e3\u96c6\u5408\u7684\u5167\u5bb9\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u60a8\u53ef\u4ee5\u5229\u7528\u96c6\u5408\u4f86\u9032\u884c\u6578\u64da\u53bb\u91cd\u3001\u8a08\u7b97\u91cd\u8907\u5143\u7d20\u7b49\u61c9\u7528\uff0c\u4f8b\u5982\u5728\u6578\u64da\u5206\u6790\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\u4f86\u5feb\u901f\u627e\u51fa\u7368\u7279\u503c\u3002<\/p>\n<p>\u5982\u9700\u66f4\u6df1\u5165\u7684 Python \u6559\u5b78\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003 [\u9019\u88e1\u7684\u6587\u7ae0](https:\/\/vocus.cc) \u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u7684 `set()` \u51fd\u6578\u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u4fbf\u7684\u65b9\u5f0f\u4f86\u5c07\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u5e8f\u5217\u8f49\u63db\u70ba\u96c6\u5408\uff0c\u4e26\u4e14\u652f\u6301\u591a\u7a2e\u96c6\u5408\u64cd\u4f5c\u3002\u7121\u8ad6\u662f\u6578\u64da\u8655\u7406\u9084\u662f\u7b97\u6cd5\u5be6\u4f5c\uff0c\u96c6\u5408\u90fd\u662f\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: set() \u51fd\u6578\u80fd\u8655\u7406\u54ea\u4e9b\u6578\u64da\u985e\u578b\uff1f<\/h3>\n<p>A1: `set()` \u51fd\u6578\u53ef\u4ee5\u8655\u7406\u4efb\u4f55\u53ef\u8fed\u4ee3\u7684\u5c0d\u8c61\uff0c\u5305\u62ec\u5217\u8868\u3001\u5143\u7d44\u548c\u5b57\u5178\u7684\u9375\u3002<\/p>\n<h3>Q2: \u70ba\u4f55\u96c6\u5408\u4e0d\u5141\u8a31\u91cd\u8907\u5143\u7d20\uff1f<\/h3>\n<p>A2: \u96c6\u5408\u7684\u7279\u6027\u4f7f\u5176\u5728\u6578\u64da\u7d50\u69cb\u4e0a\u53ea\u80fd\u5132\u5b58\u552f\u4e00\u7684\u5143\u7d20\uff0c\u4ee5\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u548c\u7bc0\u7701\u5167\u5b58\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u8a08\u7b97\u5169\u500b\u96c6\u5408\u7684\u4ea4\u96c6\uff1f<\/h3>\n<p>A3: \u53ef\u4ee5\u4f7f\u7528 `&#038;` \u904b\u7b97\u7b26\u6216 `intersection()` \u65b9\u6cd5\u4f86\u8a08\u7b97\u5169\u500b\u96c6\u5408\u7684\u4ea4\u96c6\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7684Set()\u51fd\u6578\u53ef\u4ee5\u5c07\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u5e8f\u5217\u8f49\u63db\u70ba\u96c6\u5408\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6d88\u9664\u91cd\u8907\u5143\u7d20\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u7a2e\u7c21\u55ae\u800c\u6709\u6548\u7684\u65b9\u6cd5\u4f86\u64cd\u4f5c\u96c6\u5408\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Set()\u51fd\u6578\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\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-2848","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-JW","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2848","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=2848"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2848\/revisions"}],"predecessor-version":[{"id":13021,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2848\/revisions\/13021"}],"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=2848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}