內建集合類型。集合是可變更且可列舉的不重複值集合,也就是集合的元素。集合的類型名稱為 "set"
。
集合可提供常數時間作業,用於插入、移除或檢查值是否存在。集合是使用雜湊表實作,因此就像字典的鍵一樣,集合的元素必須可雜湊。只有在可用做為字典的鍵時,才能將值用做集合的元素。
您可以使用 set()
內建函式建構集合,該函式會傳回包含選用引數的唯一元素的新集合,且該引數必須是可迴圈的項目。在沒有引數的情況下呼叫 set()
會建立空集。集合沒有文字語法。
in
和 not in
作業會檢查值是否屬於某個集合:
s = set(["a", "b", "c"]) "a" in s # True "z" in s # False
集合是可枚舉的,因此可用做 for
迴圈、清單理解式和各種內建函式的運算元,這些函式會在可枚舉項目上運作。您可以使用 len()
內建函式擷取長度,而迭代順序則是元素首次加入集合的順序:
s = set(["z", "y", "z", "y"]) len(s) # prints 2 s.add("x") len(s) # prints 3 for e in s: print e # prints "z", "y", "x"
在布林值情境中使用的集合,只有在非空時才會設為 true。
s = set() "non-empty" if s else "empty" # "empty" t = set(["x", "y"]) "non-empty" if t else "empty" # "non-empty"
您可以使用 ==
和 !=
比較集合的相等或不等情形。只有在 t
是包含相同元素的集合時,s
才會等於 t
;迭代順序不具意義。具體來說,集合「不等於」其元素的清單。集合不會依據其他集合排序,因此嘗試使用 <
、<=
、>
、>=
比較兩個集合,或嘗試排序一組集合,都會失敗。
set() == set() # True set() != [] # True set([1, 2]) == set([2, 1]) # True set([1, 2]) != [1, 2] # True
對兩個集合執行 |
運算會傳回兩個集合的並集:包含在原始集合中找到的元素的集合。
set([1, 2]) | set([3, 2]) # set([1, 2, 3])
對兩個集合執行 &
運算會傳回兩個集合的交集:一個集合,只包含在兩個原始集合中找到的元素。
set([1, 2]) & set([2, 3]) # set([2]) set([1, 2]) & set([3, 4]) # set()
對兩個集合執行 -
運算會傳回兩個集合的差異:包含左側集合中元素,但不包含右側集合中元素的集合。
set([1, 2]) - set([2, 3]) # set([1]) set([1, 2]) - set([3, 4]) # set([1, 2])
對兩個集合執行 ^
運算會傳回兩個集合的對稱差異:包含兩個原始集合中任一集合的元素,但不包含兩個集合的元素。
set([1, 2]) ^ set([2, 3]) # set([1, 3]) set([1, 2]) ^ set([3, 4]) # set([1, 2, 3, 4])
在上述每項運算中,結果集的元素會保留兩個運算元組的順序,從左側繪製的所有元素會排在只出現在右側的任何元素之前。
對應的擴充指派作業 |=
、&=
、-=
和 ^=
會修改左側集合。
s = set([1, 2]) s |= set([2, 3, 4]) # s now equals set([1, 2, 3, 4]) s &= set([0, 1, 2, 3]) # s now equals set([1, 2, 3]) s -= set([0, 1]) # s now equals set([2, 3]) s ^= set([3, 4]) # s now equals set([2, 4])
與 Starlark 中的所有可變動值一樣,集合可以凍結,一旦凍結,所有後續嘗試更新集合的作業都會失敗。
成員
- 新增
- clear
- 差異
- difference_update
- discard
- 交集
- intersection_update
- isdisjoint
- issubset
- issuperset
- pop
- 移除
- symmetric_difference
- symmetric_difference_update
- union
- 更新
add
None
set.add(element)
您可以 add
已在集合中存在的值,這不會影響集合。
如果您需要在集合中新增多個元素,請參閱 update
或 |=
加值指派作業。
參數
參數 | 說明 |
---|---|
element
|
必填 要新增的元素。 |
關閉
None
set.clear()
差值
set set.difference(*others)
如果 s
和 t
是集合,s.difference(t)
就等同於 s - t
;不過請注意,-
作業要求兩側都必須是集合,而 difference
方法也接受序列和字典。
您可以不帶任何引數呼叫 difference
,這會傳回集合的副本。
例如:
set([1, 2, 3]).difference([2]) # set([1, 3]) set([1, 2, 3]).difference([0, 1], [3, 4]) # set([2])
參數
參數 | 說明 |
---|---|
others
|
必要 Set、可雜湊元素序列或字典。 |
difference_update
None
set.difference_update(*others)
如果 s
和 t
是集合,s.difference_update(t)
就等同於 s -= t
;不過,請注意,-=
增強型指派需要兩側都為集合,而 difference_update
方法也接受序列和字典。
您可以不帶任何引數呼叫 difference_update
,這樣做不會變更集合。
例如:
s = set([1, 2, 3, 4]) s.difference_update([2]) # None; s is set([1, 3, 4]) s.difference_update([0, 1], [4, 5]) # None; s is set([3])
參數
參數 | 說明 |
---|---|
others
|
必要 Set、可雜湊元素序列或字典。 |
捨棄
None
set.discard(element)
您可以 discard
不存在於集合中的值,這樣集合就不會有所變動。如果您想在移除不存在的元素時失敗,請改用 remove
。如果您需要從集合中移除多個元素,請參閱 difference_update
或 -=
擴充指派作業。
例如:
s = set(["x", "y"]) s.discard("y") # None; s == set(["x"]) s.discard("y") # None; s == set(["x"])
參數
參數 | 說明 |
---|---|
element
|
必要 要捨棄的元素。必須可進行雜湊運算。 |
交集
set set.intersection(*others)
如果 s
和 t
是集合,s.intersection(t)
就等同於 s & t
;不過請注意,&
作業要求兩側都必須是集合,而 intersection
方法也接受序列和字典。
您可以不帶任何引數呼叫 intersection
,這會傳回集合的副本。
例如:
set([1, 2]).intersection([2, 3]) # set([2]) set([1, 2, 3]).intersection([0, 1], [1, 2]) # set([1])
參數
參數 | 說明 |
---|---|
others
|
必要 Set、可雜湊元素序列或字典。 |
intersection_update
None
set.intersection_update(*others)
如果 s
和 t
是集合,s.intersection_update(t)
就等同於 s &= t
;不過,請注意,&=
增強型指派需要兩側都是集合,而 intersection_update
方法也接受序列和字典。
您可以不帶任何引數呼叫 intersection_update
,這樣做不會變更集合。
例如:
s = set([1, 2, 3, 4]) s.intersection_update([0, 1, 2]) # None; s is set([1, 2]) s.intersection_update([0, 1], [1, 2]) # None; s is set([1])
參數
參數 | 說明 |
---|---|
others
|
必要 Set、可雜湊元素序列或字典。 |
isdisjoint
bool set.isdisjoint(other)
例如:
set([1, 2]).isdisjoint([3, 4]) # True set().isdisjoint(set()) # True set([1, 2]).isdisjoint([2, 3]) # False
參數
參數 | 說明 |
---|---|
other
|
必要 集合、可雜湊元素序列或字典。 |
issubset
bool set.issubset(other)
請注意,一組一律會視為自身的子集。
例如:
set([1, 2]).issubset([1, 2, 3]) # True set([1, 2]).issubset([1, 2]) # True set([1, 2]).issubset([2, 3]) # False
參數
參數 | 說明 |
---|---|
other
|
必要 集合、可雜湊元素序列或字典。 |
issuperset
bool set.issuperset(other)
請注意,集合一律會視為自身的超集。
例如:
set([1, 2, 3]).issuperset([1, 2]) # True set([1, 2, 3]).issuperset([1, 2, 3]) # True set([1, 2, 3]).issuperset([2, 3, 4]) # False
參數
參數 | 說明 |
---|---|
other
|
必要 集合、可雜湊元素序列或字典。 |
流行
unknown set.pop()
如果集合為空白,則會失敗。
例如:
s = set([3, 1, 2]) s.pop() # 3; s == set([1, 2]) s.pop() # 1; s == set([2]) s.pop() # 2; s == set() s.pop() # error: empty set
移除
None
set.remove(element)
如果集合中沒有元素,remove
就會失敗。如果您不希望在嘗試移除不存在的元素時失敗,請改用 discard
。如果您需要從集合中移除多個元素,請參閱 difference_update
或 -=
擴充指派作業。
參數
參數 | 說明 |
---|---|
element
|
必填 要移除的元素。必須是集合的元素 (且可進行雜湊運算)。 |
symmetric_difference
set set.symmetric_difference(other)
如果 s
和 t
是集合,s.symmetric_difference(t)
就等同於 s ^ t
;不過請注意,^
作業要求兩側都必須是集合,而 symmetric_difference
方法也接受序列或字典。
例如:
set([1, 2]).symmetric_difference([2, 3]) # set([1, 3])
參數
參數 | 說明 |
---|---|
other
|
必要 集合、可雜湊元素序列或字典。 |
symmetric_difference_update
None
set.symmetric_difference_update(other)
如果 s
和 t
是集合,s.symmetric_difference_update(t)
就等同於 `s ^= t; however, note that the
^=` 加強型指派需要兩側皆為集合,而 symmetric_difference_update
方法也接受序列或字典。
例如:
s = set([1, 2]) s.symmetric_difference_update([2, 3]) # None; s == set([1, 3])
參數
參數 | 說明 |
---|---|
other
|
必要 集合、可雜湊元素序列或字典。 |
聯集
set set.union(*others)
如果 s
和 t
是集合,s.union(t)
就等同於 s | t
;不過請注意,|
作業要求兩側都必須是集合,而 union
方法也接受序列和字典。
您可以不帶任何引數呼叫 union
,這會傳回集合的副本。
例如:
set([1, 2]).union([2, 3]) # set([1, 2, 3]) set([1, 2]).union([2, 3], {3: "a", 4: "b"}) # set([1, 2, 3, 4])
參數
參數 | 說明 |
---|---|
others
|
必要 Set、可雜湊元素序列或字典。 |
update
None
set.update(*others)
例如:
s = set() s.update([1, 2]) # None; s is set([1, 2]) s.update([2, 3], [3, 4]) # None; s is set([1, 2, 3, 4])
如果 s
和 t
是集合,s.update(t)
就等同於 s |= t
;不過,請注意,|=
增強型指派需要兩側都是集合,而 update
方法也接受序列和字典。
您可以不使用任何引數呼叫 update
,這樣做不會變更集合。
參數
參數 | 說明 |
---|---|
others
|
必要 Set、可雜湊元素序列或字典。 |