0x01 set与dict
>>>type({1,2,3})
<class 'set'>
>>>type({1:2})
<class 'dict'>
0x02 set函数简介
- 如果set函数参数为空,则返回一个空的set对象
- 如果参数是一个可迭代对象时,则返回去重的一个set对象
>>>help(set)
set() -> new empty set object
set(iterable) -> new set object
Build an unordered collection of unique elements.
>>>set([1,1,2,2])
{1,2}
0x03 set对象方法图示
方法 | 参数 | 返回值 | 举例 | 数学 |
---|---|---|---|---|
set.intersection() | 一个可迭代对象 | set对象 | A.intersection(B) | A∩B |
set.union() | 一个或多个可迭代对象 | set对象 | A.intersection(B,C) | A∪B∪C |
set.difference() | 一个或多个可迭代对象 | set对象 | A.difference(B,C) | A-A∩B-A∩C |
set.discard() | 一个元素 | 无 | A.difference(a) | A-a |
set.issubset() | 一个可迭代对象 | True/False | A.issubset(B) | A∈B(A属于B) |
set.issuperset() | 一个可迭代对象 | True/False | A.issuperset(B) | A包含B |
0x04 set对象方法解析
>>>dir(set)
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
这里笔者只说set对象中独有的,像add(),clear()等方法就不在此赘述了
difference & difference_update
- set.difference(*another_interable) -> set
- 类比数学中的 A - A ∩ B,返回set中独有的元素所构成的新的set对象
set的值不改变
>>>help(set.difference) Return the difference of two or more sets as a new set. (i.e. all elements that are in this set but not the others)
>>> a {5, 6, 7, 8, 9} >>> a.difference({8,9,10}) {5, 6, 7} >>> a {5, 6, 7, 8, 9}
set.difference_update(*another_interable)
- 在上面函数的基础上更新set的值为上面函数的返回值
- 无返回值
>>> a {5, 6, 7, 8, 9} >>> a.difference_update({8,9,10}) >>> a {5, 6, 7}
difference 与 difference_update的区别:
difference是先copy该对象,然后在copy的对象上操作,不影响原对象
difference是在原对象上直接update
discard && remove
- set.discard(element)
从set对象中移除指定元素,如果该元素不在该set对象中,则什么都不做
>>>help(set.discard) Remove an element from a set if it is a member. If the element is not a member, do nothind.
>>>a = set(range(10)) >>>a {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} >>>a.discard(3) >>>a {0, 1, 2, 4, 5, 6, 7, 8, 9} >>>a.discard(10)#集合内不存在该元素时,不做任何事情 >>>a {0, 1, 2, 4, 5, 6, 7, 8, 9}
discard与remove的区别:
>>>help(set.remove) Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.
如果集合内不存在该元素,抛出KeyError异常
intersection & intersection_update
- set.intersection(another_iterable) -> set
- 参数填写可迭代对象
类似数学中的 A∩B,返回一个新的set对象,不影响原set对象的数值
>>>help(set.intersection) Return the intersection of two sets as a new set. (i.e. all elements that are in both sets.)
>>> a = set(range(10)) >>> a {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} >>> a.intersection([1,3]) {1, 3} >>>
set.intersection_update(another_iterable)
- 改变set对象的数值为 A ∩ B 的值
- 无返回值
>>> b [5, 6, 7, 8, 9, 10, 11, 12, 13, 14] >>> a {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} >>> a.intersection_update(b) >>> a {5, 6, 7, 8, 9} >>> b [5, 6, 7, 8, 9, 10, 11, 12, 13, 14] >>>
issubset & issuperset
- set.issubset(another_iterable)
- 判断set是否属于another_iterable,属于则返回True,不属于则返回False
即判断set里的元素是否都在another_iterable中
>>> a {5, 6, 7, 8, 9} >>> a.issubset({3,4}) False >>> a.issubset({4,5,6,7,8,9}) True
set.issuperset(another_iterable)
- 判断another_iterable是否属于set,属于则返回True,不属于则返回False
- 即判断another_iterable里的元素是否都在set中
>>> a {5, 6, 7, 8, 9} >>> a.issuperset({5,6}) True >>> a.issuperset({1,2,5,6}) False >>>
union & update
- set.union(*other_iterable)
- 取所有对象的并集,即A∪B∪C…
set的值不改变
>>> a {5, 6, 7, 8, 9} >>> a.union({3,2},{2,4}) {2, 3, 4, 5, 6, 7, 8, 9} >>>a >>>{5, 6, 7, 8, 9}
set.update(*other_iterable)
- 将set的值,更新为set.union(*other_iterable)所返回的
>>> a {5, 6, 7, 8, 9} >>> a.union({3,2},{2,4}) {2, 3, 4, 5, 6, 7, 8, 9} >>>a {2, 3, 4, 5, 6, 7, 8, 9}
symmetric_difference & symmetric_difference_update
- set.symmetric_difference(another_iterable)
- 返回两个对象中非都存在的元素,类似数学中的A∪B-A∩B
set的值不改变
>>> a {5, 6, 7, 8, 9} >>> a.symmetric_difference({7,8,9,10,11}) {5, 6, 10, 11} >>> a {5, 6, 7, 8, 9}
set.symmeteric_difference_update(another_iterable)
- 更新set值,更新为set.symmetric_differnece(another_iterable)所返回的
>>> a {5, 6, 7, 8, 9} >>> a.symmetric_difference({7,8,9,10,11}) {5, 6, 10, 11} >>>a {5, 6, 10, 11}