這篇文章是閱讀Asabeneh的30 Days Of Python: Day 7 - Sets後的學習筆記與心得。
集合中的元素是無順序(unordered)及無索引(un-indexed)的,並且集合中的項目是無法被修改(change)的。
JavaScript(以下簡稱JS)中也有類似的物件類型 - Set。
在Python中這種資料型別會用來儲存具唯一性(unique)的項目(items),並能用來尋找聯集(union)、交集(intersection),差集(difference)以及對稱差集(symmetric difference),子集(subset),超集合(super set)和併查集(disjoin set)。
{}
或是set()
函式來建立一個新的空集合empty_set = {}
empty_set = set()
characters = {"a", "b", "c"}
使用len()
函式:
* 原文是寫方法(method),但官方文件中是放在Build-in Functions下
characters = {"a", "b", "c"}
len(characters) # 3
與串列及元組相同,使用in
運算符:
characters = {"a", "b", "c"}
print("a" in characters) # True
print("p" in characters) # False
add(item)
新增一個項目:characters = {"a", "b", "c"}
characters.add("d")
print(characters) # {'d', 'a', 'b', 'c'}
update(item1, item2...)
新增多個項目:characters = {"d", "a", "b", "c"}
characters.update("e", "f", "g")
print(characters) # {'a', 'b', 'e', 'd', 'g', 'f', 'c'}
使用remove(item)
方法,如果該項目不存在於集合中,將產生KeyError
:
characters = {'a', 'b', 'e', 'd', 'g', 'f', 'c'}
characters.remove("a")
print(characters) # {'b', 'e', 'd', 'g', 'f', 'c'}
characters.remove("k") # KeyError
使用discard(item)
方法,如果該項目不存在也不會產生Error
:
characters = {'a', 'b', 'e', 'd', 'g', 'f', 'c'}
characters.discard("a")
print(characters) # {'e', 'f', 'b', 'd', 'c', 'g'}
characters.discard("k") # nothing happens
使用pop()
方法,移除隨機的一個集合項目,並會回傳該移除的項目,如果集合為空,產生KeyError
:
characters = {'e', 'f', 'b', 'd', 'c', 'g'}
print(characters.pop()) # e (it's random)
print(characters) # {'f', 'b', 'd', 'c', 'g'}
使用clear(set)
,把集合內的項目都移除:
characters = {'f', 'b', 'd', 'c', 'g'}
characters.clear()
print(characters) # set()
使用del
運算符:
characters = set()
del characters
使用set(list)
函式,若串列中原有重複的元素,轉換後只會保留一個,即集合中不會有重複的項目:
polls = ["hiking", "swimming", "hiking", "fitting"]
options = set(polls)
print(options) # {'hiking', 'fitting', 'swimming'}
使用union(set)
方法,會回傳一個新的集合:
weekend = {"sunday", "saturday"}
workday = {"monday", "tuesday", "wednesday", "thursday", "friday"}
week = workday.union(weekend)
print(week) # {'thursday', 'friday', 'tuesday', 'wednesday', 'saturday', 'sunday', 'monday'}
print(workday) # {'thursday', 'friday', 'tuesday', 'wednesday', 'monday'}
使用update(set)
方法:
weekend = {"sunday", "saturday"}
workday = {"monday", "tuesday", "wednesday", "thursday", "friday"}
workday.update(weekend) # weekend days are added to workday (oh bother!)
print(workday) # {'thursday', 'friday', 'tuesday', 'wednesday', 'saturday', 'sunday', 'monday'}
使用intersection
方法,回傳兩個集合中皆有的項目:
strong = {"java", "c#", "python"}
static = {"c#", "java"}
print(strong.intersection(static)) # {'c#', 'java'}
使用issubset(set)
方法,回傳True
如果set
是某集合的子集,否則回傳False
。
使用issuperset(set)
方法,回傳True
如果set
是某集合的超集合,否則回傳False
。
week = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}
workday = {"monday", "tuesday", "wednesday", "thursday", "friday"}
# week contains all items in the workday
week.issuperset(workday) # True
# content of workday just a part of the week
week.issubset(workday) # False
workday.issubset(week) # True
使用difference(set)
方法,回傳set
與某集合的差集:
rgba = {"red", "green", "blue", "alpha"}
hlsa = {"hue", "lightness", "saturation", "alpha"}
print(rgba.difference(hlsa)) # {'green', 'red', 'blue'} - the result is unordered
使用symmetric_difference(set)
方法,回傳set
與某集合的對稱差集:
rgba = {"red", "green", "blue", "alpha"}
hlsa = {"hue", "lightness", "saturation", "alpha"}
print(rgba.symmetric_difference(hlsa)) # {'green', 'saturation', 'lightness', 'red', 'hue', 'blue'}
使用isdisjoint
,回傳True
如果兩集合沒有任何相同的項目,否則回傳False
:
rgba = {"red", "green", "blue", "alpha"}
hlsa = {"hue", "lightness", "saturation", "alpha"}
rgba.isdisjoint(hlsa) # False - have 'alpha' in common
rgb = {"red", "green", "blue"}
rgb.isdisjoint(hlsa) # True