設定

回報問題 查看來源 Nightly · 8.4 · 8.3 · 8.2 · 8.1 · 8.0 · 7.6

內建的集合類型。集合是可變動、可疊代的唯一值集合,也就是集合的元素。集合的型別名稱"set"

集合提供常數時間作業,可插入、移除或檢查值是否存在。集合是使用雜湊表實作,因此與字典的鍵一樣,集合的元素必須可雜湊。只有在值可做為字典的鍵時,才能做為集合的元素。

集合可使用 set() 內建函式建構,該函式會傳回新的集合,其中包含選用引數的不重複元素,且引數必須是可疊代項目。呼叫不含引數的 set() 會建構空集合。集合沒有常值語法。

innot 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 才會等於 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 中所有可變動的值一樣,集合可以凍結,凍結後,所有嘗試更新集合的後續作業都會失敗。

成員

add

None set.add(element)

將元素新增至集合。

允許 add 集合中已有的值,但集合不會因此變更。

如要將多個元素新增至一組,請參閱 update|= 擴增指派作業。

參數

參數 說明
element 必要
要新增的元素。

關閉

None set.clear()

移除集合的所有元素。

差值

set set.difference(*others)

傳回新的可變動集合,內含這個集合與其他集合的差異。

如果 st 是集合,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 必要
可雜湊元素的集合、序列或字典。

difference_update

None set.difference_update(*others)

Removes any elements found in any others from this set.

如果 st 是集合,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 必要
可雜湊元素的集合、序列或字典。

捨棄

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)

傳回新的可變動集合,其中包含這個集合與其他集合的交集。

如果 st 是集合,則 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 必要
可雜湊元素的集合、序列或字典。

intersection_update

None set.intersection_update(*others)

從這個集合中移除所有其他集合中沒有的元素。

如果 st 是集合,則 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 必要
可雜湊元素的集合、序列或字典。

isdisjoint

bool set.isdisjoint(other)

如果這個集合與另一個集合沒有共同元素,則傳回 true。

例如:

set([1, 2]).isdisjoint([3, 4])  # True
set().isdisjoint(set())         # True
set([1, 2]).isdisjoint([2, 3])  # False

參數

參數 說明
other 必要
一組可雜湊的元素、序列或字典。

issubset

bool set.issubset(other)

如果這個集合是另一個集合的子集,則傳回 true。

請注意,集合一律視為自身的子集。

例如:

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)

如果這個集合是另一個集合的超集,則傳回 true。

請注意,集合一律視為自身的超集。

例如:

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)

傳回新的可變動集合,其中包含這個集合與另一個集合、序列或字典的對稱差集。

如果 st 是集合,則 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)

傳回新的可變動集合,其中包含這個集合與另一個集合、序列或字典的對稱差集。

如果 st 是集合,則 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)

傳回新的可變動集合,其中包含這個集合與其他集合的聯集。

如果 st 是集合,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 必要
可雜湊元素的集合、序列或字典。

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])

如果 st 是集合,s.update(t) 就等同於 s |= t;不過請注意,|= 擴增指派需要兩側都是集合,而 update 方法也接受序列和字典。

您可以呼叫不含任何引數的 update,這樣一來,集合就不會變更。

參數

參數 說明
others 必要
可雜湊元素的集合、序列或字典。