1、添加一个元素:
add(...)
Addan element to a set.
1
2
3
4
|
>>> a
=
{
'shaw'
,
11
,
22
}
>>>a.add(
'sina'
)
>>> a
{
'sina'
,
11
,
22
,
'shaw'
}
|
2、清空集合
clear(...)
Remove all elements from this set.
1
2
3
4
|
>>> a
=
{
'shaw'
,
11
,
22
}
>>>a.clear()
>>> a
set
()
|
3、浅copy
copy(...)
Return a shallow copy of a set.
1
2
3
4
|
>>> a
=
{
'shaw'
,
11
,
22
}
>>> b
=
a.copy()
>>>
print
(b)
{
11
,
22
,
'shaw'
}
|
4、返回一个A中存在,B中不存在元素的集合
difference(...)
Return the difference of two or more setsas a new set.
1
2
3
4
|
>>> A
=
{
'shaw'
,
11
,
22
}
>>> B
=
{
11
,
"sam"
}
>>>A.difference(B)
{
22
,
'shaw'
}
|
5、从当前元素中删除和B中相同的元素
difference_update(...)
Remove all elements of another set fromthis set.
1
2
3
4
5
|
>>> A
=
{
'shaw'
,
11
,
22
}
>>> B
=
{
11
,
"sam"
}
>>>A.difference_update(B)
>>>
print
(A)
{
22
,
'shaw'
}
|
6、删除指定元素,如果不存在不会报错
discard(...)
Remove an element from a set if it is amember.
1
2
3
4
5
6
7
|
>>> A
=
{
'shaw'
,
11
,
22
}
>>>A.discard(
11
)
>>>
print
(A)
{
22
,
'shaw'
}
>>>A.discard(
15
)
>>>
print
(A)
{
22
,
'shaw'
}
|
7、取交集
intersection(...)
Return the intersection of two sets as anew set.
1
2
3
4
|
>>> a
=
{
'shaw'
,
11
,
22
}
>>> b
=
{
'sam'
,
22
}
>>>a.intersection(b)
{
22
}
|
8、取交集,并更新到A中
intersection_update(...)
Update a set with the intersection ofitself and another.
1
2
3
4
5
|
>>> A
=
{
'shaw'
,
11
,
22
}
>>> B
=
{
'sam'
,
22
}
>>>A.intersection_update(B)
>>>
print
(A)
{
22
}
|
9、判断是否有交集,如果没有交集,返回True,否则返回False
isdisjoint(...)
Return True if two sets have a nullintersection.
1
2
3
4
|
>>> A
=
{
'shaw'
,
11
,
22
}
>>> B
=
{
'sam'
,
22
}
>>>A.isdisjoint(B)
False
|
10、判断是否是子序列
issubset(...)
Report whether another set contains thisset.
1
2
3
4
5
6
7
|
>>> A
=
{
'shaw'
,
11
,
22
}
>>> B
=
{
'sam'
,
22
}
>>>B.issubset(A)
False
>>> C
=
{
'shaw'
}
>>>C.issubset(A)
True
|
11、判断是否是父序列
issuperset(...)
Report whether this set contains anotherset.
1
2
3
4
|
>>> A
=
{
'shaw'
,
11
,
22
}
>>> B
=
{
22
}
>>>A.issuperset(B)
True
|
12、移除元素,如果集合为空,会报错
pop(...)
Remove and return an arbitrary setelement.
Raises KeyError if the set is empty.
1
2
3
4
5
|
>>> A
=
{
'shaw'
,
11
,
1
,
98
,
3
,
'abc'
,
'Shaw'
}
>>>A.pop()
1
>>>
print
(A)
{
98
,
3
,
'abc'
,
11
,
'Shaw'
,
'shaw'
}
|
13、删除指定元素,元素不存在会报错
remove(...)
Remove an element from a set; it must be amember.
If the element is not a member, raise aKeyError.
1
2
3
4
5
6
7
8
|
>>> A
=
{
'shaw'
,
11
,
22
,
1
,
98
,
3
,
'abc'
,
'Shaw'
}
>>>A.remove(
'22'
)
Traceback (mostrecent call last):
File
"<input>"
, line
1
,
in
<module>
KeyError:
'22'
>>>A.remove(
22
)
>>>
print
(A)
{
1
,
98
,
3
,
'abc'
,
11
,
'Shaw'
,
'shaw'
}
|
14、取并集
union(...)
Return the union of sets as a new set.
1
2
3
4
|
>>> A
=
{
'shaw'
,
11
,
22
,
1
,
98
,
3
,
'abc'
,
'Shaw'
}
>>> B
=
{
11
,
'sam'
}
>>>A.union(B)
{
'sam'
,
1
,
98
,
3
,
'abc'
,
11
,
'Shaw'
,
22
,
'shaw'
}
|
15、对称差集
symmetric_difference(...)
Return the symmetric difference of twosets as a new set.
1
2
3
4
|
>>> A
=
{
'shaw'
,
11
,
22
,
1
,
98
,
3
,
'abc'
,
'Shaw'
}
>>> B
=
{
11
,
'sam'
}
>>>A.symmetric_difference(B)
{
'sam'
,
1
,
98
,
3
,
'abc'
,
'Shaw'
,
22
,
'shaw'
}
|
16、更新
update(...)
Update a set with the union of itself andothers.
1
2
3
4
5
|
>>> A
=
{
'shaw'
,
11
,
22
,
1
,
98
,
3
,
'abc'
,
'Shaw'
}
>>> B
=
{
11
,
'sam'
}
>>>A.update(B)
>>>
print
(A)
{
'sam'
,
1
,
98
,
3
,
'abc'
,
11
,
'Shaw'
,
22
,
'shaw'
}
|