集合是无序的
集合中的元素是唯一的(可以去重)
集合可用来做关系测试
1、唯一性测试
List=[1,2,3,4,3,2,1,5,6,7,8]
List1=set(List)
print(List1)
运行结果:{1, 2, 3, 4, 5, 6, 7, 8}
List1为去重后并且按照大小顺序排列,在输出
2、关系测试(取交集)
List2={7,6,5,4}
List1={1,2,3,4,5,6,7,8}
print(List1.intersection(List2))
运行结果:{4, 5, 6, 7}
3、取并集
print(List1.union(List2))
运行结果:{1, 2, 3, 4, 5, 6, 7, 8}
4、取差集(去掉1中存在,而2中不存在的元素)
print(List1.difference(List2))
运行结果:{8, 1, 2, 3}
5、判断子集
print(List2.issubset(List1))
判断List2是不是List1的子集
6、判断父集
print(List1.issuperset(List2))
判断List1是不是List2的父集
7、取对称差集(会去掉1和2中都存在的元素)
print(List1.symmetric_difference(List2))
运行结果:{1, 2, 3, 8}
8、判断是否没有共同元素
print(List2.isdisjoint({0,10}))
9、交集
print(List1 & List2)
10、并集
print(List1 | List2)
11、差集
print(List1 - List2)
12、对称差集
print(List1 ^ List2)
13、添加元素
List=[1,2,3,4,3,2,1,5,6,7,8]
List1=set(List)
List1.add(99999)
print(List1)
运行结果:{1, 2, 3, 4, 5, 6, 7, 8, 99999}
List1.update([99,999])
print(List1)
运行结果:{1, 2, 3, 4, 5, 6, 7, 8, 99, 999}
14、删除数据
Set={1,2,3,4,5}
Set.remove(5)
print(Set)
运行结果:{1, 2, 3, 4}
print(Set.pop())
运行结果:1
print(Set)
运行结果:{2,3,4}
Set.discard(4)
print(Set)
运行结果:{2,3}