集合(set)和字典(dict)类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。
定义一个集合需要一个列表(list)作为输入集合:
list_1 = set([15,44,789,45,65,87]) print(list_1,type(list_1))#{65, 44, 45, 15, 789, 87} <class 'set'>
自带重复元素过滤
list_1 = set([15,44,789,45,789,65,87,87]) print(list_1,type(list_1))#{65, 44, 45, 15, 789, 87} <class 'set'>
基本操作
添加 add(key)
list_3 =set([1101,1102,1103]) list_3.add(899) print(list_3)#{899, 1101, 1102, 1103}
添加多项 update(list)
list_3 =set([1101,1102,1103]) list_3.update([899,810,827]) print(list_3)#{899, 810, 1101, 1102, 1103, 827}
删除 remove(key)
list_3 =set([1101,1102,1103]) list_3.remove(1101) print(list_3)#{1102, 1103}
如果要删除一个不存在的元素会报错
list_3 =set([1101,1102,1103]) list_3.remove(1201) #################### Traceback (most recent call last): File "E:/pywww/day02/04.py", line 26, in <module> list_3.remove(1201) KeyError: 1201
可以使用另外一种方法 discard
list_3 =set([1101,1102,1103])
print(list_3.discard(1101))#None
print(list_3.discard(1201))#None
print(list_3)#{1102, 1103}
discard 没有返回值
统计长度len(s)
list_3 =set([1101,1102,1103]) print(len(list_3))#3
成员判断 in not in
list_3 =set([1101,1102,1103]) print(1101 in list_3)#True print(1109 in list_3)#False print(1101 not in list_3)#False print(1109 not in list_3)#True
关系操作
集合交集 intersection
求两个集合中相同的key
list_1 = set([15,44,789,45,789,65,87,87]) list_2 = set([5,789,64,38,62,56,45]) print(list_1.intersection(list_2))#{789, 45}
集合并集 union
求两个集合去重后的key的集合
list_1 = set([15,44,789,45,789,65,87,87]) list_2 = set([5,789,64,38,62,56,45]) print(list_1.union(list_2))#{64, 65, 5, 38, 44, 45, 15, 789, 87, 56, 62}
集合差集 difference
查找 list_1中 在 list_2 里面没有的key
list_1 = set([15,44,789,45,789,65,87,87]) list_2 = set([5,789,64,38,62,56,45]) print(list_1.difference(list_2))#{65, 44, 87, 15}
集合子集
list_1 = set([15,44,789,45,789,65,87,87]) list_2 = set([5,789,64,38,62,56,45]) list3 = set([15,789,65]) print(list3.issubset(list_1))#True print(list_2.issubset(list_1))#False
集合父集
list_1 = set([15,44,789,45,789,65,87,87]) list_2 = set([5,789,64,38,62,56,45]) list3 = set([15,789,65]) print(list_1.issuperset(list3))#True print(list_1.issuperset(list_2))#False
对称差集
两个集合中互相都没有的key的集合
list_1 = set([15,44,789,45,789,65,87,87]) list_2 = set([5,789,64,38,62,56,45]) print(list_2.symmetric_difference(list_1))#{64, 65, 5, 15, 87, 38, 44, 56, 62}
判断两个集合是否有交集
list_1 = set([15,44,789,45,789,65,87,87,1101]) list_2 = set([5,789,64,38,62,56,45]) list_3 =set([1101,1102,1103]) print(list_2.isdisjoint(list_3))#True print(list_1.isdisjoint(list_3))#False
运算符
list_1 = set([15,44,789,45,789,65,87,87,1101]) list_2 = set([5,789,64,38,62,56,45]) list_3 =set([1101,1102,1103])
交集 &
print(list_1 & list_2)#{789, 45}
并集 |
print(list_1| list_2)#{64, 65, 5, 38, 44, 1101, 45, 15, 789, 87, 56, 62}
差集 -
print(list_1 - list_2)#{65, 44, 1101, 87, 15}
在list_1 中不在list_2中的key
对称差集 ^
print(list_1 ^ list_2)#{64, 65, 5, 38, 87, 56, 44, 1101, 62, 15}
项在 list_1 或 list_2 中,但不会同时出现在二者中