首页 > 代码库 > day03-集合
day03-集合
一、概述
集合是无序的,天生不重复的数据组合,它的作用如下:
- 去重,即:把一个列表变成集合,就去重了
- 关系测试,即:测试两组集合的交集、并集和差集等
二、语法
1 >>> name_1 = [1,2,3,4,7,8,7,10] 2 3 #把列表转换为集合 4 >>> name_1 = set(name_1) 5 #转换后,去重 6 >>> print(name_1,type(name_1)) 7 {1, 2, 3, 4, 7, 8, 10} <class ‘set‘>
三、关系测试
1、交集(intersection())
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #输出结果 >>> name_1.intersection(name_2) {8, 1, 10, 3}
交集(&)
1 >>> name_1 = [1,2,3,4,7,8,7,10] 2 >>> name_2 = [1,3,5,8,10] 3 >>> name_1 = set(name_1) 4 >>> name_2 = set(name_2) 5 #结果输出 6 >>> name_1 & name_2 7 {8, 1, 10, 3}
2、并集(union())
1 >>> name_1 = [1,2,3,4,7,8,7,10] 2 >>> name_2 = [1,3,5,8,10] 3 >>> name_1 = set(name_1) 4 >>> name_2 = set(name_2) 5 #输出结果 6 >>> name_1.union(name_2) 7 {1, 2, 3, 4, 5, 7, 8, 10}
并集(|)
1 >>> name_1 = [1,2,3,4,7,8,7,10] 2 >>> name_2 = [1,3,5,8,10] 3 >>> name_1 = set(name_1) 4 >>> name_2 = set(name_2) 5 #结果输出 6 >>> name_1 | name_2 7 {1, 2, 3, 4, 5, 7, 8, 10}
3、差集(difference())
差集取的是数值在第一个集合中,但是不在第二个集合中(在我不在你)
1 >>> name_1 = [1,2,3,4,7,8,7,10] 2 >>> name_2 = [1,3,5,8,10] 3 >>> name_1 = set(name_1) 4 >>> name_2 = set(name_2) 5 #输出结果 6 >>> name_1.difference(name_2) 7 {2, 4, 7}
差集(-)
1 >>> name_1 = [1,2,3,4,7,8,7,10] 2 >>> name_2 = [1,3,5,8,10] 3 >>> name_1 = set(name_1) 4 >>> name_2 = set(name_2) 5 #结果输出 6 >>> name_1 - name_2 7 {2, 4, 7}
4、issubset()
判断一个集合是否是另一个集合的子集
1 >>> name_1 = [1,2,3,4,7,8,7,10] 2 >>> name_3 = [1,2,3,4] 3 >>> name_1 = set(name_1) 4 >>> name_3 = set(name_3) 5 #输出结果 6 >>> name_3.issubset(name_1) 7 True
是否是子集(<=)
1 >>> name_1 = [1,2,3,4,7,8,10] 2 >>> name_3 = [1,2,3,4] 3 >>> name_1 = set(name_1) 4 >>> name_3 = set(name_3) 5 #输出 6 >>> name_3 <= name_1 7 True
5、issuperset()
判断一个集合是否是另一个集合的父集
1 >>> name_1 = [1,2,3,4,7,8,7,10] 2 >>> name_3 = [1,2,3,4] 3 >>> name_1 = set(name_1) 4 >>> name_3 = set(name_3) 5 #输出结果 6 >>> name_1.issuperset(name_3) 7 True
是否是父集(>=)
1 >>> name_1 = [1,2,3,4,7,8,10] 2 >>> name_3 = [1,2,3,4] 3 >>> name_1 = set(name_1) 4 >>> name_3 = set(name_3) 5 #输出 6 >>> name_1 >= name_3 7 True
6、对称差集(symmetric_difference())
把两个集合没有交集的数值取出来
1 >>> name_1 = [1,2,3,4,7,8,7,10] 2 >>> name_2 = [1,3,5,8,10] 3 >>> name_1 = set(name_1) 4 >>> name_2 = set(name_2) 5 #输出结果 6 >>> name_1.symmetric_difference(name_2) 7 {2, 4, 5, 7}
对称差集(^)
1 >>> name_1 = [1,2,3,4,7,8,7,10] 2 >>> name_2 = [1,3,5,8,10] 3 >>> name_1 = set(name_1) 4 >>> name_2 = set(name_2) 5 #输出 6 >>> name_1 ^ name_2 7 {2, 4, 5, 7}
7、isdisjoint()
判断两个集合是否有交集,没有交集,则返回True
1 >>> name_1 = [1,2,3,4,7,8,7,10] 2 >>> name_2 = [1,3,5,8,10] 3 >>> name_3 = [11] 4 >>> name_1 = set(name_1) 5 >>> name_2 = set(name_2) 6 >>> name_3 = set(name_3) 7 #有交集 8 >>> name_1.isdisjoint(name_2) 9 False 10 #无交集 11 >>> name_1.isdisjoint(name_3) 12 True
四、基本操作
1、添加(add())
1 >>> name_2 = [1,3,5,8,10] 2 >>> name_2 = set(name_2) 3 #添加已存在,不报错 4 >>> name_2.add(1) 5 >>> name_2 6 {8, 1, 10, 3, 5} 7 #添加不存在,添加一个新的数值 8 >>> name_2.add(11) 9 >>> name_2 10 {1, 3, 5, 8, 10, 11}
2、添加多项(update())
1 >>> name_2 = [1,3,5,8,10] 2 >>> name_2 = set(name_2) 3 >>> name_2.update([12,13,14]) 4 #输出结果 5 >>> name_2 6 {1, 3, 5, 8, 10, 12, 13, 14}
3、删除(remove(),pop(),discard())
①remove()
1 >>> name_2 = [1,3,5,8,10] 2 >>> name_2 = set(name_2) 3 >>> name_2 4 {8, 1, 10, 3, 5} 5 >>> name_2.remove(1) 6 #输出 7 >>> name_2 8 {8, 10, 3, 5} 9 #删除不存在的元素,会报错 10 >>> name_2.remove(1) 11 Traceback (most recent call last): 12 File "<input>", line 1, in <module> 13 KeyError: 1
注:用remove删除时,当元素不存在,会报错
②pop()
1 >>> name_2 = [1,3,5,8,10] 2 >>> name_2 = set(name_2) 3 >>> name_2 4 {8, 1, 10, 3, 5} 5 #输出 6 >>> name_2.pop() 7 8
注:pop是随机删除集合中的某个元素,并且打印
③discard()
1 >>> name_2 = [1,3,5,8,10] 2 >>> name_2 = set(name_2) 3 >>> name_2.discard(10) 4 #输出结果 5 >>> name_2 6 {8, 1, 3, 5} 7 #删除不存在元素,不报错 8 >>> name_2.discard(10)
注:用discard删除不存在的元素,不会出现报错
4、长度(len())
1 >>> name_1 = [1,2,3,4,7,8,7,10] 2 >>> name_1 = set(name_1) 3 #结果输出 4 >>> len(name_1) 5 7
5、x in s
测试 x 是否是 s 的成员
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_1 = set(name_1) #结果输出 >>> 1 in name_1 True
6、x not in s
测试 x 是否不是 s 的成员
1 >>> name_1 = [1,2,3,4,7,8,7,10] 2 >>> name_1 = set(name_1) 3 #输出 4 >>> 12 not in name_1 5 True
day03-集合
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。