共計 307 個字符,預計需要花費 1 分鐘才能閱讀完成。
set() 函數用于創建一個集合(set),集合是無序、不重復的元素的集合。
使用方法如下:
- 創建一個空集合:
my_set = set()
- 創建一個包含元素的集合:
my_set = set([1, 2, 3, 4, 5])
- 添加元素到集合中:
my_set.add(6)
- 刪除元素從集合中:
my_set.remove(1)
- 集合的操作:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2) # 求并集
intersection_set = set1.intersection(set2) # 求交集
difference_set = set1.difference(set2) # 求差集
丸趣 TV 網 – 提供最優質的資源集合!
正文完