{"id":3318,"date":"2025-05-31T16:08:37","date_gmt":"2025-06-04T08:14:36","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3318"},"modified":"2025-06-04T16:08:37","modified_gmt":"2025-06-04T08:14:36","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84set%e5%87%bd%e6%95%b8-3","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84set%e5%87%bd%e6%95%b8-3\/","title":{"rendered":"\u6df1\u5165\u63a2\u8a0e Python \u4e2d\u7684 set() \u51fd\u5f0f\uff1a\u6700\u65b0\u7528\u6cd5\u8207\u61c9\u7528\u5be6\u4f8b\uff082025 \u5e74\u66f4\u65b0\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, set(), Python set, Python \u96c6\u5408\"><\/p>\n<h1>\u6df1\u5165\u63a2\u8a0e Python \u4e2d\u7684 set() \u51fd\u5f0f<\/h1>\n<p>Python \u4e2d\u7684 `set()` \u51fd\u5f0f\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u7528\u65bc\u5efa\u7acb\u4e00\u500b\u7121\u5e8f\u4e14\u4e0d\u91cd\u8907\u7684\u5143\u7d20\u96c6\u5408\u3002\u5b83\u80fd\u5920\u63a5\u53d7\u5404\u7a2e\u5e8f\u5217\u578b\u5225\uff08\u5982\u5217\u8868\u3001\u5143\u7d44\u3001\u5b57\u4e32\uff09\uff0c\u4e26\u5c07\u5176\u8f49\u63db\u70ba\u96c6\u5408\u3002\u6839\u64da 2025 \u5e74\u7684\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u6211\u5011\u4f86\u4e86\u89e3\u5982\u4f55\u6709\u6548\u4f7f\u7528 `set()` \u51fd\u5f0f\u3002<\/p>\n<h2>set() \u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>`set()` \u51fd\u5f0f\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nset(iterable)<br \/>\n&#8220;`<\/p>\n<p>\u5176\u4e2d\uff0c`iterable` \u662f\u4efb\u4f55\u53ef\u8fed\u4ee3\u7684\u7269\u4ef6\uff0c\u5982\u5217\u8868\u3001\u5143\u7d44\u3001\u5b57\u4e32\u7b49\u3002<\/p>\n<p>\u8209\u500b\u4f8b\u5b50\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u5217\u8868\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_list = [1, 2, 3, 4, 5, 5, 6, 7, 8, 8]\n&#8220;`<\/p>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `set()` \u51fd\u5f0f\u5c07\u5176\u8f49\u63db\u70ba\u96c6\u5408\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_set = set(my_list)<br \/>\n&#8220;`<\/p>\n<p>\u6b64\u6642\uff0c`my_set` \u7684\u503c\u70ba\uff1a<\/p>\n<p>&#8220;`python<br \/>\n{1, 2, 3, 4, 5, 6, 7, 8}<br \/>\n&#8220;`<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u91cd\u8907\u7684\u5143\u7d20 5 \u548c 8 \u5df2\u7d93\u88ab\u79fb\u9664\u4e86\u3002\u9019\u6a23\u7684\u7279\u6027\u4f7f\u5f97 `set()` \u975e\u5e38\u9069\u5408\u7528\u65bc\u6578\u64da\u53bb\u91cd\u7684\u5834\u666f\u3002<\/p>\n<h2>set() \u7684\u61c9\u7528<\/h2>\n<p>`set()` \u51fd\u5f0f\u53ef\u4ee5\u7528\u4f86\u505a\u5f88\u591a\u6709\u8da3\u4e14\u5be6\u7528\u7684\u4e8b\u60c5\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><strong>\u6aa2\u67e5\u5169\u500b\u5e8f\u5217\u662f\u5426\u6709\u91cd\u8907\u5143\u7d20<\/strong>\uff1a\u6211\u5011\u53ef\u4ee5\u5c07\u5169\u500b\u5e8f\u5217\u8f49\u63db\u70ba\u96c6\u5408\uff0c\u7136\u5f8c\u6aa2\u67e5\u5b83\u5011\u662f\u5426\u6709\u91cd\u8907\u5143\u7d20\uff1a<br \/>\n        &#8220;`python<br \/>\n        list1 = [1, 2, 3, 4, 5]\n        list2 = [4, 5, 6, 7, 8]\n<p>        set1 = set(list1)<br \/>\n        set2 = set(list2)<\/p>\n<p>        # \u6aa2\u67e5\u662f\u5426\u6709\u91cd\u8907\u5143\u7d20<br \/>\n        if set1 &#038; set2:<br \/>\n            print(&#8220;\u5169\u500b\u5e8f\u5217\u6709\u91cd\u8907\u5143\u7d20&#8221;)<br \/>\n        else:<br \/>\n            print(&#8220;\u5169\u500b\u5e8f\u5217\u6c92\u6709\u91cd\u8907\u5143\u7d20&#8221;)<br \/>\n        &#8220;`\n    <\/li>\n<li><strong>\u6c42\u5169\u500b\u5e8f\u5217\u7684\u4ea4\u96c6<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528 `&#038;` \u904b\u7b97\u5b50\u6c42\u5169\u500b\u5e8f\u5217\u7684\u4ea4\u96c6\uff1a<br \/>\n        &#8220;`python<br \/>\n        intersection = set1 &#038; set2<br \/>\n        print(intersection)  # \u8f38\u51fa\u7d50\u679c\u70ba {4, 5}<br \/>\n        &#8220;`\n    <\/li>\n<li><strong>\u6c42\u5169\u500b\u5e8f\u5217\u7684\u5dee\u96c6<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528 `-` \u904b\u7b97\u5b50\u6c42\u5169\u500b\u5e8f\u5217\u7684\u5dee\u96c6\uff1a<br \/>\n        &#8220;`python<br \/>\n        difference = set1 &#8211; set2<br \/>\n        print(difference)  # \u8f38\u51fa\u7d50\u679c\u70ba {1, 2, 3}<br \/>\n        &#8220;`\n    <\/li>\n<\/ul>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `set()` \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>&#8211; **TypeError**\uff1a\u5982\u679c\u4f60\u5617\u8a66\u5c07\u4e0d\u53ef\u8fed\u4ee3\u7684\u7269\u4ef6\uff08\u4f8b\u5982\u6578\u5b57\uff09\u50b3\u905e\u7d66 `set()`\uff0c\u5c07\u6703\u5f15\u767c\u932f\u8aa4\u3002\u8acb\u78ba\u4fdd\u4f7f\u7528\u7684\u53c3\u6578\u662f\u53ef\u8fed\u4ee3\u7684\u3002<br \/>\n&#8211; **\u96c6\u5408\u4e0d\u53ef\u8b8a**\uff1a\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u5fc5\u9808\u662f\u53ef\u54c8\u5e0c\u7684\uff08\u4f8b\u5982\uff0c\u4e0d\u80fd\u5305\u542b\u5217\u8868\u6216\u5176\u4ed6\u96c6\u5408\uff09\u3002\u5982\u679c\u5617\u8a66\u5c07\u5217\u8868\u653e\u5165\u96c6\u5408\uff0c\u5247\u6703\u5f15\u767c\u932f\u8aa4\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`set()` \u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u57fa\u672c\u7684\u96c6\u5408\u64cd\u4f5c\uff0c\u9084\u53ef\u4ee5\u61c9\u7528\u65bc\u985e\u4f3c\u6578\u64da\u5206\u6790\u7684\u9ad8\u7d1a\u5834\u666f\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; **\u6578\u64da\u53bb\u91cd**\uff1a\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\uff0c\u53ef\u4ee5\u5229\u7528 `set()` \u5feb\u901f\u53bb\u9664\u91cd\u8907\u9805\u76ee\u3002<br \/>\n&#8211; **\u6578\u64da\u5206\u6790**\uff1a\u5728\u6578\u64da\u5206\u6790\u4e2d\uff0c\u96c6\u5408\u904b\u7b97\uff08\u5982\u4ea4\u96c6\u3001\u5dee\u96c6\uff09\u53ef\u7528\u65bc\u6bd4\u8f03\u6578\u64da\u96c6\u4e4b\u9593\u7684\u5dee\u7570\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u6587\u8a73\u7d30\u4ecb\u7d39\u4e86 Python \u4e2d\u7684 `set()` \u51fd\u5f0f\u53ca\u5176\u61c9\u7528\uff0c\u4e26\u63d0\u4f9b\u4e86 2025 \u6700\u65b0\u7684\u7528\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002`set()` \u51fd\u5f0f\u96d6\u7136\u7c21\u55ae\uff0c\u4f46\u5b83\u80fd\u89e3\u6c7a\u8a31\u591a\u5be6\u969b\u554f\u984c\uff0c\u503c\u5f97\u6211\u5011\u6df1\u5165\u7814\u7a76\u8207\u61c9\u7528\u3002<\/p>\n<p>\u8981\u4e86\u89e3\u66f4\u591a Python \u7684\u9032\u968e\u7528\u6cd5\uff0c\u53ef\u4ee5\u53c3\u8003 [\u9019\u88e1\u7684\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc) \u4f86\u64f4\u5c55\u4f60\u7684\u77e5\u8b58\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `set()` \u548c `frozenset()` \u6709\u4ec0\u9ebc\u5340\u5225\uff1f**<br \/>\nA1: `set()` \u662f\u53ef\u8b8a\u7684\u96c6\u5408\uff0c\u800c `frozenset()` \u5247\u662f\u4e0d\u53ef\u8b8a\u7684\u96c6\u5408\u3002\u9019\u610f\u5473\u8457 `frozenset()` \u7684\u5143\u7d20\u7121\u6cd5\u88ab\u4fee\u6539\uff0c\u9069\u5408\u7528\u65bc\u9700\u8981\u4fdd\u6301\u4e0d\u8b8a\u7684\u96c6\u5408\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5c07\u96c6\u5408\u8f49\u63db\u56de\u5217\u8868\uff1f**<br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `list()` \u51fd\u5f0f\u5c07\u96c6\u5408\u8f49\u63db\u70ba\u5217\u8868\uff0c\u4f8b\u5982\uff1a`my_list = list(my_set)`\u3002<\/p>\n<p>**Q3: `set()` \u662f\u5426\u53ef\u4ee5\u5305\u542b\u91cd\u8907\u5143\u7d20\uff1f**<br \/>\nA3: \u4e0d\u53ef\u4ee5\uff0c`set()` \u4e2d\u7684\u5143\u7d20\u662f\u552f\u4e00\u7684\uff0c\u91cd\u8907\u7684\u5143\u7d20\u6703\u88ab\u81ea\u52d5\u53bb\u9664\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2Python\u4e2d\u7684Set()\u51fd\u6578\uff0c\u53ef\u4ee5\u8b93\u4f60\u66f4\u6709\u6548\u7387\u5730\u8655\u7406\u8cc7\u6599\uff0c\u66f4\u5feb\u5730\u5b8c\u6210\u5de5\u4f5c\u3002\u672c\u6587\u5c07\u4ecb\u7d39Set()\u51fd\u6578\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u8655\u7406\u8cc7\u6599\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-3318","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-Rw","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3318","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=3318"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3318\/revisions"}],"predecessor-version":[{"id":12867,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3318\/revisions\/12867"}],"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=3318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}