一:定义
集合是无序的,不重复的数据类型,本身是不可哈希的(所以不能做字典的键),但是里面的元素是可哈希。
二:涉及操作
去重:把集合先变成列表,然后在将列表变成集合,就是去重。
三:集合
1 交集(&,intersection)
set1={1,2,"alex",}set2={1,2,"alex",3,4}print(set1.intersection(set2))print(set1&set2)
输出结果
2 并集(|,union)
set1={1,2,"alex",}set2={1,2,"alex",3,4}print(set2.union(set1))print(set1|set2)
输出结果
3差集(-,difference)
set1={1,2,"alex",}set2={1,2,"alex",3,4}print(set2.difference(set1))print(set2-set1)
输出结果
4 反交集(^, symmetric_difference ))
2,"alex",}set2={1,2,"alex",3,4}print(set2.symmetric_difference(set1))print(set2^set1)
输出结果:
2,"alex",}set2={1,2,"alex",3,4}print(set2.symmetric_difference(set1))print(set2^set1)
5 子集和超集(<,issubset, >,issuperset)
子集:(<,issubset)
set1={1,2,"alex",}set2={1,2,"alex",3,4}print(set1.issubset(set2))print(set1
打印结果:
超集:(>,issuperset)
set1={1,2,"alex",}set2={1,2,"alex",3,4}print(set2.issuperset(set1))print(set2>set1)
打印结果