add(增加元素)
name = set(['Tom','Lucy','Ben']) name.add('Juny')print(name)#输出:{'Lucy', 'Juny', 'Ben', 'Tom'}
clear(清空所有元素)
name = set(['Tom','Lucy','Ben']) name.clear()print(name)#输出:set()
copy(拷貝set集合)
name = set(['Tom','Lucy','Ben']) new_name = name.copy()print(new_name)#输出:{'Tom', 'Lucy', 'Ben'}
difference(傳回兩個或多個集合中不同的元素,並產生新的集合)
A = set([2,3,4,5= set([3,4= set([2=
difference_update(刪除A集合裡面,在B集合中存在的元素。
A = set([2,3,4,5]) B = set([4,5]) A.difference_update(B)print(A)#输出:{2, 3}
isdisjoint(判斷交集,是回傳False,否回傳True)
n = set([2,3,4]) n.discard(3)print(n)#输出:{2, 4}
issubset(判斷子集)
n1 = set([2,3,4,5]) n2 = set([4,5,6,7]) n = n1.intersection(n2)print(n)#输出:{4, 5}
issuperset(判斷父集)
n1 = set([2,3,4,5]) n2 = set([4,5,6,7]) n1.intersection_update(n2)print(n1)#输出:{4, 5}
pop(隨機移除一個元素)
n1 = set([2,3,4,5]) n2 = set([4,5,6,7])print(n1.isdisjoint(n2))#输出:False
remove(移除指定元素)
A = set([2,3]) B = set([2,3,4,5])print(A.issubset(B))#输出:True#A是B的子集
symmetric_difference(取交集,並產生新的集合)
A = set([2,3]) B = set([2,3,4,5])print(B.issuperset(A))# 输出:True#B是A的父集
symmetric_difference_update(取交集,改變原來的集合)
n = set([2,3,4,5]) n1 = n.pop()print(n,n1)# 输出:{3, 4, 5} 2
union
(取並集,並產生新的集合)
n = set([2,3,4,5]) n.remove(2)print(n)# 输出:{3, 4, 5}
update(取並集,改變原來的集合)
A = set([2,3,4,5]) B = set([4,5,6,7])print(A.symmetric_difference(B))# 输出:{2, 3, 6, 7}
以上是舉例說明python3 set方法功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!