#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__:anxu.qi
# Date:2018/11/30

一篇介绍python集合的文章,写的很好

https://tw.saowen.com/a/ab713431b675abe3d71cf675d04082b9601135ba484291bb2b3a95b6ccad0e4f
################################ 集合 ########################################

python-集合(set)知识整理 Python 第1张


# 集合是一个可变的、无序的,不重复的数据组合,
它的主要作用如下:
  1、去重,把一个列表变成集合,就自动去重了
  2、关系测试,测试两组数据之前的交集、差集、并集等关系
(注:set元素要求必须可hash才能加入到set元素中)
因为没有顺序,所以无法被索引,但是可以被迭代。
list01 = [1,4,5,6,7,8,4,5,6]
print(list01,type(list01))
# [1, 4, 5, 6, 7, 8, 4, 5, 6] <class 'list'>
list01 = set(list01)
print(list01,type(list01))
# {1, 4, 5, 6, 7, 8} <class 'set'>
# ################### 交集 (intersection)#########################

python-集合(set)知识整理 Python 第2张

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

python-集合(set)知识整理 Python 第3张

 

# 交集 表示符号:& 表示 集合1和集合2 中都有的。
list02 = set([2,6,0,22,8,4])
print(list01,list02)
# {1, 4, 5, 6, 7, 8} {0, 2, 4, 6, 8, 22}
# # 交集 intersection
print(list01.intersection(list02))

# ################### 并集 (union)#########################


# 并集 表示符号:| 表示 集合1或者是集合2中有的
# # 并集 union 并起来去重
print(list01.union(list02))
# {0, 1, 2, 4, 5, 6, 7, 8, 22}

# ################### 差集 (difference)#########################

python-集合(set)知识整理 Python 第4张

python-集合(set)知识整理 Python 第5张

 python-集合(set)知识整理 Python 第6张

 


# 差集表示符号:- 表示 set01 - set02 表示 set01中有的,set02中没有的
# set02 - set01 表示 set02中有的,set01中没有的
#  # 差集  difference list01中有的,list02中没有的
print(list01.difference(list02))
# {1, 5, 7}
print(list02.difference(list01))
# {0, 2, 22}

# ################### 子集 (issubset) #########################
print(list01.issubset(list02))
# False

# ################### 父集 (issuperset) #########################
print(list01.issuperset(list02))
# False

# ################### 反向差集|对称差集 (symmetric_difference) #########################

python-集合(set)知识整理 Python 第7张

 

python-集合(set)知识整理 Python 第8张

 



# list01和list02里面,互相都没有的,取出来放到一块
print(list01.symmetric_difference(list02))

########################## 集合的功能介绍及举例说明 ###################################
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__:anxu.qi
# Date:2018/12/4
#######################################################################################
a = {11, 22, 33, 44, 66, 99, "oopp",1123, "111"}
b = {11,22,88,99,"oopp","opop"}
# ##################### add 向集合中添加元素 注:一次只允许添加一个元素 ####

python-集合(set)知识整理 Python 第9张

 


# a.add("vivo")
# print(a) # {33, 66, 'opop', 'vivo', 11, 44, 77, 22, 55}
# #################### clear 删除集合的所有的元素 ####

python-集合(set)知识整理 Python 第10张

 


# #################### copy 浅copy ####
# #################### difference A中存在的B中不存在的 ####
# A中存在,B中不存在 返回一个新值,变量接受
c = a.difference(b)
print(c) # {33, 66, 1123, 44, '111'}
# #################### difference_update A中存在的B中不存在的,结果更新为A ####
a.difference_update(b)
print(a) # {33, 66, 1123, '111', 44}

a = {11, 22, 33, 44, 66, 99, "oopp",1123, "111"}
b = {11,22,88,99,"oopp","opop"}
# #################### discard 移除指定的元素,不存在不报错 ####

python-集合(set)知识整理 Python 第11张

 


# #################### remove 移除指定的元素,不存在则提示ERROR。 ####

python-集合(set)知识整理 Python 第12张

 做的是key的比较


a.discard(11111)
# #################### intersection 取出交集并赋值给cc #####
cc = a.intersection(b)
print(cc) # {11, 'oopp', 99, 22}
# #################### intersection_update 取出交集并赋值给A ####
a.intersection_update(b)
print(a) # {'oopp', 11, 99, 22}
# #################### isdisjoint 如果没有交集返回True,有交集为False ####
print(a.isdisjoint(b)) # False
se = {11,22,33,44}
be = {11,22}
# #################### issubset # 另一个集合是否包含此集合, 子序列 ####
print(be.issubset(se)) # True
# #################### issuperset # 这个集合是否包含另一个集合 父序列 ####
print(se.issuperset(be)) # True
# #################### pop # 移除并返回任意集合元素,如果集合为空,则引发KeyError。 ####

python-集合(set)知识整理 Python 第13张

 


aaa = se.pop() # 可以将移除的那个赋值给其他元素 ####
print(aaa) # 33
# #################### pop # 移除并返回任意集合元素,如果集合为空,则引发KeyError。 ####
se = {11,22,33,44,99}
be = {11,22,77,88}
# #################### symmetric_difference # 将se中存在的be不存在的,be不存在的se中存在的合并到了一起,赋值为dddd ####
dddd = se.symmetric_difference(be)
print(dddd) # {33, 99, 44, 77, 88}
cccc = se.symmetric_difference(be)
print(cccc) # {33, 99, 44, 77, 88}
# #################### symmetric_difference_update # 将se中存在的be不存在的,be不存在的se中存在的合并到了一起,赋值为se ####
se.symmetric_difference_update(be)
print(se) # {33, 99, 44, 77, 88}
# ##############################
se = {11,22,33,44,99}
be = {11,22,77,88}
# #################### union # 取并集 ####

python-集合(set)知识整理 Python 第14张

 python-集合(set)知识整理 Python 第15张

 

 


tt = se.union(be)
print(tt) # {33, 99, 11, 44, 77, 22, 88}
# #################### update # 更新的时候,必须得是可迭代(Iterable)的 ####
|= 就等于update
python-集合(set)知识整理 Python 第16张

 

 

python-集合(set)知识整理 Python 第17张

 python-集合(set)知识整理 Python 第18张

 python-集合(set)知识整理 Python 第19张

 


se.update([666,888])
se.update(666,888)
print(se)
# ########################################################################################################

python-集合(set)知识整理 Python 第20张

 

python-集合(set)知识整理 Python 第21张

list 随着规模的增加,效率在往下走,

set 随着规模的增加,效率还是那样



python-集合(set)知识整理 Python 第22张

 

python-集合(set)知识整理 Python 第23张

 

 




扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄