<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Python 集 > 原文: [http://zetcode.com/python/set/](http://zetcode.com/python/set/) Python `set`教程介紹了 Python `set`集合。 我們展示了如何創建集并對其執行操作。 ## Python 集 Python 集是無序數據集,沒有重復的元素。 集支持數學中已知的諸如并集,相交或求差的運算。 ## Python 集字面值 從 Python 2.6 開始,可以使用字面值符號創建集。 我們使用大括號在 Python 中定義一個集,并且元素之間用逗號分隔。 `python_set_literal.py` ```py #!/usr/bin/python3 nums = { 1, 2, 2, 2, 3, 4 } print(type(nums)) print(nums) ``` 該示例創建帶有字面值符號的 Python 集。 ```py nums = { 1, 2, 2, 2, 3, 4 } ``` 集是唯一元素的集; 即使我們提供了 3 次值 2,該集也只包含一個 2。 ```py $ ./python_set_literal.py <class 'set'> {1, 2, 3, 4} ``` 這是輸出。 ## Python 集函數 Python `set()`函數創建一個新集,其元素來自可迭代對象。 可迭代對象是我們可以迭代的對象; 例如字符串或列表。 `python_set_fun.py` ```py #!/usr/bin/python3 seasons = ["spring", "summer", "autumn", "winter"] myset = set(seasons) print(myset) ``` 在示例中,我們使用`set()`內置函數從列表創建了一個集。 ```py $ ./python_set_fun.py {'summer', 'autumn', 'winter', 'spring'} ``` 這是輸出。 ## Python 集成員性測試 `in`和`not in`運算符測試集中元素的存在。 `python_set_membership.py` ```py #!/usr/bin/python3 words = { "spring", "table", "cup", "bottle", "coin" } word = 'cup' if (word in words): print("{0} is present in the set".format(word)) else: print("{0} is not present in the set".format(word)) word = 'tree' if (word not in words): print("{0} is not present in the set".format(word)) else: print("{0} is present in the set".format(word)) ``` 我們使用成員運算符檢查集中是否存在兩個單詞。 ```py $ ./python_set_membership.py cup is present in the set tree is not present in the set ``` 這是輸出。 ## Python 集內置函數 有幾個內置 Python 函數,例如`len()`或`min()`,可以在 Python 集上使用。 `python_set_builtins.py` ```py #!/usr/bin/python3 nums = { 21, 11, 42, 29, 22, 71, 18 } print(nums) print("Number of elements: {0}".format(len(nums))) print("Minimum: {0}".format(min(nums))) print("Maximum: {0}".format(max(nums))) print("Sum: {0}".format(sum(nums))) print("Sorted elements:") print(sorted(nums)) ``` 在示例中,我們對一組整數值應用了五個內置函數。 ```py print("Number of elements: {0}".format(len(nums))) ``` `len()`方法返回集中的元素數。 ```py print("Minimum: {0}".format(min(nums))) ``` `min()`方法返回集中的最小值。 ```py print("Maximum: {0}".format(max(nums))) ``` `max()`方法返回集中的最大值。 ```py print("Sum: {0}".format(sum(nums))) ``` `sum()`方法返回集中值的總和。 ```py print(sorted(nums)) ``` 最后,使用`sorted()`方法,我們可以從集中創建無序列表。 ```py $ ./python_set_builtins.py {71, 42, 11, 18, 21, 22, 29} Number of elements: 7 Minimum: 11 Maximum: 71 Sum: 214 Sorted elements: [11, 18, 21, 22, 29, 42, 71] ``` 這是輸出。 ## Python 集迭代 可以使用`for`循環來迭代 Python 集。 `python_set_iteration.py` ```py #!/usr/bin/python3 words = { "spring", "table", "cup", "bottle", "coin" } for word in words: print(word) ``` 在示例中,我們遍歷該集并逐個打印其元素。 ```py $ ./python_set_iteration.py table cup coin spring bottle ``` 這是輸出。 ## Python 集添加 Python set `add()`方法將新元素添加到集中。 `python_set_add.py` ```py #!/usr/bin/python3 words = { "spring", "table", "cup", "bottle", "coin" } words.add("coffee") print(words) ``` 我們有一套話。 我們使用`add()`方法添加一個新單詞。 ```py $ ./python_set_add.py {'table', 'coffee', 'coin', 'spring', 'bottle', 'cup'} ``` 這是輸出。 ## Python 集更新 Python set `update()`方法將一個或多個可迭代對象添加到集中。 `python_set_update.py` ```py #!/usr/bin/python3 words = { "spring", "table", "cup", "bottle", "coin" } words.add("coffee") print(words) words2 = { "car", "purse", "wind" } words3 = { "nice", "prime", "puppy" } words.update(words2, words3) print(words) ``` 我們有三組單詞。 我們使用`update()`方法將第二組和第三組添加到第一組。 ```py $ ./python_set_update.py {'spring', 'bottle', 'cup', 'coin', 'purse', 'wind', 'nice', 'car', 'table', 'prime', 'puppy'} ``` 這是輸出。 ## Python 集刪除 Python 有兩種刪除元素的基本方法:`remove()`和`discard()`。 `remove()`方法從集中刪除指定的元素,如果元素不在集中,則提高`KeyError`。 `discard()`方法從集中刪除元素,如果要刪除的元素不在集中,則不執行任何操作。 `python_set_remove.py` ```py #!/usr/bin/python3 words = { "spring", "table", "cup", "bottle", "coin" } words.discard("coin") words.discard("pen") print(words) words.remove("cup") try: words.remove("cloud") except KeyError as e: pass print(words) ``` 在示例中,我們使用`remove()`和`discard()`刪除集元素。 ```py try: words.remove("cloud") except KeyError as e: pass ``` 如果我們沒有抓住`KeyError`,腳本將終止而不執行最后一條語句。 ```py $ ./python_set_remove.py {'table', 'cup', 'bottle', 'spring'} {'table', 'bottle', 'spring'} ``` 這是輸出。 ## Python 集彈出&清除 `pop()`方法從集中移除并返回任意元素。 `clear()`方法從集中刪除所有元素。 `python_set_remove2.py` ```py #!/usr/bin/python3 words = { "spring", "table", "cup", "bottle", "coin", "pen", "water" } print(words.pop()) print(words.pop()) print(words) words.clear() print(words) ``` 在示例中,我們刪除并打印兩個隨機元素,并顯示其余元素。 然后,使用`clear()`從集中刪除所有元素。 ```py $ ./python_set_remove2.py water pen {'cup', 'spring', 'table', 'bottle', 'coin'} set() ``` 這是輸出。 ## Python 集運算 使用 Python 集,我們可以執行特定的運算:并集,交集,差和對稱差。 `python_set_operations.py` ```py #!/usr/bin/python3 set1 = { 'a', 'b', 'c', 'c', 'd' } set2 = { 'a', 'b', 'x', 'y', 'z' } print("Set 1:", set1) print("Set 2:", set2) print("intersection:", set1.intersection(set2)) print("union:", set1.union(set2)) print("difference:", set1.difference(set2)) print("symmetric difference:", set1.symmetric_difference(set2)) ``` 該示例顯示了四個設置操作。 ```py print("intersection:", set1.intersection(set2)) ``` `intersection()`方法執行相交操作,該操作返回`set1`和`set2`中的元素。 ```py print("union:", set1.union(set2)) ``` `union()`方法執行聯合操作,該操作返回兩個集中的所有元素。 ```py print("difference:", set1.difference(set2)) ``` `difference()`方法執行差分操作,該操作返回`set1`中而不是`set2`中的元素。 ```py print("symmetric difference:", set1.symmetric_difference(set2)) ``` `symmetric_difference()`方法執行對稱差分操作,該操作返回`set1`或`set2`中的元素,但不返回兩者中的元素。 ```py $ ./python_set_operations.py Set 1: {'c', 'b', 'a', 'd'} Set 2: {'y', 'b', 'a', 'x', 'z'} intersection: {'b', 'a'} union: {'b', 'a', 'z', 'c', 'x', 'y', 'd'} difference: {'c', 'd'} symmetric difference: {'z', 'c', 'x', 'y', 'd'} ``` 這是一個示例輸出。 可以使用&,|,-和^運算符執行這些操作。 `python_set_operations2.py` ```py #!/usr/bin/python3 set1 = { 'a', 'b', 'c', 'c', 'd' } set2 = { 'a', 'b', 'x', 'y', 'z' } print("Set 1:", set1) print("Set 2:", set2) print("intersection:", set1 & set2) print("union:", set1 | set2) print("difference:", set1 - set2) print("symmetric difference:", set1 ^ set2) ``` 該示例顯示了使用運算符的四個`set`操作。 ## 子集和超集 如果集 A 的所有元素都包含在集 B 中,則將 A 稱為 B 的子集,將 B 稱為 A 的超集。 `python_subset_superset.py` ```py #!/usr/bin/python3 set1 = { 'a', 'b', 'c', 'd', 'e' } set2 = { 'a', 'b', 'c' } set3 = {'x', 'y', 'z' } if (set2.issubset(set1)): print("set2 is a subset of set1") if (set1.issuperset(set2)): print("set1 is a superset of set2") if (set2.isdisjoint(set3)): print("set2 and set3 have no common elements") ``` 在示例中,我們使用`issubset()`,`issuperset()`和`isdisjoint()`方法。 ```py if (set2.issubset(set1)): print("set1 is a subset of set2") ``` 使用`issubset()`方法,我們檢查`set2`是否是`s1`的子集。 ```py if (set1.issuperset(set2)): print("set1 is a superset of set2") ``` 使用`issuperset()`方法,我們檢查`set1`是否是`s2`的超集。 ```py if (set2.isdisjoint(set3)): print("set2 and set3 have no common elements") ``` 使用`isdisjoint()`方法,我們檢查`set2`和`set3`是否沒有共同的元素。 ```py $ ./python_subset_superset.py set1 is a subset of set2 set1 is a superset of set2 set2 and set3 have no common elements ``` 這是輸出。 ## Python 不可變集 不可變的集(無法修改的集)是使用`frozenset()`函數創建的。 ```py >>> s1 = frozenset(('blue', 'green', 'red')) >>> s1 frozenset({'red', 'green', 'blue'}) >>> s1.add('brown') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'frozenset' object has no attribute 'add' ``` 當我們嘗試向凍結集中添加新元素時出現錯誤。 在本教程中,我們使用了 Python `set`集。 您可能也對以下相關教程感興趣: [Python 教程](/lang/python/), [Python lambda 函數](/python/lambda/), [Python `for`循環](/python/forloop/), [Python 列表推導](/articles/pythonlistcomprehensions/), [Python 映射教程](/python/map/), [OpenPyXL 教程](/articles/openpyxl/), [Python Requests 教程](/web/pythonrequests/)和 [Python CSV 教程](/python/csv/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看