集合的特点:
1.无序
2.可变
3.元素不重复(可用于去重过滤)
集合的方法:
1.交集 &
2.并集 |
3.差集 -
4.交叉补集 ^
具体方法总结如下(set):用{}表示,但是和字典不同,不是键值对。
def add(self, *args, **kwargs): # real signature unknown
--给集合增加元素,不可以添加字典和列表,可以是元组,字符串,数字
"""
Add an element to a set.
This has no effect if the element is already present.
"""
pass
def clear(self, *args, **kwargs): # real signature unknown
--清空集合
""" Remove all elements from this set. """
pass
def copy(self, *args, **kwargs): # real signature unknown
--复制一个新的元组,浅拷贝
""" Return a shallow copy of a set. """
pass
******def difference(self, *args, **kwargs): # real signature unknown**********重要
--求差集,类似于两个集合:s1-s2
"""
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.)
"""
pass
def difference_update(self, *args, **kwargs): # real signature unknown
--求s1和s2的差集,并把结果重新赋值给s1
""" Remove all elements of another set from this set. """
pass
def discard(self, *args, **kwargs): # real signature unknown
--删除集合中的指定元素
"""
Remove an element from a set if it is a member.
If the element is not a member, do nothing.
"""
pass
*****def intersection(self, *args, **kwargs): # real signature unknown*********重要
--求交集
"""
Return the intersection of two sets as a new set.
(i.e. all elements that are in both sets.)
"""
pass
def intersection_update(self, *args, **kwargs): # real signature unknown
--求交集并复制给s1
""" Update a set with the intersection of itself and another. """
pass
*******def isdisjoint(self, *args, **kwargs): # real signature unknown************重要
--用于判断两个集合是否存在交集,如果没有返回 True,否则返回 False。
""" Return True if two sets have a null intersection. """
pass
def issubset(self, *args, **kwargs): # real signature unknown
--判断集合的所有元素是否都包含在指定集合中,如果是则返回 True,否则返回 False。
s1 = {1,2,3,4,5}
s2 = {1,2}
v = s2.issubset(s1)
print(v) 输出:True
""" Report whether another set contains this set. """
pass
def issuperset(self, *args, **kwargs): # real signature unknown
--跟issubset刚好相反
s1 = {1,2,3,4,5}
s2 = {1,2}
v = s1.issuperset(s2)
print(v) 输出:True
""" Report whether this set contains another set. """
pass
def pop(self, *args, **kwargs): # real signature unknown
--随机删除集合中的某个元素
"""
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
"""
pass
def remove(self, *args, **kwargs): # real signature unknown
--删除集合中的指定元素
"""
Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError.
"""
pass
*********def symmetric_difference(self, *args, **kwargs): # real signature unknown*********重要
--求交叉补集,即删除两个集合中的交集。
"""
Return the symmetric difference of two sets as a new set.
(i.e. all elements that are in exactly one of the sets.)
"""
pass
def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
--求交叉补集,并把新值赋给s1
""" Update a set with the symmetric difference of itself and another. """
pass
*********def union(self, *args, **kwargs): # real signature unknown**********重要
--求并集
"""
Return the union of sets as a new set.
(i.e. all elements that are in either set.)
"""
pass
def update(self, *args, **kwargs): # real signature unknown
--更新集合,如:
s1 = {11,20,13,43,50}
s2 = {11,13,29}
s1.update(s2)
print(s1)--输出:{43, 11, 13, 50, 20, 29} 把s2更新到s1中,并且去重。
""" Update a set with the union of itself and others. """
pass