python-set集合
Python set集合
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.
sets 支持 x in set, len(set), 和 for x in set。作为一个无序的集合,sets 不记录元素位置或者插入点。因此,sets 不支持 indexing, slicing, 或其它类序列(sequence-like)的操作。
>>> a = {'111','222',333} >>> a {'111', '222', 333} >>> type(a) <class 'set'> >>> >>> s = ['1','1','2','3','2233'] >>> s ['1', '1', '2', '3', '2233'] >>> b = set(s) >>> b {'2233', '2', '1', '3'} >>> type(b) <class 'set'> >>> b. b.add( b.intersection( b.remove( b.clear( b.intersection_update( b.symmetric_difference( b.copy( b.isdisjoint( b.symmetric_difference_update( b.difference( b.issubset( b.union( b.difference_update( b.issuperset( b.update( b.discard( b.pop( >>> b.add('666')
add增加一个
>>> a = {'1','2','3'} >>> type(a) <class 'set'> >>> a.add('4') >>> a {'1', '3', '4', '2'} >>> a.add('5','6') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: add() takes exactly one argument (2 given) >>> >>> a.add('2') >>> a {'3', '2', '4', '1'} >>>
update增加多个,参数是list
>>> a {'3', '2', '4', '1'} >>> a.update('5','6') >>> a {'1', '5', '6', '4', '2', '3'} >>> a.update('7','8') >>> a {'1', '7', '8', '5', '6', '4', '2', '3'} >>> >>> b = {'11','22'} >>> a {'1', '7', '8', '5', '6', '4', '2', '3'} >>> a.add(b) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'set' >>> a.update(b) >>> a {'1', '11', '7', '8', '5', '6', '4', '2', '3', '22'} >>>
remove()用于删除一个set中的元素
这个值在set中必须存在,如果不存在的话,会引发KeyError错误。
>>> a {'1', '11', '7', '8', '5', '6', '4', '2', '3', '22'} >>> a.remove('1','2','3') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: remove() takes exactly one argument (3 given) >>> a.remove('1') >>> a {'11', '7', '8', '5', '6', '4', '2', '3', '22'} >>> a.remove('33') Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: '33' >>>
discard()用于删除一个set中的元素
这个值不必一定存在,不存在的情况下删除也不会触发错误
>>> a {'11', '7', '8', '5', '6', '4', '2', '3', '22'} >>> a.discard('44') >>> a.discard('2','3') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: discard() takes exactly one argument (2 given) >>> a.discard('2') >>> a {'11', '7', '8', '5', '6', '4', '3', '22'} >>>
pop随机删除
从开始。这个函数随机返回一个元素值,然后把这个值删除,如果set为空,调用这个函数会返回Key错误。
>>> a {'11', '7', '8', '5', '6', '4', '3', '22'} >>> a.pop() '11' >>> a.pop() '7' >>> a.pop('5','6') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: pop() takes no arguments (2 given) >>> a.pop('5') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: pop() takes no arguments (1 given) >>> >>> a set() >>> a.pop() Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'pop from an empty set' >>> >>> a {'22', '4', '5', '11', '6', '8', '7', '3'} >>> re = a.pop() >>> re '22' >>> #移除元素,并且可以把移除的元素赋值给另一个变量
clear清空
>>> a {'8', '5', '6', '4', '3', '22'} >>> a.clear() >>> a set()
intersection 交集
>>> a {'4', '5', '11', '6', '8', '7', '3'} >>> b {'8', '6', '7'} >>> c = a.intersection(b) >>> c {'8', '6', '7'} >>>
intersection_update 取出交集并更新
>>> a {'4', '5', '11', '6', '8', '7', '3'} >>> b {'8', '6', '7'} >>> a.intersection_update(b) >>> a {'8', '6', '7'} >>>
in/not in
>>> '8' in a True >>> '9' not in a True >>> '8' not in a False >>>
difference 不在集合中
>>> a {'222', '6', '111', '8', '7'} >>> b {'777', '6', '8', '666', '7'} >>> a.difference(b) {'111', '222'} >>> b.difference(a) {'777', '666'} >>>
symmetric_difference 差集
>>> a {'222', '6', '111', '8', '7'} >>> b {'777', '6', '8', '666', '7'} >>> a.symmetric_difference(b) {'222', '777', '111', '666'} >>>
issubset和issuperset 集合元素是否在另一个集合中
>>> a {'222', '6', '111', '8', '7'} >>> b {'777', '6', '8', '666', '7'} >>> c = {'6','7','8'} >>> c.issubset(a) True >>> c.issubset(b) True >>> b.issubset(a) False >>> c.issuperset(a) False >>> c.issuperset(b) False >>> a.issuperset(c) True >>>
union合集
>>> a {'222', '6', '111', '8', '7'} >>> b {'777', '6', '8', '666', '7'} >>> c.clear() >>> c = a.union(b) >>> c {'222', '6', '777', '666', '111', '8', '7'} >>>