Python集合基础:创建、运算与方法详解
本文详细介绍Python中集合(set)的基础操作,涵盖使用花括号或set()函数创建集合、区分空集合与字典的方法。深入解析成员运算、交并差及比较运算,演示常用增删改方法,并介绍不可变集合frozenset的特性与使用场景。
1、集合
在Python语言中,集合(set)的概念与数学中的集合高度一致。它是一种无序的数据结构,不支持通过索引访问元素,且内部不允许存在重复的值。
2、创建集合
在Python中,可以通过花括号{}字面量语法,或者调用set()函数来构建集合。需要注意的是,使用{}语法时必须至少包含一个元素;若需创建空集合,严禁使用{},因为这会生成一个空字典。正确的做法是使用set()函数来实例化一个空集合。
# 使用字面量方法创建集合
set1 = {"Hello", "World"}
print(type(set1), set1) # <class 'set'> {'World', 'Hello'}
# print(set1[1]) # TypeError: 'set' object does not support indexing
# 使用set()的方式创建
set2 = set("hello") # {'l', 'o', 'h', 'e'}集合会去掉重复的元素,即"l"
print(set2)
# 创建空集合
set3 = {}
set4 = set()
print(type(set3), type(set4)) # <class 'dict'>字典 <class 'set'>
list1 = [1, 2, 3, 4, 3, 5, 1, 3]
# 将列表转换为集合
set6 = set(list1)
print(type(set6), set6) # <class 'set'> {1, 2, 3, 4, 5}
# 使用生成列生成列表
set5 = {num for num in range(20) if num % 3 == 0}
print(set5) # {0, 3, 6, 9, 12, 15, 18}
# 遍历循环
for ch in set5:
print(ch)
注意:集合内的元素必须是不可变类型,例如整数、浮点数、字符串或元组等。这意味着可变类型的对象不能作为集合的元素。由于集合本身是可变的,因此集合也不能作为另一个集合的元素。
3、集合的运算
集合数据类型提供了丰富的运算符支持,包括成员资格检查、交集、并集、差集以及比较运算(如相等性判断、子集与超集检测)等。
3.1 成员运算
可以利用in和not in运算符来检测某个元素是否存在于集合中。
示例代码如下:
set1 = {"你好", "Python", "这个是", "集合", "set"}
print("Python" in set1) # True
print("您好" in set1) # False
print("set" not in set1) # False
print("list" not in set1) # True
3.2 交并差运算
Python中的集合支持数学意义上的交集、并集和差集运算,且既可以通过运算符直接操作,也可以调用对应的方法来实现。
示例代码如下:
set1 = {1, 2, 3, 4, 5, 6, 7} set2 = {2, 4, 6, 8, 10}
# 交集(咱俩都有的)
# 方法一: 使用 & 运算符
print(set1 & set2) # {2, 4, 6}
# 方法二: 使用intersection方法
print(set1.intersection(set2)) # {2, 4, 6}
# 并集(咱俩加在一起)
# 方法一: 使用 | 运算符
print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7, 8, 10}
# 方法二: 使用union方法
print(set1.union(set2)) # {1, 2, 3, 4, 5, 6, 7, 8, 10}
# 差集(我不要你有的)
# 方法一: 使用 - 运算符
print(set1 - set2) # {1, 3, 5, 7}
# 方法二: 使用difference方法
print(set1.difference(set2)) # {1, 3, 5, 7}
# 对称差(咱俩不要你的和我的,只要专一的)
# 方法一: 使用 ^ 运算符
print(set1 ^ set2) # {1, 3, 5, 7, 8, 10}
# 方法二: 使用symmetric_difference方法
print(set1.symmetric_difference(set2)) # {1, 3, 5, 7, 8, 10}
# 方法三: 对称差相当于两个集合的并集减去交集
print((set1 | set2) - (set1 & set2)) # {1, 3, 5, 7, 8, 10}
集合的交集、并集和差集运算还可以与赋值运算符结合,形成复合赋值运算。
示例代码如下:
set1 = {1, 3, 5, 7}
set2 = {2, 4, 6}
# 将set1和set2求并集再赋值给set1
# 也可以通过set1.update(set2)来实现
set1 |= set2 # set1 = set1 | set2
print(set1) # {1, 2, 3, 4, 5, 6, 7}
set3 = {3, 6, 9}
# 将set1和set3求交集再赋值给set1
# 也可以通过set1.intersection_update(set3)来实现
set1 &= set3 # set1 = set1 & set3
print(set1) # {3, 6}
3.3 比较运算
可以使用==和!=运算符对两个集合进行相等性判断。若两个集合包含完全相同的元素,则==比较的结果为True,否则返回False;!=运算符的逻辑则相反。
若集合A中的所有元素均属于集合B,则称A是B的子集,B是A的超集;若A是B的子集且A不等于B,则称A是B的真子集。判断子集和超集可使用<和>运算符。示例代码如下:
set1 = {1, 3, 5}
set2 = {1, 2, 3, 4, 5}
set3 = set2
# <运算符表示真子集,<=运算符表示子集
print(set1 < set2, set1 <= set2) # True True
print(set2 < set3, set2 <= set3) # False True
# 通过issubset方法也能进行子集判断
print(set1.issubset(set2)) # True
print(set2.issubset(set2)) # True
# issubset方法和<=运算符是等价的
# 反过来可以用issuperset或>运算符进行超集判断
print(set2.issuperset(set1)) # True
print(set2 > set1) # True
4、集合的方法
集合属于可变序列类型,可以通过集合类型提供的方法来修改其元素。
示例代码:
# 创建一个空集合
set1 = set()
# 通过add()方法为集合添加元素
set1.add(1)
set1.add(2)
print(set1) # {1, 2}
# 通过update()给集合添加元素
set1.update({1, 2, 3, 4, 5})
print(set1) # {1, 2, 3, 4, 5} 元素在集合中已存在,该元素只会出现一次
# 通过discard()方法删除指定元素
set1.discard(4)
set1.discard(2)
set1.discard(22) # 集合里面没有不会出错
print(set1) # {1, 3, 5}
# 通过remove()方法删除指定元素
# set1.remove(6) # KeyError: 6,使用remove()方法删除指定元素时先做成员运算就不会出错了
if 6 in set1:
set1.remove(6)
# pop方法可以从集合中随机删除一个元素并返回该元素
print(set1.pop()) # 1 本次剔除的是1
# clear方法可以清空整个集合
set1.clear()
print(type(set1)) # <class 'set'>
print(set1) # set()
# 通过isdisjoint()方法判断两个集合有没有重复的元素,有返回False没有则返回Ture
set2 = {"Hello", "World"}
set3 = {"Hello", "一碗周"}
set4 = {"Hi", "Python"}
print(set2.isdisjoint(set3)) # False
print(set2.isdisjoint(set4)) # True
5、不可变集合
Python还提供了一种不可变类型的集合,称为frozenset。它与普通集合的关系类似于列表与元组的关系。除了不能执行添加和删除元素的操作外,frozenset在其他功能方面与set基本一致。
示例代码:
set1 = frozenset({1, 3, 5, 7})
set2 = frozenset(range(1, 6))
print(set1 & set2) # frozenset({1, 3, 5})
print(set1 | set2) # frozenset({1, 2, 3, 4, 5, 7})
print(set1 - set2) # frozenset({7})
print(set1 < set2) # False
到此这篇关于Python集合的基础操作的文章就介绍到这了,更多相关Python集合内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









