| clear(...) 清空一个集合
| Remove all elements from this set.
>>>set1.clear()>>>set1
set()
| copy(...) 影子复制,指向同一个内存地址
| Return a shallow copy of a set. |
>>> list1
[3, 2, 1, 1, 2, 3, 4, 5]
>>> list2
[3, 4, 5, 6, 7, 8]
>>> set1=set(list1)>>>id(set1)140138768485512>>> set3=set1.copy()>>>id(set3)140138695576712
| difference(...) 差集,格式set1.difference(set2),求in list1 not in list2的集合
| 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.)
>>>set1
{1, 2, 3, 4, 5}>>>set2
{3, 4, 5, 6, 7, 8}>>>set1.difference(set2)
{1, 2}
| difference_update(...) 删除在本集合同时也在其他集合的元素,差集
| Remove all elements of another set from this set. |
>>> set1=set(list1)
>>> set2=set(list2)
>>> set1,set2
({1, 2, 3, 4, 5}, {3, 4, 5, 6, 7, 8})
>>> set1=set(list1)>>> set2=set(list2)>>>set2.difference_update(set1)>>>set2
{6, 7, 8}
| discard(...) 删除一个在本集合中的元素
| Remove an element from a set if it is a member.
|
| If the element is not a member, do nothing. |
>>> set3=set([1,2,3,'a','b','c'])>>> set3.discard(1)>>>set3
{2, 3, 'c', 'b', 'a'}>>> set3.discard('a')>>>set3
{2, 3, 'c', 'b'}>>> set3.discard('dd')>>>set3
{2, 3, 'c', 'b'}
| intersection(...)并集,同时在两个集合中的元素
| Return the intersection of two sets as a new set.
|
| (i.e. all elements that are in both sets.) |
>>>set1
{1, 2, 3, 4, 5}>>>set2
{3, 4, 5, 6, 7, 8}>>>set1.intersection(set2)
{3, 4, 5}>>>set2.intersection(set1)
{3, 4, 5}
| intersection_update(...) 交集
| Update a set with the intersection of itself and another. |
>>>set1,set2
({1, 2, 3, 4, 5}, {3, 4, 5, 6, 7, 8})>>>set1.intersection_update(set2)>>>set1
{3, 4, 5}
| isdisjoint(...) 返回布尔值,判断两个集合是否没有并集
| Return True if two sets have a null intersection.
|
>>>set1,set2,set3,set4
({3, 4, 5}, {3, 4, 5, 6, 7, 8}, {2, 3, 'c', 'b'}, {'y', 'x', 'z'})>>>set1.isdisjoint(set2)
False>>>set1.isdisjoint(set4)
True
| issubset(...) 返回布尔值,判断前一个集合是否是后一个集合的子集
| Report whether another set contains this set. |
>>>set1
{3, 4, 5}>>>set5
{3, 4}>>>set5.issubset(set1)
True
| issuperset(...) 返回布尔值,判断前一个集合是否是后一个集合的父集
| Report whether this set contains another set. |
>>>set1
{3, 4, 5}>>>set5
{3, 4}>>>set1.issuperset(set5)
True
| pop(...) 随机删除一个集合元素,返回被删除元素,空集合删除则报错
| Remove and return an arbitrary set element.
| Raises KeyError if the set is empty. |
>>>set1
{3, 4, 5}>>>set1.pop()3
>>>set1
{4, 5}>>>set1.pop()4
>>>set1.pop()5
>>>set1.pop()
Traceback (most recent call last):
File"", line 1, in KeyError:'pop from an empty set'
| remove(...) 删除指定在集合中的元素
| Remove an element from a set; it must be a member. |
>>> set1=set(list1)>>>set1
{1, 2, 3, 4, 5}>>> set1.remove(1)>>>set1
{2, 3, 4, 5}>>> set1.remove('a')
Traceback (most recent call last):
File"", line 1, in KeyError:'a'
| symmetric_difference(...) 对称差集,集合A与集合B不相交的部分,交集的反集
| Return the symmetric difference of two sets as a new set.
| (i.e. all elements that are in exactly one of the sets.)
>>>set1
{1, 2, 3, 4, 5}>>> set6={4,5,6,7,8}>>>set1.symmetric_difference(set6)
{1, 2, 3, 6, 7, 8}>>>set6.symmetric_difference(set1)
{1, 2, 3, 6, 7, 8}
>>> set1,set7
({1, 2, 3, 4, 5}, {'a', 'c', 'b'})
>>> set1.symmetric_difference(set7)
{'c', 2, 3, 1, 4, 5, 'b', 'a'}