{"id":1148882,"date":"2025-01-13T16:42:46","date_gmt":"2025-01-13T08:42:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1148882.html"},"modified":"2025-01-13T16:42:48","modified_gmt":"2025-01-13T08:42:48","slug":"python%e5%88%97%e8%a1%a8%e5%a6%82%e4%bd%95%e5%8f%98%e6%88%90%e9%9b%86%e5%90%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1148882.html","title":{"rendered":"python\u5217\u8868\u5982\u4f55\u53d8\u6210\u96c6\u5408"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25172629\/2fec1365-80a8-4c38-93f6-f447befec94a.webp\" alt=\"python\u5217\u8868\u5982\u4f55\u53d8\u6210\u96c6\u5408\" \/><\/p>\n<p><p> <strong>Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684<code>set()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u3002\u8fd9\u6837\u505a\u6709\u52a9\u4e8e\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff0c\u5e76\u4e14\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u6570\u636e\u7ed3\u6784\u3002<\/strong>\u3001<strong>\u8f6c\u6362\u7b80\u5355\u3001\u53ef\u4ee5\u53bb\u91cd\u3001\u9002\u5408\u505a\u96c6\u5408\u8fd0\u7b97\u3002<\/strong>\u4e0b\u9762\u8be6\u7ec6\u89e3\u91ca\u5176\u4e2d\u7684\u4e00\u4e2a\u65b9\u9762\uff1a<\/p>\n<\/p>\n<p><p><strong>\u53bb\u91cd<\/strong>\uff1a\u5217\u8868\u4e2d\u53ef\u80fd\u4f1a\u5305\u542b\u91cd\u590d\u7684\u5143\u7d20\uff0c\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u53ef\u4ee5\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20\u3002\u96c6\u5408\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u552f\u4e00\u7684\uff0c\u8fd9\u5bf9\u4e8e\u9700\u8981\u4fdd\u8bc1\u6570\u636e\u552f\u4e00\u6027\u7684\u573a\u666f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 4, 5, 5, 6]<\/p>\n<p>my_set = set(my_list)<\/p>\n<p>print(my_set)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{1, 2, 3, 4, 5, 6}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u5217\u8868\u5982\u4f55\u53d8\u6210\u96c6\u5408\u7684\u5404\u4e2a\u65b9\u9762\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001Python\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p><h2>1\u3001\u4f7f\u7528<code>set()<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u5185\u7f6e\u51fd\u6570<code>set()<\/code>\uff0c\u53ef\u4ee5\u5c06\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u4f8b\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u8f6c\u6362\u4e3a\u96c6\u5408\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 4, 5, 5, 6]<\/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\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u5217\u8868<code>my_list<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>set()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u96c6\u5408<code>my_set<\/code>\u3002\u8f93\u51fa\u7684\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{1, 2, 3, 4, 5, 6}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u901a\u8fc7\u96c6\u5408\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u4f7f\u7528<code>set()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u96c6\u5408\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u96c6\u5408\u3002\u96c6\u5408\u63a8\u5bfc\u5f0f\u7684\u8bed\u6cd5\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f46\u4f7f\u7528\u7684\u662f\u82b1\u62ec\u53f7<code>{}<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 4, 5, 5, 6]<\/p>\n<p>my_set = {item for item in 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\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f<code>{item for item in my_list}<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u96c6\u5408<code>my_set<\/code>\u3002\u8f93\u51fa\u7684\u7ed3\u679c\u540c\u6837\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{1, 2, 3, 4, 5, 6}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u96c6\u5408\u7684\u57fa\u672c\u64cd\u4f5c<\/strong><\/h2>\n<p><h2>1\u3001\u6dfb\u52a0\u5143\u7d20<\/h2>\n<\/p>\n<p><p>\u5728\u96c6\u5408\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>add()<\/code>\u65b9\u6cd5\u6765\u6dfb\u52a0\u5355\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\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><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{1, 2, 3, 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u5220\u9664\u5143\u7d20<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u6216<code>discard()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002\u4e0d\u540c\u7684\u662f\uff0c\u5982\u679c\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u5220\u9664\u4e0d\u5b58\u5728\u7684\u5143\u7d20\u4f1a\u5f15\u53d1<code>KeyError<\/code>\uff0c\u800c<code>discard()<\/code>\u65b9\u6cd5\u4e0d\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {1, 2, 3}<\/p>\n<p>my_set.remove(2)<\/p>\n<p>print(my_set)<\/p>\n<p>my_set.discard(4)  # \u4e0d\u4f1a\u5f15\u53d1\u9519\u8bef<\/p>\n<p>print(my_set)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{1, 3}<\/p>\n<p>{1, 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u96c6\u5408\u8fd0\u7b97<\/h2>\n<\/p>\n<p><p>\u96c6\u5408\u652f\u6301\u591a\u79cd\u8fd0\u7b97\uff0c\u4f8b\u5982\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6\u548c\u5bf9\u79f0\u5dee\u96c6\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">set1 = {1, 2, 3}<\/p>\n<p>set2 = {3, 4, 5}<\/p>\n<h2><strong>\u4ea4\u96c6<\/strong><\/h2>\n<p>print(set1 &amp; set2)  # {3}<\/p>\n<h2><strong>\u5e76\u96c6<\/strong><\/h2>\n<p>print(set1 | set2)  # {1, 2, 3, 4, 5}<\/p>\n<h2><strong>\u5dee\u96c6<\/strong><\/h2>\n<p>print(set1 - set2)  # {1, 2}<\/p>\n<h2><strong>\u5bf9\u79f0\u5dee\u96c6<\/strong><\/h2>\n<p>print(set1 ^ set2)  # {1, 2, 4, 5}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u96c6\u5408\u7684\u5e94\u7528\u573a\u666f<\/strong><\/h2>\n<p><h2>1\u3001\u6570\u636e\u53bb\u91cd<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u5c24\u5176\u662f\u4ece\u5916\u90e8\u6570\u636e\u6e90\u83b7\u53d6\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u91cd\u590d\u7684\u6570\u636e\u3002\u901a\u8fc7\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u53ef\u4ee5\u8f7b\u677e\u53bb\u9664\u91cd\u590d\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_data = list(set(data))<\/p>\n<p>print(unique_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">[1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u96c6\u5408\u8fd0\u7b97<\/h2>\n<\/p>\n<p><p>\u96c6\u5408\u8fd0\u7b97\u5728\u7edf\u8ba1\u3001\u6570\u636e\u5206\u6790\u548c\u5176\u4ed6\u9700\u8981\u96c6\u5408\u95f4\u5173\u7cfb\u8ba1\u7b97\u7684\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u8fd0\u7b97\u6765\u8ba1\u7b97\u4e24\u4e2a\u6570\u636e\u96c6\u7684\u4ea4\u96c6\u3001\u5e76\u96c6\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">students_A = {&quot;John&quot;, &quot;Jane&quot;, &quot;Tom&quot;}<\/p>\n<p>students_B = {&quot;Jane&quot;, &quot;Tom&quot;, &quot;Alice&quot;}<\/p>\n<h2><strong>\u8ba1\u7b97\u540c\u65f6\u53c2\u52a0A\u548cB\u8bfe\u7a0b\u7684\u5b66\u751f<\/strong><\/h2>\n<p>both_courses = students_A &amp; students_B<\/p>\n<p>print(both_courses)  # {&quot;Jane&quot;, &quot;Tom&quot;}<\/p>\n<h2><strong>\u8ba1\u7b97\u81f3\u5c11\u53c2\u52a0\u4e00\u4e2a\u8bfe\u7a0b\u7684\u5b66\u751f<\/strong><\/h2>\n<p>at_least_one = students_A | students_B<\/p>\n<p>print(at_least_one)  # {&quot;John&quot;, &quot;Jane&quot;, &quot;Tom&quot;, &quot;Alice&quot;}<\/p>\n<h2><strong>\u8ba1\u7b97\u53ea\u53c2\u52a0A\u8bfe\u7a0b\u7684\u5b66\u751f<\/strong><\/h2>\n<p>only_A = students_A - students_B<\/p>\n<p>print(only_A)  # {&quot;John&quot;}<\/p>\n<h2><strong>\u8ba1\u7b97\u53ea\u53c2\u52a0\u4e00\u4e2a\u8bfe\u7a0b\u7684\u5b66\u751f<\/strong><\/h2>\n<p>only_one_course = students_A ^ students_B<\/p>\n<p>print(only_one_course)  # {&quot;John&quot;, &quot;Alice&quot;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u96c6\u5408\u7684\u9ad8\u6548\u67e5\u627e<\/h2>\n<\/p>\n<p><p>\u96c6\u5408\u7684\u67e5\u627e\u64cd\u4f5c\u975e\u5e38\u9ad8\u6548\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\u3002\u5728\u9700\u8981\u9891\u7e41\u67e5\u627e\u5143\u7d20\u7684\u573a\u666f\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">large_list = list(range(1000000))<\/p>\n<p>large_set = set(large_list)<\/p>\n<p>import time<\/p>\n<h2><strong>\u5728\u5217\u8868\u4e2d\u67e5\u627e\u5143\u7d20<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>999999 in large_list<\/p>\n<p>print(&quot;List search time:&quot;, time.time() - start_time)<\/p>\n<h2><strong>\u5728\u96c6\u5408\u4e2d\u67e5\u627e\u5143\u7d20<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>999999 in large_set<\/p>\n<p>print(&quot;Set search time:&quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u663e\u793a\u96c6\u5408\u7684\u67e5\u627e\u65f6\u95f4\u663e\u8457\u77ed\u4e8e\u5217\u8868\u7684\u67e5\u627e\u65f6\u95f4\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u96c6\u5408\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/strong><\/h2>\n<p><h2>1\u3001\u96c6\u5408\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p>\u96c6\u5408\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u96c6\u5408\u7684\u65b9\u5f0f\uff0c\u8bed\u6cd5\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u8fc7\u6ee4\u6216\u8f6c\u6362\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6]<\/p>\n<p>even_numbers = {num for num in numbers if num % 2 == 0}<\/p>\n<p>print(even_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{2, 4, 6}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u51bb\u7ed3\u96c6\u5408<\/h2>\n<\/p>\n<p><p>\u51bb\u7ed3\u96c6\u5408\uff08<code>frozenset<\/code>\uff09\u662f\u4e0d\u53ef\u53d8\u7684\u96c6\u5408\u3002\u4e00\u65e6\u521b\u5efa\uff0c\u4e0d\u80fd\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u9700\u8981\u4e0d\u53ef\u53d8\u96c6\u5408\u7684\u573a\u666f\uff0c\u4f8b\u5982\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">frozen_set = frozenset([1, 2, 3, 3, 4])<\/p>\n<p>print(frozen_set)<\/p>\n<h2><strong>\u5c1d\u8bd5\u6dfb\u52a0\u5143\u7d20\u5c06\u5f15\u53d1\u9519\u8bef<\/strong><\/h2>\n<h2><strong>frozen_set.add(5)  # AttributeError: &#39;frozenset&#39; object has no attribute &#39;add&#39;<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">frozenset({1, 2, 3, 4})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u96c6\u5408\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u7684\u8f6c\u6362<\/strong><\/h2>\n<p><h2>1\u3001\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {1, 2, 3, 4}<\/p>\n<p>my_list = list(my_set)<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">[1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u96c6\u5408\u8f6c\u6362\u4e3a\u5143\u7ec4<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>tuple()<\/code>\u51fd\u6570\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {1, 2, 3, 4}<\/p>\n<p>my_tuple = tuple(my_set)<\/p>\n<p>print(my_tuple)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">(1, 2, 3, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u96c6\u5408\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {1, 2, 3, 4}<\/p>\n<p>my_str = str(my_set)<\/p>\n<p>print(my_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{1, 2, 3, 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001\u96c6\u5408\u7684\u6027\u80fd\u4e0e\u6548\u7387<\/strong><\/h2>\n<p><h2>1\u3001\u67e5\u627e\u6548\u7387<\/h2>\n<\/p>\n<p><p>\u96c6\u5408\u7684\u67e5\u627e\u64cd\u4f5c\u5728\u5e73\u5747\u60c5\u51b5\u4e0b\u5177\u6709O(1)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u8fd9\u4f7f\u5f97\u5b83\u5728\u9700\u8981\u9891\u7e41\u67e5\u627e\u5143\u7d20\u7684\u573a\u666f\u4e2d\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>large_list = list(range(1000000))<\/p>\n<p>large_set = set(large_list)<\/p>\n<h2><strong>\u5728\u5217\u8868\u4e2d\u67e5\u627e\u5143\u7d20<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>999999 in large_list<\/p>\n<p>print(&quot;List search time:&quot;, time.time() - start_time)<\/p>\n<h2><strong>\u5728\u96c6\u5408\u4e2d\u67e5\u627e\u5143\u7d20<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>999999 in large_set<\/p>\n<p>print(&quot;Set search time:&quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u6dfb\u52a0\u548c\u5220\u9664\u6548\u7387<\/h2>\n<\/p>\n<p><p>\u96c6\u5408\u7684\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u4e5f\u5177\u6709O(1)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u8fd9\u4f7f\u5f97\u5b83\u5728\u9700\u8981\u9891\u7e41\u4fee\u6539\u5143\u7d20\u7684\u573a\u666f\u4e2d\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>large_set = set(range(1000000))<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>large_set.add(1000000)<\/p>\n<p>print(&quot;Set add time:&quot;, time.time() - start_time)<\/p>\n<h2><strong>\u5220\u9664\u5143\u7d20<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>large_set.remove(999999)<\/p>\n<p>print(&quot;Set remove time:&quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001\u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/strong><\/h2>\n<p><h2>1\u3001\u53bb\u91cd\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u53bb\u9664\u91cd\u590d\u9879\u3002\u96c6\u5408\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 2, 3, 4, 4, 5]<\/p>\n<p>unique_data = list(set(data))<\/p>\n<p>print(unique_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u96c6\u5408\u8fd0\u7b97<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u96c6\u5408\u8fd0\u7b97\u53ef\u4ee5\u7528\u4e8e\u4f8b\u5982\u7edf\u8ba1\u5206\u6790\u3001\u6570\u636e\u6316\u6398\u7b49\u9886\u57df\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u9645\u7684\u5e94\u7528\u6848\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u4e24\u4e2a\u73ed\u7ea7\u7684\u5b66\u751f\u540d\u5355<\/p>\n<p>class_A = {&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;, &quot;David&quot;}<\/p>\n<p>class_B = {&quot;Bob&quot;, &quot;David&quot;, &quot;Eve&quot;, &quot;Frank&quot;}<\/p>\n<h2><strong>\u8ba1\u7b97\u540c\u65f6\u5728\u4e24\u4e2a\u73ed\u7ea7\u7684\u5b66\u751f<\/strong><\/h2>\n<p>common_students = class_A &amp; class_B<\/p>\n<p>print(&quot;Common students:&quot;, common_students)<\/p>\n<h2><strong>\u8ba1\u7b97\u81f3\u5c11\u5728\u4e00\u4e2a\u73ed\u7ea7\u7684\u5b66\u751f<\/strong><\/h2>\n<p>all_students = class_A | class_B<\/p>\n<p>print(&quot;All students:&quot;, all_students)<\/p>\n<h2><strong>\u8ba1\u7b97\u53ea\u5728A\u73ed\u7ea7\u7684\u5b66\u751f<\/strong><\/h2>\n<p>only_A = class_A - class_B<\/p>\n<p>print(&quot;Only in class A:&quot;, only_A)<\/p>\n<h2><strong>\u8ba1\u7b97\u53ea\u5728\u4e00\u4e2a\u73ed\u7ea7\u7684\u5b66\u751f<\/strong><\/h2>\n<p>unique_students = class_A ^ class_B<\/p>\n<p>print(&quot;Unique students:&quot;, unique_students)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u5df2\u7ecf\u5904\u7406\u8fc7\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u6765\u8ddf\u8e2a\u5df2\u5904\u7406\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">processed_elements = set()<\/p>\n<p>for element in range(10):<\/p>\n<p>    if element not in processed_elements:<\/p>\n<p>        print(&quot;Processing element:&quot;, element)<\/p>\n<p>        processed_elements.add(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516b\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u5217\u8868\u5982\u4f55\u53d8\u6210\u96c6\u5408\u7684\u5404\u79cd\u65b9\u6cd5\u548c\u5e94\u7528\u573a\u666f\u3002\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u4e0d\u4ec5\u53ef\u4ee5\u53bb\u9664\u91cd\u590d\u5143\u7d20\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528\u96c6\u5408\u7684\u9ad8\u6548\u67e5\u627e\u548c\u5404\u79cd\u96c6\u5408\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\u548c\u63d0\u9ad8\u6027\u80fd\u3002\u65e0\u8bba\u662f\u5728\u6570\u636e\u53bb\u91cd\u3001\u96c6\u5408\u8fd0\u7b97\u8fd8\u662f\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u7684\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u96c6\u5408\u90fd\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>set()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u3002\u53ea\u9700\u5c06\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>set()<\/code>\u51fd\u6570\u5373\u53ef\u3002\u4f8b\u5982\uff1a<code>my_list = [1, 2, 3, 2]<\/code>\uff0c\u8c03\u7528<code>my_set = set(my_list)<\/code>\u540e\uff0c<code>my_set<\/code>\u5c06\u53d8\u6210<code>{1, 2, 3}<\/code>\uff0c\u5e76\u4e14\u81ea\u52a8\u53bb\u9664\u4e86\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<p><strong>\u4f7f\u7528\u96c6\u5408\u6709\u54ea\u4e9b\u4f18\u70b9\uff1f<\/strong><br \/>\u96c6\u5408\u5728Python\u4e2d\u662f\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u3002\u8fd9\u610f\u5473\u7740\u96c6\u5408\u53ef\u4ee5\u6709\u6548\u5730\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff0c\u5e2e\u52a9\u7b80\u5316\u6570\u636e\u5904\u7406\u3002\u6b64\u5916\uff0c\u96c6\u5408\u652f\u6301\u591a\u79cd\u6570\u5b66\u64cd\u4f5c\uff0c\u4f8b\u5982\u4ea4\u96c6\u3001\u5e76\u96c6\u548c\u5dee\u96c6\uff0c\u8fd9\u4e9b\u5728\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4fdd\u7559\u5217\u8868\u4e2d\u7684\u987a\u5e8f\uff1f<\/strong><br \/>\u6807\u51c6\u7684\u96c6\u5408\u662f\u4e0d\u4fdd\u7559\u987a\u5e8f\u7684\u3002\u5982\u679c\u9700\u8981\u5728\u8f6c\u6362\u7684\u540c\u65f6\u4fdd\u7559\u5143\u7d20\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>dict.fromkeys()<\/code>\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\u6765\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u7136\u540e\u518d\u8c03\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u5b57\u5178\u7684\u952e\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u8fd9\u5c06\u4fdd\u7559\u5143\u7d20\u7684\u987a\u5e8f\u5e76\u53bb\u9664\u91cd\u590d\u9879\uff0c\u4f8b\u5982\uff1a<code>unique_ordered_list = list(dict.fromkeys(my_list))<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684set()\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u3002\u8fd9\u6837\u505a\u6709\u52a9\u4e8e\u53bb\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff0c\u5e76\u4e14\u96c6\u5408\u662f [&hellip;]","protected":false},"author":3,"featured_media":1148884,"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\/1148882"}],"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=1148882"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148882\/revisions"}],"predecessor-version":[{"id":1148885,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148882\/revisions\/1148885"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1148884"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1148882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1148882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1148882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}