图解Python 集合

集合基本功能


集合是一个无序的,不重复的数据组合,用{}表示,它的主要作用如下:
  1. 去重,把一个列表变成集合,就会自动去重
  2. 关系测试,测试两组数据之前的交集、差集、并集、子集等关系

 集合创建:
>>> set_job = set(['DEV', 'OPS', 'DBA', 'QA', 'Sales'])
>>> set_man = set(('lucky', 'jack', 'andy', 'tom', 'andy', 'jim'))
>>> print(set_job, type(set_job))
{'DEV', 'OPS', 'Sales', 'QA', 'DBA'} <class 'set'>
>>> print(set_man, type(set_man)) # 天生去重,只有一个andy了
{'andy', 'jack', 'lucky', 'tom', 'jim'} <class 'set'>

 
元素添加:
>>> set_job = set(['DEV', 'OPS', 'DBA', 'QA', 'Sales'])
>>> set_job.add('HR') # add方法只能添加一个
>>> print(set_job)
{'QA', 'HR', 'Sales', 'DEV', 'OPS', 'DBA'}
>>> set_job.update(['FD', 'MD', 'MD'])
>>> print(set_job)
{'QA', 'HR', 'Sales', 'DEV', 'MD', 'OPS', 'FD', 'DBA'}
>>> set_job.update(('AD', 'PD')) # update方法可以添加是列表或者元组,去重,如果添加的为一个单独字符串,则把字符串拆成字母添加到集合中
>>> print(set_job)
{'QA', 'HR', 'PD', 'Sales', 'DEV', 'MD', 'OPS', 'AD', 'FD', 'DBA'}

元素删除:
>>> set_job = {'QA', 'HR', 'PD', 'Sales', 'DEV', 'MD', 'OPS', 'AD', 'FD', 'DBA'}
>>> set_job.remove('PD') # 删除指定元素
>>> set_job.remove('xx') # 元素不存在则报错 KeyError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'xx'
>>> print(set_job)
{'QA', 'HR', 'MD', 'DEV', 'Sales', 'OPS', 'AD', 'FD', 'DBA'}
>>> set_job.pop() # 随机删除一个元素
'QA'
>>> print(set_job)
{'HR', 'MD', 'DEV', 'Sales', 'OPS', 'AD', 'FD', 'DBA'}
>>> set_job.discard('OPS') # 指定删除
>>> set_job.discard('xxx') # 不存在返回None,不会报KeyError
>>> print(set_job)
{'HR', 'MD', 'DEV', 'Sales', 'AD', 'FD', 'DBA'}

其他:
>>> set_job = {'QA', 'HR', 'PD', 'Sales', 'DEV', 'MD', 'OPS', 'AD', 'FD', 'DBA'}
>>> len(set_job)  # 集合长度
10
>>> 'QA' in set_job  # 判断是否在集合中
True
>>> 'XXX' not in set_job # 不在集合中
True
>>> for i in set_job:   # 循环
...     print(i)

集合关系测试


交集:
intercaiton.png
>>> set_a = {5, 6, 7, 8, 9, 10}
>>> set_b = {1, 2, 3, 4, 5, 6}
>>> print(set_a.intersection(set_b)) # 常规方式
{5, 6}
>>> print(set_a & set_b) # 运算符(&)方式
{5, 6}

并集
bingji.png
>>> set_a = {5, 6, 7, 8, 9, 10}
>>> set_b = {1, 2, 3, 4, 5, 6}
>>> set_c = set_a.union(set_b)    # 关键字union做并集运算 先后顺序无关,谁并谁都可以
>>> print(set_c)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
>>> 
>>> set_c = set_a | set_b     # 运算符关键符 | 做并集运算  先后顺序无关,谁并谁都可以
>>> print(set_c)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

差集
chaji.png
>>> set_a = {5, 6, 7, 8, 9, 10}
>>> set_b = {1, 2, 3, 4, 5, 6}
>>> set_c = set_a - set_b # a集合跟b集合做差集 关键符 -
>>> print(set_c)
{8, 9, 10, 7}
>>> set_d = set_b - set_a # b集合跟a集合做差集 关键符 -
>>> print(set_d)
{1, 2, 3, 4}
>>> set_c = set_a.difference(set_b) # a集合跟b集合做差集 关键字difference
>>> print(set_c)
{8, 9, 10, 7}
>>> set_d = set_b.difference(set_a) # b集合跟a集合做差集 关键字difference
>>> print(set_d)
{1, 2, 3, 4}

 
子集父集
fuziji.png

拿苹果来打比方就是,把苹果掰开,然后掰开的一小部分就是子集,然后整个苹果就是父集
>>> set_a = {5, 6, 7, 8, 9, 10}
>>> set_b = {1, 2, 3, 4, 5, 6}
>>> set_c = {7, 8, 9, 10}
>>> set_d = {1, 2, 3, 4}
>>> set_e = {5, 6}
>>> set_f = {11, 12, 13, 14, 15, 16}
>>> set_c.issubset(set_a) # 测试集合c是否是集合a的子集 放回布尔值 关键字issubset
True
>>> set_d.issubset(set_b) # 测试集合d是否是集合b的子集 返回布尔值 issubset
True
>>> set_e.issubset(set_a)
True
>>> set_e.issubset(set_b)
True
>>> set_e <= set_a # 测试集合e是否是集合a的子集 关键符 <=
True
>>> set_e <= set_b
True
>>> set_f.issuperset(set_a) # 测试f集合是否是a集合的父集
False
>>> set_a.issuperset(set_e) # 测试a集合是否是集合e的父集 关键字issuperset
True
>>> set_b >= set_e # 测试集合b是否是集合e的父集 关键符 >=
True
>>> set_b >= set_d
True

对称差集
对称差集就是两个集合去掉相同的部分,然后剩下的所有元素组成的集合
duichengchaji.png
>>> set_a = {5, 6, 7, 8, 9, 10}
>>> set_b = {1, 2, 3, 4, 5, 6}
>>> set_c = set_a.symmetric_difference(set_b) # 集合a和集合b做对称差集 关键字symmetric_difference
>>> print(set_c)
{1, 2, 3, 4, 7, 8, 9, 10}
>>> set_c = set_a ^ set_b # 集合a和集合b做对称差集 关键符 ^
>>> print(set_c)
{1, 2, 3, 4, 7, 8, 9, 10}
>>> set_c = set_b ^ set_a
>>> print(set_c)
{1, 2, 3, 4, 7, 8, 9, 10}

所有方法:
class set(object):
"""
set() -> new empty set object
set(iterable) -> new set object

Build an unordered collection of unique elements.
"""
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
"""
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
""" 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
""" Update a set with the intersection of itself and another. """
pass

def isdisjoint(self, *args, **kwargs): # real signature unknown
""" Return True if two sets have a null intersection. """
pass

def issubset(self, *args, **kwargs): # real signature unknown
""" Report whether another set contains this set. """
pass

def issuperset(self, *args, **kwargs): # real signature unknown
""" 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
""" 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
""" Update a set with the union of itself and others. """
pass

2 个评论

必须支持老大,继续去打赏
嘿嘿 不用打赏,你有收获就好! 你也可以分析一些工作中遇到的问题,总结成文,然后还有学习笔记,都可以发出来,共同学习!

要回复文章请先登录注册