python-集合set

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)

猜你喜欢

转载自www.cnblogs.com/benchdog/p/8994416.html