使用者:Xyy23330121/Python/集合
Python 中,集合是無序的、由多個不同元素組成的結構。其中 set 是可變的,而 frozenset 是不可變的。因此,frozenset 可以作為字典的鍵,而 set 不能。
當嘗試向集合添加集合中已有的元素,或者在創建集合時使用了重複的元素。則只會保留重複元素中的一個,其餘的會被忽略。
集合是可迭代對象,即便它沒有明確的順序。進行迭代時,它會按照一個隨機的順序迭代元素,直至所有元素都進行過迭代為止。
創建集合[編輯 | 編輯原始碼]
Python 創建集合主要有三種方式:
- 字面格式:
newSet = {'str', 1, False}
- 推導式:
newSet = {c for c in 'Python' if c not in 'py'}
- set函數:對任意可迭代對象
iterable
,用set(iterable)
或frozenset(iterable)
特別的,可以用set()
或frozenset()
創建空字典。
嵌套[編輯 | 編輯原始碼]
列表、元組等結構也可以作為集合的元素出現。特別的,集合也可以作為集合的元素。
集合的操作[編輯 | 編輯原始碼]
集合元素數量[編輯 | 編輯原始碼]
len(s)[編輯 | 編輯原始碼]
返回集合 s
中元素的數量。
屬於 / 不屬於[編輯 | 編輯原始碼]
in / not in[編輯 | 編輯原始碼]
可以用 x in s
或 x not in s
判斷元素是否在集合中。比較運算符可以串聯。
含於 / 真含於[編輯 | 編輯原始碼]
s <= other[編輯 | 編輯原始碼]
如果集合 s
含於集合 other
則返回 True
。反之返回 False
。比較運算符可以串聯。
s.issubset(other)[編輯 | 編輯原始碼]
同 s <= other
。但此時 other
可以為任何可用 set
函數或 frozenset
函數轉變為集合的類型。
s < other[編輯 | 編輯原始碼]
如果集合 s
真含於集合 other
則返回 True
。反之返回 False
。比較運算符可以串聯。
s >= other[編輯 | 編輯原始碼]
如果集合 other
含於集合 s
則返回 True
。反之返回 False
。比較運算符可以串聯。
s.issuperset(other)[編輯 | 編輯原始碼]
同 s >= other
。但此時 other
可以為任何可用 set
函數或 frozenset
函數轉變為集合的類型。
s > other[編輯 | 編輯原始碼]
如果集合 other
真含於集合 s
則返回 True
。反之返回 False
。比較運算符可以串聯。
交集[編輯 | 編輯原始碼]
s.isdisjoint(other)[編輯 | 編輯原始碼]
如果集合 s
和集合 other
交集為空,則返回 True
。反之返回 False
。
s & other & ...[編輯 | 編輯原始碼]
返回該表達式中所有集合的交集。返回的集合是 set
還是 frozenset
,取決於 s
的類型。
s.intersection(other, ...)[編輯 | 編輯原始碼]
同 s & other & ...
。但此時 other
可以為任何可用 set
函數或 frozenset
函數轉變為集合的類型。
併集[編輯 | 編輯原始碼]
s | other | ...[編輯 | 編輯原始碼]
返回該表達式中所有集合的併集。返回的集合是 set
還是 frozenset
,取決於 s
的類型。
s.union(other, ...)[編輯 | 編輯原始碼]
同 s | other | ...
。但此時 other
可以為任何可用 set
函數或 frozenset
函數轉變為集合的類型。
集合的差[編輯 | 編輯原始碼]
s - other - ...[編輯 | 編輯原始碼]
如果集合 s
和其它所有集合 other, ...
的差。即:返回一個新集合,該集合包含在 s
中、但不在 other | ...
中的元素。
返回的集合是 set
還是 frozenset
,取決於 s
的類型。
s.difference(other, ...)[編輯 | 編輯原始碼]
同 s - other - ...
。但此時 other
可以為任何可用 set
函數或 frozenset
函數轉變為集合的類型。
集合的對稱差[編輯 | 編輯原始碼]
s ^ other[編輯 | 編輯原始碼]
返回一個集合,該集合中的元素屬於 s.union(other)
但不屬於 s.difference(other)
返回的集合是 set
還是 frozenset
,取決於 s
的類型。
s.symmetric_difference(other)[編輯 | 編輯原始碼]
同 s ^ other
。但此時 other
可以為任何可用 set
函數或 frozenset
函數轉變為集合的類型。
s.copy()[編輯 | 編輯原始碼]
返回集合 s
的拷貝。
可變集合的特有操作[編輯 | 編輯原始碼]
以下操作中,s
均為 set
對象。而不能是 frozenset
對象
賦值並[編輯 | 編輯原始碼]
s |= other | ...[編輯 | 編輯原始碼]
在集合 s
中添加所有屬於 other | ...
的元素。
s.update(other, ...)[編輯 | 編輯原始碼]
同 s |= other | ...
。但此時 other
可以為任何可用 set
函數或 frozenset
函數轉變為集合的類型。
賦值交[編輯 | 編輯原始碼]
s &= other & ...[編輯 | 編輯原始碼]
從集合 s
中移除所有不屬於 other & ...
的元素。
s.intersection_update(other, ...)[編輯 | 編輯原始碼]
同 s &= other & ...
。但此時 other
可以為任何可用 set
函數或 frozenset
函數轉變為集合的類型。
賦值差[編輯 | 編輯原始碼]
s -= other | ...[編輯 | 編輯原始碼]
從集合 s
中移除所有屬於 other | ...
的元素。
s.difference_update(other, ...)[編輯 | 編輯原始碼]
同 s -= other | ...
。但此時 other
可以為任何可用 set
函數或 frozenset
函數轉變為集合的類型。
賦值對稱差[編輯 | 編輯原始碼]
s ^= other[編輯 | 編輯原始碼]
更新 s
,使其等於 s ^ other
s.symmetric_difference_update(other)[編輯 | 編輯原始碼]
同 s ^= other
。但此時 other
可以為任何可用 set
函數或 frozenset
函數轉變為集合的類型。
添加 / 刪除元素[編輯 | 編輯原始碼]
s.add(elem)[編輯 | 編輯原始碼]
將 elem
添加到集合 s
中。
s.remove(elem)[編輯 | 編輯原始碼]
將 elem
從集合 s
中移除,如果集合中沒有 elem
,則報錯 KeyError
。
s.iscard(elem)[編輯 | 編輯原始碼]
將 elem
從集合 s
中移除。如果集合中沒有 elem
,則不做任何操作。
s.pop(elem)[編輯 | 編輯原始碼]
從集合 s
中任意移除一個元素並返回。如果集合為空,則報錯 KeyError
。
s.clear()[編輯 | 編輯原始碼]
將集合 s
變為空集。