A set object is an unordered collection of distinct hashtable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.
Like other collections, sets support x in set
, len(set)
, and for x in set
. Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.
operations:
len(x) #长度 'i' in x s.issubset(x) #s是否为x的子集 x.union(y) #交 x.difference(y) #差 x.symmetric_difference(y) #对称差 s.update(x) #更新s,加上x中的元素 s.add() #增加元素 s.remove() #删除已有元素,如果没有会返回异常 s.discard(2) #如果存在元素,就删除;没有不报异常 s.clear() #清除set x.pop() #随机删除一元素