不同于普通数据类型, 集合基于hash表存储, 而非连续存储.
集合的声明与字典一样, 都是大括号{}
, 唯一不同的是, 字典内是键值对, 而集合内是多种数据类型.
声明空集合时使用empty_set = set()
, 而不能用empty_set = {}
{}
代表空字典
集合中只会保留不同的数据类型, 所以, 集合通常被用作去重
增
add 向集合中增加元素
pet_set = {"dog", "cat", "goose", "cat", "sheep"}
pet_set.add("pig")
print(pet_set) # {'dog', 'cat', 'pig', 'goose', 'sheep'}
copy 复制一份集合
注意: 直接赋值时, 两个结合共享同一份内存空间, 一个集合改变会导致另一个集合改变
pet_set = {"dog", "cat", "goose", "cat", "sheep"}
animal_set = pet_set.copy()
pet_set.remove("dog")
print(animal_set) # {'dog', 'cat', 'sheep', 'goose'}
update 将另一个集合合并的当前集合中
pet_set = {"dog", "cat", "goose", "cat", "sheep"}
animal_set = {"pig"}
animal_set.update(pet_set)
print(animal_set) # {'dog', 'goose', 'cat', 'pig', 'sheep'}
删
clear 清空集合
pet_set = {"dog", "cat", "goose", "cat", "sheep"}
pet_set.clear()
print(pet_set) # set()
remove 移除集合中的元素(错误移除报异常)
移除不存在的元素会报异常
pet_set = {"dog", "cat", "goose", "cat", "sheep"}
pet_set.remove("cat")
print(pet_set) # {'dog', 'sheep', 'goose'}
pet_set.remove("xxxx") # KeyError: 'xxxx'
pop 随机移除1个集合中的元素
pet_set = {"dog", "cat", "goose", "cat", "sheep"}
pet_set.pop()
print(pet_set) # {'cat', 'sheep', 'goose'}
discard 移除集合中的元素(错误移除不报异常)
pet_set = {"dog", "cat", "goose", "cat", "sheep"}
pet_set.discard("dog")
print(pet_set) # {'dog', 'cat', 'sheep', 'goose'}
pet_set.discard("xxx")
print(pet_set) # {'dog', 'cat', 'sheep', 'goose'}
查
in, not in
pet_set = {"dog", "cat", "goose", "cat", "sheep"}
print("dog" in pet_set) # True
print("dog" not in pet_set) # False
isdisjoint 判断两个集合没有重复的元素
只要两个结合有相同的元素, 返回false
animal_set = {"cat", "goose", "pig"}
pet_set = {"dog"}
print(pet_set.isdisjoint(animal_set)) # True
bird_set = {"duck", "goose"}
print(bird_set.isdisjoint(animal_set)) # False
issubset 判断原集合是否为目标集合的子集
animal_set = {"dog", "cat", "goose"}
pet_set = {"cat", "goose"}
print(pet_set.issubset(animal_set)) # True
print(animal_set.issubset(pet_set)) # False
isupperset 判断原集合是否为目标集合的父集
animal_set = {"dog", "cat", "goose"}
pet_set = {"cat", "goose"}
print(animal_set.issuperset(pet_set)) # True
print(pet_set.issuperset(animal_set)) # False
交集
& 计算两个集合的交集
pet_set = {"dog", "cat", "goose"}
animal_set = {"pig", "cat", "dog"}
print(pet_set & animal_set) # {'dog', 'cat'}
intersection 获取与目标集合的交集, 与&符号运算结果相同
intersection 获取交集后, 并不会改变原集合的值
pet_set = {"dog", "cat", "goose"}
animal_set = {"pig", "cat", "dog"}
print(pet_set.intersection(animal_set))
intersection_update 获取与目标集合的交集, 并改变原集合的值
pet_set = {"dog", "cat", "goose"}
animal_set = {"pig", "goose"}
bird_set = {"duck", "eagle", "goose"}
pet_set.intersection_update(animal_set, bird_set)
print(pet_set)
并集
| 计算两个结合的并集
animal_set = {"dog", "cat", "goose"}
pet_set = {"pig", "goose"}
print(pet_set | animal_set) # {'dog', 'goose', 'cat', 'pig'}
union 计算原集合与目标集合的并集, 与|运算符结果相同
animal_set = {"dog", "cat", "goose"}
pet_set = {"pig", "goose"}
print(pet_set.union(animal_set)) # {'dog', 'goose', 'cat', 'pig'}
差集
difference 返回原集合中存在, 目标集合中没有的元素集合
animal_set = {"dog", "cat", "goose"}
pet_set = {"pig", "goose"}
print(pet_set.difference(animal_set)) # {'pig'}
symmetric_difference 返回原集合与目标集合不重复的集合
animal_set = {"dog", "cat", "goose"}
pet_set = {"pig", "goose"}
print(animal_set.symmetric_difference(pet_set)) # {'dog', 'cat', 'pig'}
print( (animal_set | pet_set).difference(animal_set & pet_set) ) # {'dog', 'cat', 'pig'}
symmetric_difference_update 返回原集合与目标集合不重复的集合, 并结果修改原集合
animal_set = {"dog", "cat", "goose"}
pet_set = {"pig", "goose"}
animal_set.symmetric_difference_update(pet_set)
print(animal_set) # {'dog', 'cat', 'pig'}