{"id":1054013,"date":"2024-12-31T14:38:49","date_gmt":"2024-12-31T06:38:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1054013.html"},"modified":"2024-12-31T14:38:51","modified_gmt":"2024-12-31T06:38:51","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e4%b8%80%e4%b8%aa%e9%9b%86%e5%90%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1054013.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u96c6\u5408"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/a7f81372-555e-4323-b0d9-612f0395dffa.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u96c6\u5408\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u751f\u6210\u96c6\u5408\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528\u5927\u62ec\u53f7\u521b\u5efa\u3001\u4f7f\u7528set()\u51fd\u6570\u3001\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u4f7f\u7528\u5927\u62ec\u53f7{}\u6216\u8005\u8c03\u7528set()\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u96c6\u5408\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u5177\u4f53\u6b65\u9aa4\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5927\u62ec\u53f7{}\u521b\u5efa\u96c6\u5408<\/h3>\n<\/p>\n<p><p>Python\u4e2d\uff0c\u4f7f\u7528\u5927\u62ec\u53f7{}\u53ef\u4ee5\u5feb\u901f\u521b\u5efa\u4e00\u4e2a\u96c6\u5408\u3002\u96c6\u5408\u662f\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u7684\u96c6\u5408\u4f53\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {1, 2, 3, 4, 5}<\/p>\n<p>print(my_set)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u6570\u5b571\u52305\u7684\u96c6\u5408\u3002<strong>\u4f7f\u7528\u5927\u62ec\u53f7\u521b\u5efa\u96c6\u5408\u7684\u4f18\u70b9\u662f\u7b80\u6d01\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u8f83\u5c0f\u4e14\u9759\u6001\u7684\u96c6\u5408<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528set()\u51fd\u6570\u521b\u5efa\u96c6\u5408<\/h3>\n<\/p>\n<p><p>set()\u51fd\u6570\u53ef\u4ee5\u5c06\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u8f6c\u6362\u4e3a\u96c6\u5408\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_set = set(my_list)<\/p>\n<p>print(my_set)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u4e86\u96c6\u5408\u3002<strong>\u4f7f\u7528set()\u51fd\u6570\u7684\u4f18\u52bf\u5728\u4e8e\u80fd\u591f\u52a8\u6001\u5730\u5c06\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u521b\u5efa\u96c6\u5408\u7684\u573a\u666f<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u521b\u5efa\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u57fa\u4e8e\u73b0\u6709\u96c6\u5408\u521b\u5efa\u65b0\u96c6\u5408\u7684\u7b80\u6d01\u65b9\u6cd5\u3002\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u96c6\u5408\u63a8\u5bfc\u5f0f\u7684\u8bed\u6cd5\u4f7f\u7528\u82b1\u62ec\u53f7{}\uff0c\u5e76\u4e14\u53ef\u4ee5\u5305\u542b\u6761\u4ef6\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {x for x in range(10) if x % 2 == 0}<\/p>\n<p>print(my_set)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b0\u52309\u4e4b\u95f4\u6240\u6709\u5076\u6570\u7684\u96c6\u5408\u3002<strong>\u96c6\u5408\u63a8\u5bfc\u5f0f\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\uff0c\u53ef\u8bfb\u6027\u5f3a\uff0c\u9002\u7528\u4e8e\u9700\u8981\u57fa\u4e8e\u67d0\u4e9b\u6761\u4ef6\u521b\u5efa\u96c6\u5408\u7684\u573a\u666f<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u96c6\u5408\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1\u3001\u6dfb\u52a0\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528add()\u65b9\u6cd5\u5411\u96c6\u5408\u4e2d\u6dfb\u52a0\u5355\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {1, 2, 3}<\/p>\n<p>my_set.add(4)<\/p>\n<p>print(my_set)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5220\u9664\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528remove()\u6216discard()\u65b9\u6cd5\u5220\u9664\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002remove()\u5728\u5143\u7d20\u4e0d\u5b58\u5728\u65f6\u4f1a\u5f15\u53d1KeyError\uff0c\u800cdiscard()\u4e0d\u4f1a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {1, 2, 3, 4}<\/p>\n<p>my_set.remove(3)<\/p>\n<p>my_set.discard(5)  # \u4e0d\u4f1a\u5f15\u53d1\u9519\u8bef<\/p>\n<p>print(my_set)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u96c6\u5408\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>\u96c6\u5408\u652f\u6301\u591a\u4e2a\u8fd0\u7b97\uff0c\u5982\u5e76\u96c6\u3001\u4ea4\u96c6\u3001\u5dee\u96c6\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">set1 = {1, 2, 3}<\/p>\n<p>set2 = {3, 4, 5}<\/p>\n<p>union_set = set1.union(set2)<\/p>\n<p>intersection_set = set1.intersection(set2)<\/p>\n<p>difference_set = set1.difference(set2)<\/p>\n<p>print(union_set)<\/p>\n<p>print(intersection_set)<\/p>\n<p>print(difference_set)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u96c6\u5408\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u4e0d\u53ef\u53d8\u96c6\u5408<\/h4>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86frozenset()\u51fd\u6570\u6765\u521b\u5efa\u4e0d\u53ef\u53d8\u96c6\u5408\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">immutable_set = frozenset([1, 2, 3])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u96c6\u5408\u7684\u904d\u5386<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {1, 2, 3, 4, 5}<\/p>\n<p>for element in my_set:<\/p>\n<p>    print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u96c6\u5408\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u5728\u6570\u636e\u53bb\u91cd\u3001\u96c6\u5408\u8fd0\u7b97\u3001\u6570\u636e\u7b5b\u9009\u7b49\u65b9\u9762\u6709\u5e7f\u6cdb\u5e94\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u53bb\u91cd<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">data_list = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_data = set(data_list)<\/p>\n<p>print(unique_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u96c6\u5408\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>\u96c6\u5408\u8fd0\u7b97\u5728\u6c42\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6\u7b49\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students_classA = {&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;}<\/p>\n<p>students_classB = {&quot;Bob&quot;, &quot;David&quot;, &quot;Edward&quot;}<\/p>\n<p>common_students = students_classA.intersection(students_classB)<\/p>\n<p>print(common_students)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u751f\u6210\u96c6\u5408\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u76f4\u63a5\u4f7f\u7528\u5927\u62ec\u53f7{}\u521b\u5efa\u3001\u4f7f\u7528set()\u51fd\u6570\u521b\u5efa\u3001\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u521b\u5efa\u3002<strong>\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u70b9<\/strong>\u3002\u6b64\u5916\uff0c\u96c6\u5408\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u57fa\u672c\u64cd\u4f5c\u548c\u9ad8\u7ea7\u7528\u6cd5\uff0c\u5982\u6dfb\u52a0\u3001\u5220\u9664\u5143\u7d20\uff0c\u96c6\u5408\u8fd0\u7b97\u7b49\uff0c\u80fd\u591f\u5728\u6570\u636e\u5904\u7406\u3001\u53bb\u91cd\u3001\u96c6\u5408\u8fd0\u7b97\u7b49\u65b9\u9762\u53d1\u6325\u91cd\u8981\u4f5c\u7528\u3002\u901a\u8fc7\u719f\u7ec3\u638c\u63e1\u96c6\u5408\u7684\u57fa\u672c\u64cd\u4f5c\u548c\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\u3002<\/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\u901a\u8fc7\u4f7f\u7528\u5927\u62ec\u53f7\u6216<code>set()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u7a7a\u96c6\u5408\u3002\u5982\u679c\u4f7f\u7528\u5927\u62ec\u53f7\uff0c\u5fc5\u987b\u6ce8\u610f\u4e0d\u8981\u4e0e\u5b57\u5178\u6df7\u6dc6\uff0c\u56e0\u4e3a\u4e00\u4e2a\u7a7a\u7684\u82b1\u62ec\u53f7<code>{}<\/code>\u4f1a\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u5b57\u5178\u3002\u6b63\u786e\u7684\u521b\u5efa\u65b9\u5f0f\u662f\u4f7f\u7528<code>set()<\/code>\uff0c\u4f8b\u5982\uff1a<code>my_set = set()<\/code>\u3002<\/p>\n<p><strong>\u96c6\u5408\u7684\u5143\u7d20\u662f\u5426\u53ef\u4ee5\u91cd\u590d\uff1f<\/strong><br \/>\u5728Python\u7684\u96c6\u5408\u4e2d\uff0c\u6240\u6709\u7684\u5143\u7d20\u90fd\u662f\u552f\u4e00\u7684\uff0c\u91cd\u590d\u7684\u5143\u7d20\u4f1a\u88ab\u81ea\u52a8\u5ffd\u7565\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u4f60\u5c1d\u8bd5\u5c06\u76f8\u540c\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u96c6\u5408\u4e2d\uff0c\u5b83\u53ea\u4f1a\u51fa\u73b0\u4e00\u6b21\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u521b\u5efa\u4e00\u4e2a\u96c6\u5408\u5e76\u6dfb\u52a0\u591a\u4e2a\u76f8\u540c\u7684\u5143\u7d20\uff0c\u7ed3\u679c\u96c6\u5408\u4e2d\u53ea\u4f1a\u4fdd\u7559\u4e00\u4e2a\u8be5\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u5411\u4e00\u4e2a\u5df2\u6709\u7684\u96c6\u5408\u6dfb\u52a0\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>add()<\/code>\u65b9\u6cd5\u5411\u96c6\u5408\u4e2d\u6dfb\u52a0\u5355\u4e2a\u5143\u7d20\uff0c\u4f8b\u5982\uff1a<code>my_set.add(3)<\/code>\u3002\u5982\u679c\u4f60\u9700\u8981\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\uff0c\u4f20\u5165\u4e00\u4e2a\u5217\u8868\u3001\u5143\u7ec4\u6216\u53e6\u4e00\u4e2a\u96c6\u5408\u3002\u4f8b\u5982\uff1a<code>my_set.update([1, 2, 3])<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u6269\u5c55\u96c6\u5408\u7684\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u751f\u6210\u96c6\u5408\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528\u5927\u62ec\u53f7\u521b\u5efa\u3001\u4f7f\u7528set()\u51fd\u6570\u3001\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f [&hellip;]","protected":false},"author":3,"featured_media":1054021,"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\/1054013"}],"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=1054013"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054013\/revisions"}],"predecessor-version":[{"id":1054026,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054013\/revisions\/1054026"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1054021"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1054013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1054013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1054013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}