在学习本节之前,我们先对数据类型做一个补充,也就是数据类型的分类: 按照可变和不可变来分: 可变:列表,字典 不可变:数字,字符串,元组 按照访问顺序来分: 顺序访问:字符串,列表,元组 映射的方式访问:字典 直接访问:数字 按照存放元素的个数来分: 容器类型:列表,元组,字典 原子类型:数字,字符串
集合,又不同的元素组成,目的是将不同的值放到一起,不同的集合间用作关系运算,无需纠结集合中的单个值。无序,元素必须是不可变类型。创建集合的方法有两种,一种是set(),另一种是frozenset()。通过set()创建的集合是可变集合,不存在hash值,无序且不重复;通过frozenset()创建的集合是不可变的,我们也可以叫做冻结集合,存在hash值,可以作为字典的key值。在通过这两种方法进行创建的时候,如果不传入参数的话,就会创建空的集合,如果传入参数,那就必须是可迭代的,比如一个序列,一个列表等。
集合的创建
set()来创建
name = "jasonhy"
s = set(name)
print(s)
输出结果是:
{'s', 'a', 'h', 'n', 'j', 'y', 'o'}
names = ["jasonhy","Jasonhy"]
s = set(names)
print(s)
输出结果是:
{'Jasonhy', 'jasonhy'}
功能一:添加元素
def add(self, *args, **kwargs):
"""
Add an element to a set. #添加一个元素到集合中 This has no effect if the element is already present.
"""
pass
def update(self, *args, **kwargs):
""" Update a set with the union of itself and others. """ #可添加多个值,只要是可迭代的都可以添加进去
pass
案例:
name = "jasonhy"
s = set(name)
s.add("")
print(s)
输出结果是:
{'y', 'j', 'a', 'h', '18', 's', 'n', 'o'}
#添加多个值
name = "jasonhy"
s = set(name)
s.update("JASONHY")
print(s)
输出结果是:
{'n', 'N', 'O', 'Y', 'o', 'j', 'J', 'S', 'A', 's', 'h', 'H', 'a', 'y'}
功能二:删除元素
def clear(self, *args, **kwargs):
""" Remove all elements from this set. """ #删除所有的元素
pass
def pop(self, *args, **kwargs):
"""
Remove and return an arbitrary set element. #随机删除一个元素,并将这个元素返回
Raises KeyError if the set is empty.
"""
pass
def remove(self, *args, **kwargs):
"""
Remove an element from a set; it must be a member. #删除一个指定的元素,如果该元素不存在就会报错 If the element is not a member, raise a KeyError.
"""
pass
def discard(self, *args, **kwargs):
"""
Remove an element from a set if it is a member. #删除指定的元素,如果该元素不存在,不会报错 If the element is not a member, do nothing.
"""
pass
案例:
#全部删除
name = "jasonhy"
s = set(name)
s.clear()
print(s)
输出结果是:
set()
#随机删除,并将删除的值返回
name = "jasonhy"
s = set(name)
vstr = s.pop()
print(vstr,s)
输出结果是:
y {'n', 's', 'h', 'j', 'a', 'o'}
#如果元素存在就会报错,元素存在的情况
name = "jasonhy"
s = set(name)
s.remove("j")
print(s)
输出结果是:
{'s', 'h', 'o', 'n', 'y', 'a'}
#元素不存在的情况
name = "jasonhy"
s = set(name)
s.remove("J")
print(s)
输出结果是:
KeyError: 'J'
#元素存不存在都不会报错
name = "jasonhy"
s = set(name)
s.discard("j")
print(s)
输出结果是:
{'s', 'n', 'y', 'h', 'a', 'o'}
功能三:运算
#求两个集合的交集,符号是&
def intersection(self, *args, **kwargs):
"""
Return the intersection of two sets as a new set. #返回两个集合的交集部分 (i.e. all elements that are in both sets.)
"""
pass
#求两个集合的并集,符号|
def union(self, *args, **kwargs):
"""
Return the union of sets as a new set. #返回两个集合的并集 (i.e. all elements that are in either set.)
"""
pass
#求两个集合的差集,符号-
def difference(self, *args, **kwargs):
"""
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.) #比如集合:SA - SB那么返回的集合就只能是SA里面的
"""
pass
#求交叉补集,符号^
def symmetric_difference(self, *args, **kwargs):
"""
Return the symmetric difference of two sets as a new set. #返回集合不同的部分 (i.e. all elements that are in exactly one of the sets.) #比如集合SA^SB,返回的集合只能属于这两个集合中的一个,不能同时属于两个
"""
pass
#求差集之后更新原来集合
def difference_update(self, *args, **kwargs):
""" Remove all elements of another set from this set. """
pass
案例:
#交集
name = "jasonhy"
s = set(name)
sother = set("jasonhyJASONHY")
print(s & sother)
输出结果是:
{'s', 'o', 'h', 'a', 'n', 'j', 'y'}
#并集
name = "jasonhy"
s = set(name)
sother = set("jasonhyJASONHY")
print(s | sother)
输出结果是:
{'o', 'Y', 'O', 'A', 'N', 'a', 'J', 'h', 'n', 'H', 'y', 's', 'S', 'j'}
#差集
name = "jasonhy"
s = set(name)
sother = set("jasonhyJASONHY")
print(sother - s)
输出结果是:
{'S', 'Y', 'O', 'A', 'J', 'H', 'N'}
#交叉补集
name = "jasonhy"
s = set(name)
sother = set("jasonhyJASONHY")
print(s ^ sother)
输出结果是:
{'H', 'S', 'J', 'O', 'Y', 'A', 'N'}
#差值之后更新sother
name = "jasonhy"
s = set(name)
sother = set("jasonhyJASONHY")
sother.difference_update(s)
print(sother)
输出结果是:
{'A', 'J', 'S', 'N', 'Y', 'O', 'H'}
功能四:判断集合之间的关系
def issubset(self, *args, **kwargs):
""" Report whether another set contains this set. """ #另外一个集合是否包含这个集合
pass
def issuperset(self, *args, **kwargs):
""" Report whether this set contains another set. """ #这个集合是否包含另外一个集合
pass
def isdisjoint(self, *args, **kwargs):
""" Return True if two sets have a null intersection. """ #两个集合是否有交集,如果没有交集,返回True
pass
案例:
#判断s是否是sother的ziji
name = "jasonhy"
s = set(name)
sother = set("jasonhyJASONHY")
print(s.issubset(sother))
输出结果是:
True
#判断sother是否是s的父集
name = "jasonhy"
s = set(name)
sother = set("jasonhyJASONHY")
print(sother.issuperset(s))
输出结果是;
True
#判断两个集合是否有交集,有的话返回False
name = "jasonhy"
s = set(name)
sother = set("jasonhyJASONHY")
print(sother.isdisjoint(s))
输出结果是:
False
frozenset()来创建
name = "jasonhy"
sthis = frozenset(name)
print(sthis)
输出结果是:
frozenset({'a', 'n', 'j', 'y', 'o', 'h', 's'})
和set()创建不同的是,它不具备删除和添加元素的功能,其他功能和set()的功能一样的