{"id":522,"date":"2025-01-17T15:52:48","date_gmt":"2025-01-17T07:52:48","guid":{"rendered":"http:\/\/sqtcw.cn\/?p=522"},"modified":"2025-01-17T15:52:48","modified_gmt":"2025-01-17T07:52:48","slug":"python%e8%ae%a4%e8%af%86%e5%b9%b6%e9%9b%86%ef%bc%8c%e4%ba%a4%e9%9b%86%ef%bc%8c%e5%b7%ae%e9%9b%86","status":"publish","type":"post","link":"http:\/\/www.cnitw.com\/?p=522","title":{"rendered":"Python\u8ba4\u8bc6\u96c6\u5408\u8fd0\u7b97\uff1a\u5e76\u96c6\uff08.unio\uff09\uff0c\u4ea4\u96c6\uff08.intersection\uff09\uff0c\u5dee\u96c6\uff08.difference\uff09"},"content":{"rendered":"\n<p>\u5728\u96c6\u5408\u8bba\u4e2d\uff1a<\/p>\n\n\n\n<p>\u5e76\u96c6\uff1a\u662f\u6307\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u96c6\u5408\u4e2d\u7684\u6240\u6709\u5143\u7d20\u7ec4\u5408\u5728\u4e00\u8d77\u6784\u6210\u7684\u65b0\u96c6\u5408\u3002\u5982\u679c\u4e00\u4e2a\u5143\u7d20\u5c5e\u4e8e\u5176\u4e2d\u81f3\u5c11\u4e00\u4e2a\u539f\u59cb\u96c6\u5408\uff0c\u90a3\u4e48\u5b83\u5c31\u5c5e\u4e8e\u8fd9\u4e9b\u96c6\u5408\u7684\u5e76\u96c6\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u96c6\u5408 A = {1, 2, 3}\uff0c\u96c6\u5408 B = {3, 4, 5}\uff0cA \u548c B \u7684\u5e76\u96c6\u5c31\u662f {1, 2, 3, 4, 5}\u3002<\/p>\n\n\n\n<p>\u4ea4\u96c6\uff1a\u662f\u6307\u7531\u540c\u65f6\u5c5e\u4e8e\u4e24\u4e2a\u6216\u591a\u4e2a\u96c6\u5408\u7684\u5143\u7d20\u6240\u7ec4\u6210\u7684\u96c6\u5408\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u96c6\u5408 A = {1, 2, 3}\uff0c\u96c6\u5408 B = {3, 4, 5}\uff0cA \u548c B \u7684\u4ea4\u96c6\u5c31\u662f {3}\u3002<\/p>\n\n\n\n<p>\u5dee\u96c6\uff1a\u5bf9\u4e8e\u4e24\u4e2a\u96c6\u5408 A \u548c B\uff0cA \u4e0e B \u7684\u5dee\u96c6\u662f\u7531\u5c5e\u4e8e A \u4f46\u4e0d\u5c5e\u4e8e B \u7684\u6240\u6709\u5143\u7d20\u7ec4\u6210\u7684\u96c6\u5408\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u96c6\u5408 A = {1, 2, 3}\uff0c\u96c6\u5408 B = {3, 4, 5}\uff0cA \u51cf\u53bb B \u7684\u5dee\u96c6\u662f {1, 2}\uff0cB \u51cf\u53bb A \u7684\u5dee\u96c6\u662f {4, 5}\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u96c6\u5408\u5728 Python \u4e2d\u7684\u5e94\u7528\u5b9e\u4f8b\uff1a\n\n1. \u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff1a\n\n   ```python\n   my_list = &#91;1, 2, 2, 3, 3, 3, 4, 4, 4, 4]\n   my_set = set(my_list)\n   unique_list = list(my_set)\n   print(unique_list) \n   ```\n\n2. \u5feb\u901f\u5224\u65ad\u5143\u7d20\u662f\u5426\u5728\u96c6\u5408\u4e2d\uff1a\n\n   ```python\n   fruits = {\"apple\", \"banana\", \"orange\"}\n   if \"apple\" in fruits:\n       print(\"\u82f9\u679c\u5728\u6c34\u679c\u96c6\u5408\u4e2d\")\n   ```\n\n3. \u96c6\u5408\u8fd0\u7b97\uff0c\u5982\u5e76\u96c6\u3001\u4ea4\u96c6\u3001\u5dee\u96c6\uff1a\n\n   ```python\n   set1 = {1, 2, 3, 4, 5}\n   set2 = {4, 5, 6, 7, 8}\n\n   union_set = set1.union(set2)               #set1 \u548c set2 \u7684\u5e76\u96c6\n   intersection_set = set1.intersection(set2)  #set1 \u548c set2 \u7684\u4ea4\u96c6\uff08\u91cd\u590d\u7684\uff09\n   difference_set = set1.difference(set2)       #set1 - set2 \u7684\u5dee\u96c6\uff08\u4e0d\u540c\u7684\uff09\n\n   print(\"\u5e76\u96c6\uff1a\", union_set)\n   print(\"\u4ea4\u96c6\uff1a\", intersection_set)\n   print(\"\u5dee\u96c6\uff1a\", difference_set)\n   ```\n\n4. \u96c6\u5408\u63a8\u5bfc\u5f0f\uff1a\n\n   ```python\n   numbers = &#91;1, 2, 3, 4, 5, 5, 4, 3, 2, 1]\n   unique_squares = {x ** 2 for x in numbers}\n   print(unique_squares)\n   ```\n\n5. \u591a\u4e2a\u96c6\u5408\u7684\u6bd4\u8f83\u548c\u64cd\u4f5c\uff1a\n\n   ```python\n   setA = {1, 2, 3}\n   setB = {2, 3, 4}\n   setC = {3, 4, 5}\n\n   # \u627e\u51fa\u4e09\u4e2a\u96c6\u5408\u7684\u5171\u540c\u5143\u7d20\n   common_elements = setA.intersection(setB).intersection(setC)\n   print(common_elements)\n   ```<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u96c6\u5408\u8bba\u4e2d\uff1a \u5e76\u96c6\uff1a\u662f\u6307\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u96c6\u5408\u4e2d\u7684\u6240\u6709\u5143\u7d20\u7ec4\u5408\u5728\u4e00\u8d77\u6784\u6210\u7684\u65b0\u96c6\u5408\u3002\u5982\u679c\u4e00\u4e2a\u5143\u7d20\u5c5e\u4e8e\u5176\u4e2d\u81f3\u5c11\u4e00\u4e2a\u539f\u59cb\u96c6\u5408\uff0c\u90a3\u4e48\u5b83\u5c31\u5c5e\u4e8e\u8fd9\u4e9b\u96c6\u5408\u7684\u5e76\u96c6\u3002 \u4f8b\u5982\uff0c\u96c6\u5408 A = {1, 2, 3}\uff0c\u96c6\u5408 B = {3, 4, 5}\uff0cA \u548c B \u7684\u5e76\u96c6\u5c31\u662f {1, 2, 3, 4, 5}\u3002 \u4ea4\u96c6\uff1a\u662f\u6307\u7531\u540c\u65f6\u5c5e\u4e8e\u4e24\u4e2a\u6216\u591a\u4e2a\u96c6\u5408\u7684\u5143\u7d20\u6240\u7ec4\u6210\u7684\u96c6\u5408\u3002 \u4f8b\u5982\uff0c\u96c6\u5408 A = {1, 2, 3}\uff0c\u96c6\u5408 B =&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-522","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=522"}],"version-history":[{"count":0,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/522\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=522"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}