下载浙江平安建设信息系统网站,律师做网络推广最好的网站有哪些,建平台需要投资多少钱,友情链接检查一、集合的定义
集合#xff1a;是一个无序的没有重复元素的序列#xff0c;因此不能通过索引来进行操作
1#xff1a;使用set()创建集合
set(object) # 参数为一个序列#xff0c;整型不能作为参数
set_a set(abcb)
print(set_a) # {b, a, c}
2…一、集合的定义
集合是一个无序的没有重复元素的序列因此不能通过索引来进行操作
1使用set()创建集合
set(object) # 参数为一个序列整型不能作为参数
set_a set(abcb)
print(set_a) # {b, a, c}
2使用{}直接定义
set_a {a, b, c}
print(type(set_a), set_a) # class set {b, c, a}
注意创建一个空集合必须使用set()而不是{}因为{}是用来创建一个空字典的
set_a set()
print(type(set_a), set_a) # class set set()b {}
print(type(b), b) # class dict {}
二、集合的使用
1添加元素add()重复添加无效
set.add(x) # x为要添加的元素
返回值为None修改了原集合
set_a set(abc)
print(set_a) # {c, a, b}
# 元素不存在
set_a.add(d)
print(set_a) # {c, a, b, d}
# 元素已存在
set_a.add(c)
print(set_a) # {c, a, b, d}
2添加多个元素update()把传入的元素拆分作为个体加入到集合中
set.update()
返回值为None修改了原集合
set_a set(abc)
print(set_a) # {c, b, a}
result set_a.update(defdab)
print(result) # None
print(set_a) # {d, f, e, b, c, a}
set_a set(abc)
print(set_a) # {a, b, c}
result set_a.update([a,g,r,1,2])
print(result) # None
print(set_a) # {1, 2, a, r, g, c, b}
3删除元素remove()
set.remove(x) # x为要删除的元素
返回值为None删除的元素不存在会报错修改了元集合
set_a set(abc)
print(set_a) # {b, c, a}
result set_a.remove(a)
print(result) # None
print(set_a) # {b, c}
result set_a.remove(d) # 报错KeyError: d
4删除元素discard()
set.discard(x) # x为要删除的元素
返回值为None删除的元素不存在不会报错修改了元集合
set_a set(abc)
print(set_a) # {c, b, a}
# 删除的元素存在
result set_a.discard(a)
print(result) # None
print(set_a) # {c, b}
# 删除的元素不存在
result set_a.discard(d)
print(result) # None
print(set_a) # {c, b}
5删除元素pop()随机删除一个元素
set.pop()
返回值为被删除的元素修改了元集合
set_a set(abc)
print(set_a) # {c, a, b}
result set_a.pop()
print(result) # c
print(set_a) # {a, b}
6统计集合的元素个数len()
len(set)
返回值为集合的元素个数
set_a {a, b, c}
result len(set_a)
print(result) # 3
7清空集合clear()
set.clear()
返回值为None修改了原集合
set_a {a, b, c}
result set_a.clear()
print(result) # None
print(set_a) # set()
8判断元素是否在集合中使用成员运算符in / not in
set_a {a, b, c}
print(a in set_a) # True
print(a not in set_a) # False
9isdisjoint()判断两个集合是否包含相同的元素
set.isdisjoint(set1) # set1必填
返回值为返回布尔值包含相同的元素返回False不包含相同的元素返回True
set_a {a, b, c}
set_b {a, d, f}
# set_a与set_b中是否有相同的元素
result set_a.isdisjoint(set_b)
print(result) # False
10issubset() 判断集合中的所有元素是否都包含在指定的集合中
set.issubset(set1) # set中的所有元素是否都包含在set1中
# set1必填
返回值为布尔值如果都包含返回True否则返回False
set_a {a, b, c}
set_b {a, d, f, b, g, c}
# set_a中的所有元素是否都包含在set_b中
result set_a.issubset(set_b)
print(result) # True
11issuperset() 判断指定集合的所有元素是否都包含在原始的集合中
set.issuperset(set1) # set1中的所有元素是否都包含在set中
# set1必填
返回值为布尔值如果都包含返回True否则返回False
set_a {a, b, c}
set_b {a, d, f, b, g, c}
# set_b中的所有元素是否都包含在set_a中
result set_a.issuperset(set_b)
print(result) # False
三、集合的操作符
1求差集集合元素包含在第一个集合中但不包含在第二个集合
1.1使用“-”操作符
set_a {a, b, c}
set_b {a, d, f}
# set_a中有的元素在set_b中没有
result set_a - set_b
print(result) # {c, b}
1.2使用difference()方法
set1.difference(set2) # set1中有的元素set2中没有
返回值为一个新的集合不修改原集合
set_a {a, b, c}
set_b {a, d, f}
# set_a中有的元素在set_b中没有
result set_a.difference(set_b)
print(result) # {c, b}
1.3使用difference_update()方法
set1.difference_update()(set2) # set1中有的元素set2中没有
返回值为一个新的集合修改了原集合set1
difference_update() 方法与 difference() 方法的区别在于 difference() 方法返回一个移除相同元素的新集合而 difference_update() 方法是直接在原来的集合中移除元素没有返回值。
set_a {a, b, c}
set_b {a, d, f}
# set_a中有的元素在set_b中没有
result set_a.difference_update(set_b)
print(result) # None
print(set_a) # {c, b}
2求交集集合元素包含在第一个集合中同时包含在第二个集合
2.1使用“”操作符
set_a {a, b, c}
set_b {a, d, f}
# # set_a中有的元素在set_b中也有
result set_a set_b
print(result) # {a}
2.2使用intersection() 方法
set.intersection(set1, set2...)
# set中有的元素在set1set2中也有
# set1必填
# set2选填
返回值为一个新的集合不修改原集合
set_a {a, b, c}
set_b {a, d, f}
# set_a中有的元素在set_b中也有
result set_a.intersection(set_b)
print(result) # {a}
2.3使用intersection_update() 方法
set.intersection_update(set1, set2 ... )
# set中有的元素在set1set2中也有
# set1必填
# set2选填
返回值为None修改了原集合set
intersection_update() 方法不同于 intersection() 方法因为 intersection() 方法是返回一个新的集合而 intersection_update() 方法是在原始的集合上移除不重叠的元素。
set_a {a, b, c}
set_b {a, d, f}
# set_a中有的元素在set_b中也有
result set_a.intersection_update(set_b)
print(result) # None
print(set_a) # {a}
3求并集合集所有集合中的所有元素
3.1使用“|”操作符
set_a {a, b, c}
set_b {a, d, f}
# set_a、set_b中所有的元素
result set_a | set_b
print(result) # {c, d, b, f, a}
3.2使用union() 方法
set.union(set1,set2...)
# setset1set2等集合中的所有元素
# set1必填
# set2选填
返回值为所有元素组成的一个新的集合不修改原集合set
set_a {a, b, c}
set_b {a, d, f}
# set_a、set_b中所有的元素
result set_a.union(set_b)
print(result) # {b, d, f, a, c} 文章转载自: http://www.morning.kmjbs.cn.gov.cn.kmjbs.cn http://www.morning.tgczj.cn.gov.cn.tgczj.cn http://www.morning.gypcr.cn.gov.cn.gypcr.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.fdmtr.cn.gov.cn.fdmtr.cn http://www.morning.yrbhf.cn.gov.cn.yrbhf.cn http://www.morning.gklxm.cn.gov.cn.gklxm.cn http://www.morning.hgwsj.cn.gov.cn.hgwsj.cn http://www.morning.fmkbk.cn.gov.cn.fmkbk.cn http://www.morning.nhpmn.cn.gov.cn.nhpmn.cn http://www.morning.wjqbr.cn.gov.cn.wjqbr.cn http://www.morning.hpkr.cn.gov.cn.hpkr.cn http://www.morning.yfpnl.cn.gov.cn.yfpnl.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.ghyfm.cn.gov.cn.ghyfm.cn http://www.morning.lsxabc.com.gov.cn.lsxabc.com http://www.morning.fbxdp.cn.gov.cn.fbxdp.cn http://www.morning.kmcby.cn.gov.cn.kmcby.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.tbhlc.cn.gov.cn.tbhlc.cn http://www.morning.ntzfl.cn.gov.cn.ntzfl.cn http://www.morning.rymb.cn.gov.cn.rymb.cn http://www.morning.hrzky.cn.gov.cn.hrzky.cn http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn http://www.morning.plxhq.cn.gov.cn.plxhq.cn http://www.morning.wpspf.cn.gov.cn.wpspf.cn http://www.morning.bytgy.com.gov.cn.bytgy.com http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.ghjln.cn.gov.cn.ghjln.cn http://www.morning.mmclj.cn.gov.cn.mmclj.cn http://www.morning.3jiax.cn.gov.cn.3jiax.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.ltkzb.cn.gov.cn.ltkzb.cn http://www.morning.fjfjm.cn.gov.cn.fjfjm.cn http://www.morning.qjlnh.cn.gov.cn.qjlnh.cn http://www.morning.zlhbg.cn.gov.cn.zlhbg.cn http://www.morning.mmjyk.cn.gov.cn.mmjyk.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.mmzhuti.com.gov.cn.mmzhuti.com http://www.morning.shangwenchao4.cn.gov.cn.shangwenchao4.cn http://www.morning.hxlch.cn.gov.cn.hxlch.cn http://www.morning.rcwbc.cn.gov.cn.rcwbc.cn http://www.morning.dspqc.cn.gov.cn.dspqc.cn http://www.morning.phgz.cn.gov.cn.phgz.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.jhkzl.cn.gov.cn.jhkzl.cn http://www.morning.hgsmz.cn.gov.cn.hgsmz.cn http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.fwkjp.cn.gov.cn.fwkjp.cn http://www.morning.qhrsy.cn.gov.cn.qhrsy.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.pmdnx.cn.gov.cn.pmdnx.cn http://www.morning.fnxzk.cn.gov.cn.fnxzk.cn http://www.morning.sxlrg.cn.gov.cn.sxlrg.cn http://www.morning.mehrim.com.gov.cn.mehrim.com http://www.morning.yuanshenglan.com.gov.cn.yuanshenglan.com http://www.morning.ktmpw.cn.gov.cn.ktmpw.cn http://www.morning.jjnry.cn.gov.cn.jjnry.cn http://www.morning.jqhrk.cn.gov.cn.jqhrk.cn http://www.morning.dmwbs.cn.gov.cn.dmwbs.cn http://www.morning.qwbtr.cn.gov.cn.qwbtr.cn http://www.morning.tkxyx.cn.gov.cn.tkxyx.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.c7625.cn.gov.cn.c7625.cn http://www.morning.qtltg.cn.gov.cn.qtltg.cn http://www.morning.rgxll.cn.gov.cn.rgxll.cn http://www.morning.rcgzg.cn.gov.cn.rcgzg.cn http://www.morning.ybqlb.cn.gov.cn.ybqlb.cn http://www.morning.dbddm.cn.gov.cn.dbddm.cn http://www.morning.mhmsn.cn.gov.cn.mhmsn.cn http://www.morning.tqdqc.cn.gov.cn.tqdqc.cn http://www.morning.gyfwy.cn.gov.cn.gyfwy.cn http://www.morning.fyglg.cn.gov.cn.fyglg.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn http://www.morning.mfxcg.cn.gov.cn.mfxcg.cn http://www.morning.jhfkr.cn.gov.cn.jhfkr.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.mczjq.cn.gov.cn.mczjq.cn http://www.morning.shprz.cn.gov.cn.shprz.cn