1.关于集合
集合是一类元素无序不重复的数据结构,常用场景是元素去重和集合运算。python可以使用大括号{}或者set()函数创建集合,如果创建一个空集合必须用set()而不是{},因为{}是用来表示空字典类型。
对比java,java有set相关的接口,是通过接口层面抽象的,具体有不同的实现类,比如:HashSet、TreeSet等。
2.常用操作
创建集合
#1.使用{}创建,自动去重
names={"li lei","ha mei mei","li lei","song lei"}
print(type(names))
print(names)
names={}
print(type(names))
#2.空set集合用set()函数表示,不能用{}
names=set()
print(type(names))
#3.基于list,tuple等创建
names_list=["li lei","ha mei mei","li lei","song lei"]
names=set(names_list)
print(type(names))
print(names)
name_tuple=("li lei","ha mei mei","li lei","song lei")
names=set(name_tuple)
print(type(names))
print(names)
输出结果:
{'ha mei mei', 'song lei', 'li lei'}
{'ha mei mei', 'song lei', 'li lei'}
{'ha mei mei', 'song lei', 'li lei'}
集合包含
使用in/not in判断是否包含某个元素
names={"li lei","ha mei mei","li lei","song lei"}
print(names)
print("li lei" in names)
print("wang lei" not in names)
输出结果:
{'song lei', 'li lei', 'ha mei mei'}
True
True
集合运算
交集/并集/差集/对称差/子集/超集
names_a={"li lei","ha mei mei","song lei","wang lei"}
names_b={"li lei","ha mei mei","song lei","zhao lei"}
names_c={"li lei","ha mei mei","song lei"}
#交集,用于获取两个或多个集合中共同拥有的元素
print("交集:",names_a.intersection(names_b))
print("交集:",names_a & names_b)
#并集,用于合并两个或多个集合中的所有元素
print("并集:",names_a.union(names_b))
print("并集:",names_a | names_b)
#差集,用于获取一个集合中存在,而另一个集合中不存在的元素
print("并集:",names_a.difference(names_b))
print("并集:",names_a - names_b)
#对称差,用于获取两个集合中不共有的元素
print("对称差:",names_a.symmetric_difference(names_b))
print("对称差:",names_a ^ names_b)
#子集,用于判断一个集合所有元素是否被另一个集合包含
print("子集:",names_c.issubset(names_a))
print("子集:",names_c <= names_a print:names_a.issupersetnames_c print:names_a>= names_c)
输出结果:
交集: {'li lei', 'ha mei mei', 'song lei'}
交集: {'li lei', 'ha mei mei', 'song lei'}
并集: {'li lei', 'zhao lei', 'ha mei mei', 'wang lei', 'song lei'}
并集: {'li lei', 'zhao lei', 'ha mei mei', 'wang lei', 'song lei'}
并集: {'wang lei'}
并集: {'wang lei'}
对称差: {'zhao lei', 'wang lei'}
对称差: {'zhao lei', 'wang lei'}
子集: True
子集: True
超集: True
超集: True
添加元素
#添加元素
names_a={"li lei","ha mei mei"}
names_a.add("zhao lei")
#重复添加,不会报错
names_a.add("zhao lei")
print(names_a)
#可以添加不可变的元组,不能是list/set/dict
names_a.add(("zhao lei","qian lei"))
print(names_a)
#让人费解的update函数
#update函数传入字符串,会将字符串拆分成字符后添加
names_a.update("123")
print(names_a)
#update函数传入元组,会添加每个元组元素
names_a.update(("zhao lei","qian lei"))
print(names_a)
#update函数传入字典,会添加每个字典的key
names_a.update({"test":10})
print(names_a)
输出结果:
{'li lei', 'zhao lei', 'ha mei mei'}
{'li lei', 'zhao lei', ('zhao lei', 'qian lei'), 'ha mei mei'}
{'ha mei mei', 'zhao lei', ('zhao lei', 'qian lei'), 'li lei', '2', '1', '3'}
{'ha mei mei', 'zhao lei', ('zhao lei', 'qian lei'), 'li lei', '2', '1', 'qian lei', '3'}
{'ha mei mei', 'zhao lei', ('zhao lei', 'qian lei'), 'li lei', '2', '1', 'qian lei', '3', 'test'}
删除元素
#删除元素
names_a={"li lei","ha mei mei","song lei","wang lei"}
#尝试remove不存在的元素,会报错:KeyError: 'not exist'
#names_a.remove("not exist")
names_a.remove("song lei")
print(names_a)
#尝试discard不存在的元素,不会报错
names_a.discard("not exist")
names_a.discard("wang lei")
print(names_a)
#pop会随机选一个
names_a.pop()
print(names_a)
#清空集合
names_a.clear()
print(names_a)
输出结果:
{'li lei', 'ha mei mei', 'wang lei'}
{'li lei', 'ha mei mei'}
{'ha mei mei'}
set()
修改元素
集合中的元素都是无序的,所以不能使用下标,一般修改操作可以转化为:先删除,再添加
#修改元素
names_a={"li lei","ha mei mei","song lei","wang lei"}
print(names_a)
names_a.discard("wang lei")
names_a.add("wang li")
print(names_a)
输出结果:
{'song lei', 'wang lei', 'li lei', 'ha mei mei'}
{'ha mei mei', 'song lei', 'wang li', 'li lei'}
集合拷贝
#集合复制
names_a={"li lei","ha mei mei","song lei","wang lei"}
print(names_a)
names_b=names_a.copy()
print(names_b)
输出结果:
{'wang lei', 'li lei', 'ha mei mei', 'song lei'}
{'wang lei', 'li lei', 'ha mei mei', 'song lei'}
本文暂时没有评论,来添加一个吧(●'◡'●)