Python 3.1 快速導覽 - 集合的 union()

集合 (set) 與原封集合 (frozenset) 型態 (type) 的 union() 方法 (method) ,取得 s1 與 s2 的聯集

方法描述
s1.union(s2)等於 s1 | s2


舉例示範如下
a = {"a", "b", "c"}
print(a)
print(type(a))
print()

b = frozenset({"a", "b", "c", "1", "2"})
print(b)
print(type(b))
print()

print(a.union(b))
print(b.union(a))

# 《程式語言教學誌》的範例程式
# http://pydoing.blogspot.com/
# 檔名:union.py
# 功能:示範 Python 程式 
# 作者:張凱慶
# 時間:西元 2010 年 12 月


執行結果如下



中英文術語對照
集合set
原封集合frozenset
型態type
方法method


內建型態




沒有留言: