1. 程式人生 > >python-集合set

python-集合set

del update remove not in sub mov sym 不能 upd

set:本身非可哈希,但set裏面元素必須可哈希
非可哈希不能作為字典的鍵

set更新:
s.add(‘asdf‘)
s.update(‘asdf‘)
s.remove(‘a‘)
s.pop()#隨機刪除
s.clear()#清空s中元素,集合s還保留
del s#刪除集合s,什麽都不保留

a = set([1,2,3,4,5])
b = set([4,5,6,7,8])
a.intersection(b) #a和b的交集元素, a & b
a.union(b) #a和b的並集元素, a | b
a.difference(b)#差集 in a but not in b, a - b
a.symmetric_difference(b)#對稱差集/反向交集 not in (a.intersection(b)), a ^ b

a.issuperset(b)
a.issubset(b)

python-集合set