当前位置:首页>python>Python集合详解

Python集合详解

  • 2026-10-11 06:37:45
Python集合详解

集合(Set)是Python中一种重要的数据结构,用于存储无序的、不重复的元素集合。Python集合提供了高效的成员检查、去重和集合运算功能。本文将详细介绍Python集合的特性、操作和最佳实践。

一、集合概述

1. 什么是集合?

集合是Python中的一种无序、可变的容器类型,用于存储不重复的元素。集合中的元素必须是可哈希的(不可变类型),如数字、字符串、元组等。

2. 集合的特点

  • 无序性
    :集合中的元素没有固定顺序,每次遍历可能得到不同的顺序
  • 唯一性
    :集合中的元素必须唯一,重复元素会被自动去重
  • 可变性
    :集合创建后可以修改其内容(添加、删除元素)
  • 可迭代性
    :可以使用循环遍历集合中的元素
  • 元素的可哈希性
    :集合中的元素必须是可哈希的(不可变类型)
  • 高效的成员检查
    :集合的成员检查操作效率很高(O(1)时间复杂度)

3. 集合的表示

集合使用花括号{}表示,元素之间用逗号,分隔。注意:空集合不能使用{}表示({}表示空字典),必须使用set()函数创建。

# 集合示例
numbers ={1,2,3,4,5}
fruits ={"apple","banana","orange"}

# 注意:空集合必须使用set()创建
empty_set =set()
print(type(empty_set))# <class 'set'>

# {}表示空字典
empty_dict ={}
print(type(empty_dict))# <class 'dict'>

二、集合的创建

1. 基本创建方法

使用花括号{}直接创建集合:

# 基本创建方法示例

# 创建包含元素的集合
colors ={"red","green","blue"}
numbers ={1,2,3,4,5}

# 重复元素会被自动去重
duplicates ={1,2,2,3,3,3}
print(duplicates)# {1, 2, 3}

# 混合类型元素
mixed ={1,"apple",3.14,(1,2)}
print(mixed)# {1, 3.14, 'apple', (1, 2)}

2. 使用set()函数创建

set()函数可以从可迭代对象创建集合:

# 使用set()函数创建集合示例

# 从列表创建集合
list_to_set =set([1,2,3,4,5])
print(list_to_set)# {1, 2, 3, 4, 5}

# 从元组创建集合
tuple_to_set =set((1,2,3,4,5))
print(tuple_to_set)# {1, 2, 3, 4, 5}

# 从字符串创建集合
string_to_set =set("hello")
print(string_to_set)# {'h', 'e', 'l', 'o'}(注意重复的'l'被去重)

# 从range对象创建集合
range_to_set =set(range(1,6))
print(range_to_set)# {1, 2, 3, 4, 5}

# 从字典创建集合(只包含键)
dict_to_set =set({"a":1,"b":2,"c":3})
print(dict_to_set)# {'a', 'b', 'c'}

3. 使用集合推导式创建

集合推导式是一种简洁创建集合的方法,语法为{expression for item in iterable if condition}:

# 使用集合推导式创建集合示例

# 基本集合推导式
squares ={x **2for x inrange(1,6)}
print(squares)# {1, 4, 9, 16, 25}

# 带有条件的集合推导式
evens ={x for x inrange(1,11)if x %2==0}
print(evens)# {2, 4, 6, 8, 10}

# 从字符串创建集合(去重)
word ="python"
unique_chars ={char for char in word}
print(unique_chars)# {'p', 'y', 't', 'h', 'o', 'n'}

# 嵌套集合推导式
matrix =[[1,2,3],[4,5,6],[7,8,9]]
flattened ={num for row in matrix for num in row}
print(flattened)# {1, 2, 3, 4, 5, 6, 7, 8, 9}

三、集合的基本操作

1. 访问集合元素

由于集合是无序的,不能通过索引访问元素,只能使用循环遍历或检查元素是否存在:

# 访问集合元素示例

fruits ={"apple","banana","orange"}

# 使用for循环遍历
for fruit in fruits:
print(fruit)

# 注意:集合是无序的,每次遍历可能得到不同的顺序

# 使用in运算符检查元素是否存在
print("apple"in fruits)# True
print("grape"notin fruits)# True

2. 添加集合元素

可以使用add()和update()方法添加元素到集合中:

# 添加集合元素示例

numbers ={1,2,3}

# 使用add()方法添加单个元素
numbers.add(4)
print(numbers)# {1, 2, 3, 4}

# 添加重复元素(不会有任何变化)
numbers.add(3)
print(numbers)# {1, 2, 3, 4}

# 使用update()方法添加多个元素(可接受任何可迭代对象)
numbers.update([4,5,6])# 添加列表
print(numbers)# {1, 2, 3, 4, 5, 6}

numbers.update((6,7,8))# 添加元组
print(numbers)# {1, 2, 3, 4, 5, 6, 7, 8}

numbers.update("90")# 添加字符串(会被拆分为单个字符)
print(numbers)# {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

3. 删除集合元素

可以使用多种方法删除集合中的元素:

# 删除集合元素示例

numbers ={0,1,2,3,4,5,6,7,8,9}

# 使用remove()方法删除元素(元素不存在会报错)
numbers.remove(0)
print(numbers)# {1, 2, 3, 4, 5, 6, 7, 8, 9}

# 删除不存在的元素会报错
try:
    numbers.remove(10)
except KeyError as e:
print(f"错误:{e}")# 错误:10

# 使用discard()方法删除元素(元素不存在不会报错)
numbers.discard(1)
print(numbers)# {2, 3, 4, 5, 6, 7, 8, 9}

# 删除不存在的元素不会报错
numbers.discard(10)
print(numbers)# {2, 3, 4, 5, 6, 7, 8, 9}

# 使用pop()方法删除并返回任意一个元素
item = numbers.pop()
print(item, numbers)# 2 {3, 4, 5, 6, 7, 8, 9}(实际输出可能不同,因为集合是无序的)

# 使用clear()方法清空集合
numbers.clear()
print(numbers)# set()

4. 集合的其他基本操作

# 集合的其他基本操作示例

numbers1 ={1,2,3,4,5}
numbers2 ={4,5,6,7,8}

# 获取集合的长度
print(len(numbers1))# 5

# 集合的复制
numbers3 = numbers1.copy()
print(numbers3)# {1, 2, 3, 4, 5}
print(numbers3 is numbers1)# False

# 检查集合是否为空
ifnot numbers3:
print("集合为空")
else:
print("集合不为空")# 集合不为空

# 集合的比较
set1 ={1,2,3}
set2 ={3,2,1}
set3 ={1,2,3,4}

print(set1 == set2)# True(集合的相等性不考虑顺序)
print(set1 != set2)# False
print(set1 < set3)# True(set1是set3的真子集)
print(set1 <= set3)# True(set1是set3的子集)
print(set3 > set1)# True(set3是set1的真超集)
print(set3 >= set1)# True(set3是set1的超集)

四、集合的常用方法

Python集合提供了丰富的方法用于操作集合:

1. 基本集合操作方法

# 基本集合操作方法示例

set1 ={1,2,3,4,5}
set2 ={4,5,6,7,8}

# union():返回两个集合的并集
a = set1.union(set2)
print(a)# {1, 2, 3, 4, 5, 6, 7, 8}

# intersection():返回两个集合的交集
b = set1.intersection(set2)
print(b)# {4, 5}

# difference():返回set1中存在但set2中不存在的元素
c = set1.difference(set2)
print(c)# {1, 2, 3}

# symmetric_difference():返回两个集合中不同时存在的元素
d = set1.symmetric_difference(set2)
print(d)# {1, 2, 3, 6, 7, 8}

2. 集合更新方法

# 集合更新方法示例

set1 ={1,2,3,4,5}
set2 ={4,5,6,7,8}

# update():将set2的元素添加到set1中
set1_copy = set1.copy()
set1_copy.update(set2)
print(set1_copy)# {1, 2, 3, 4, 5, 6, 7, 8}

# intersection_update():仅保留set1和set2的交集
set1_copy = set1.copy()
set1_copy.intersection_update(set2)
print(set1_copy)# {4, 5}

# difference_update():从set1中删除与set2相同的元素
set1_copy = set1.copy()
set1_copy.difference_update(set2)
print(set1_copy)# {1, 2, 3}

# symmetric_difference_update():保留两个集合中不同时存在的元素
set1_copy = set1.copy()
set1_copy.symmetric_difference_update(set2)
print(set1_copy)# {1, 2, 3, 6, 7, 8}

3. 集合关系方法

# 集合关系方法示例

set1 ={1,2,3}
set2 ={1,2,3,4,5}
set3 ={4,5,6}
set4 ={1,2,3}

# issubset():检查set1是否是set2的子集
print(set1.issubset(set2))# True
print(set1.issubset(set3))# False
print(set1.issubset(set4))# True

# issuperset():检查set1是否是set2的超集
print(set2.issuperset(set1))# True
print(set1.issuperset(set2))# False
print(set1.issuperset(set4))# True

# isdisjoint():检查两个集合是否没有交集
print(set1.isdisjoint(set3))# True(没有交集)
print(set1.isdisjoint(set2))# False(有交集)

4. 其他方法

# 其他方法示例

# 使用copy()方法复制集合
set1 ={1,2,3}
set2 = set1.copy()
print(set2)# {1, 2, 3}
print(set2 is set1)# False

# 使用clear()方法清空集合
set2.clear()
print(set2)# set()

# 使用pop()方法删除任意元素
set1 ={1,2,3}
element = set1.pop()
print(element)# 1(实际输出可能不同)
print(set1)# {2, 3}

五、集合的高级操作

1. 集合的数学运算

Python集合支持多种数学运算,包括并集、交集、差集和对称差集:

# 集合的数学运算示例

set1 ={1,2,3,4,5}
set2 ={4,5,6,7,8}

# 并集(|)
print(set1 | set2)# {1, 2, 3, 4, 5, 6, 7, 8}

# 交集(&)
print(set1 & set2)# {4, 5}

# 差集(-)
print(set1 - set2)# {1, 2, 3}
print(set2 - set1)# {6, 7, 8}

# 对称差集(^)
print(set1 ^ set2)# {1, 2, 3, 6, 7, 8}

# 子集(<=)和真子集(<)
set3 ={1,2,3}
print(set3 <= set1)# True
print(set3 < set1)# True

# 超集(>=)和真超集(>)
print(set1 >= set3)# True
print(set1 > set3)# True

2. 集合的推导式

集合推导式是一种简洁创建集合的方法,语法与列表推导式类似,但使用花括号:

# 集合推导式示例

# 基本集合推导式
squares ={x **2for x inrange(1,6)}
print(squares)# {1, 4, 9, 16, 25}

# 带有条件的集合推导式
even_squares ={x **2for x inrange(1,11)if x %2==0}
print(even_squares)# {4, 16, 36, 64, 100}

# 嵌套集合推导式
matrix =[[1,2,3],[4,5,6],[7,8,9]]
flattened ={num for row in matrix for num in row}
print(flattened)# {1, 2, 3, 4, 5, 6, 7, 8, 9}

# 去重和转换
word ="hello world"
unique_chars ={char.lower()for char in word if char.isalpha()}
print(unique_chars)# {'h', 'e', 'l', 'o', 'w', 'r', 'd'}

3. 集合的迭代

集合可以使用for循环迭代,也可以使用enumerate()函数获取索引和值:

# 集合的迭代示例

fruits ={"apple","banana","orange","grape"}

# 基本迭代
for fruit in fruits:
print(fruit)

# 注意:集合是无序的,每次遍历可能得到不同的顺序

# 使用enumerate()获取索引和值
for index, fruit inenumerate(fruits):
print(f"索引{index}处的水果是:{fruit}")

# 使用zip()同时迭代多个集合
colors ={"red","green","blue"}
for fruit, color inzip(fruits, colors):
print(f"{fruit}是{color}的")

4. 冻结集合(frozenset)

冻结集合是不可变的集合类型,用于需要哈希的场景(如作为字典的键或集合的元素):

# 冻结集合示例

# 创建冻结集合
fs1 =frozenset({1,2,3,4,5})
fs2 =frozenset([4,5,6,7,8])

# 冻结集合的基本操作
print(fs1)# frozenset({1, 2, 3, 4, 5})
print(len(fs1))# 5
print(3in fs1)# True

# 冻结集合支持的方法(不包含修改集合的方法)
print(fs1.union(fs2))# frozenset({1, 2, 3, 4, 5, 6, 7, 8})
print(fs1.intersection(fs2))# frozenset({4, 5})
print(fs1.difference(fs2))# frozenset({1, 2, 3})
print(fs1.symmetric_difference(fs2))# frozenset({1, 2, 3, 6, 7, 8})

# 冻结集合可以作为字典的键
d ={fs1:"冻结集合键"}
print(d[fs1])# 冻结集合键

# 冻结集合可以作为集合的元素
s ={fs1, fs2}
print(s)# {frozenset({1, 2, 3, 4, 5}), frozenset({4, 5, 6, 7, 8})}

# 冻结集合不能被修改
try:
    fs1.add(6)
except AttributeError as e:
print(f"错误:{e}")# 错误:'frozenset' object has no attribute 'add'

六、集合的性能分析

1. 时间复杂度

集合的核心优势是高效的成员检查,主要操作的时间复杂度如下:

操作
时间复杂度
描述
成员检查
O(1)
检查元素是否在集合中
添加元素
O(1)
添加单个元素到集合
删除元素
O(1)
删除集合中的元素
并集
O(n + m)
两个集合的并集(n和m是集合的大小)
交集
O(min(n, m))
两个集合的交集
差集
O(n)
两个集合的差集
对称差集
O(n + m)
两个集合的对称差集
子集检查
O(n)
检查一个集合是否是另一个集合的子集
超集检查
O(n)
检查一个集合是否是另一个集合的超集

2. 性能优化建议

  • 对于频繁的成员检查操作,使用集合而不是列表
  • 对于需要去重的场景,使用集合而不是手动去重
  • 对于需要进行集合运算的场景,使用集合而不是其他数据结构
  • 避免在集合中存储可变元素
  • 对于需要哈希的场景,使用冻结集合

3. 集合与列表的性能比较

# 集合与列表的性能比较示例

import time

# 创建大列表和大集合
n =1000000
my_list =list(range(n))
my_set =set(range(n))

# 测试成员检查性能
start = time.time()
for i inrange(n):
if i == n -1:
pass
end = time.time()
print(f"列表成员检查耗时:{end - start:.6f}秒")

start = time.time()
for i inrange(n):
if i in my_set:
pass
end = time.time()
print(f"集合成员检查耗时:{end - start:.6f}秒")

# 测试去重性能
start = time.time()
list_with_duplicates =list(range(n))+list(range(n))
unique_list =[]
for item in list_with_duplicates:
if item notin unique_list:
        unique_list.append(item)
end = time.time()
print(f"列表去重耗时:{end - start:.6f}秒")

start = time.time()
list_with_duplicates =list(range(n))+list(range(n))
unique_set =set(list_with_duplicates)
end = time.time()
print(f"集合去重耗时:{end - start:.6f}秒")

七、集合的最佳实践

1. 适用场景

  • 需要存储不重复元素的场景
  • 需要进行频繁成员检查的场景
  • 需要进行集合运算(并集、交集、差集)的场景
  • 需要去重的场景
  • 需要表示数学集合的场景

2. 最佳实践

# 最佳实践示例

# 1. 使用集合进行去重
# 不好的做法
list_with_duplicates =[1,2,2,3,3,3]
unique_list =[]
for item in list_with_duplicates:
if item notin unique_list:
        unique_list.append(item)

# 好的做法
list_with_duplicates =[1,2,2,3,3,3]
unique_list =list(set(list_with_duplicates))

# 2. 使用集合进行成员检查
# 不好的做法
names =["张三","李四","王五","赵六"]
if"王五"in names:# O(n)时间复杂度
print("王五在列表中")

# 好的做法
names ={"张三","李四","王五","赵六"}
if"王五"in names:# O(1)时间复杂度
print("王五在集合中")

# 3. 使用集合进行集合运算
# 好的做法
students ={"张三","李四","王五","赵六"}
attended ={"张三","李四","王五"}
absent = students - attended  # 计算缺席的学生
print(f"缺席的学生:{absent}")# 缺席的学生:{'赵六'}

# 4. 避免在集合中存储可变元素
# 不好的做法
try:
    bad_set ={[1,2],[3,4]}
except TypeError as e:
print(f"错误:{e}")# 错误:unhashable type: 'list'

# 好的做法
good_set ={(1,2),(3,4)}# 使用元组代替列表

# 5. 使用冻结集合作为字典键
# 好的做法
points ={frozenset({(0,0),(1,1)}):"对角线",frozenset({(0,0),(0,1)}):"垂直线"}
print(points[frozenset({(0,0),(1,1)})])# 对角线

3. 常见错误

# 常见错误示例

# 错误1:尝试使用可变元素作为集合的元素
try:
    s ={[1,2],[3,4]}
except TypeError as e:
print(f"错误:{e}")# 错误:unhashable type: 'list'

# 错误2:尝试通过索引访问集合元素
try:
    s ={1,2,3}
print(s[0])
except TypeError as e:
print(f"错误:{e}")# 错误:'set' object does not support indexing

# 错误3:使用{}创建空集合
s ={}
print(type(s))# <class 'dict'>(这是一个字典,不是集合)

# 正确的做法
empty_set =set()
print(type(empty_set))# <class 'set'>

# 错误4:忽略集合的无序性
names ={"张三","李四","王五"}
# 不要假设集合的顺序
for name in names:
print(name)# 输出顺序可能不同

# 错误5:混淆集合的并集和更新操作
set1 ={1,2,3}
set2 ={4,5,6}

# union()方法返回新集合
result = set1.union(set2)
print(set1)# {1, 2, 3}(原集合不变)
print(result)# {1, 2, 3, 4, 5, 6}(新集合)

# update()方法修改原集合
set1.update(set2)
print(set1)# {1, 2, 3, 4, 5, 6}(原集合已修改)

八、与其他数据结构的比较

特性
集合
列表
元组
字典
存储方式
元素集合
元素序列
元素序列
键值对
访问方式
成员检查
通过索引
通过索引
通过键
可变性
可变
可变
不可变
可变
有序性
无序
有序
有序
Python 3.7+有序
元素唯一性
元素必须唯一
元素可以重复
元素可以重复
键必须唯一
可哈希性
元素必须可哈希
不可哈希
可哈希
键必须可哈希
查找效率
O(1)
O(n)
O(n)
O(1)
内存占用
较大
较小
较小
较大
适用场景
去重、成员检查、集合运算
有序元素集合、需要频繁修改
不可变元素集合、需要作为字典键
键值映射、快速查找

九、总结

集合是Python中一种强大的数据结构,具有以下特点:

  1. 无序性
    :集合中的元素没有固定顺序
  2. 唯一性
    :集合中的元素必须唯一,自动去重
  3. 可变性
    :可以动态添加、删除元素
  4. 高效的成员检查
    :成员检查操作的时间复杂度为O(1)
  5. 丰富的集合运算
    :支持并集、交集、差集、对称差集等运算
  6. 支持集合推导式
    :提供简洁的集合创建方式
  7. 冻结集合
    :不可变的集合类型,用于需要哈希的场景

集合在以下场景中特别有用:

  • 需要存储不重复元素的场景
  • 需要进行频繁成员检查的场景
  • 需要进行集合运算的场景
  • 需要去重的场景

通过掌握Python集合的特性和操作方法,可以编写出更高效、更优雅的代码。在实际开发中,应根据具体需求选择合适的数据结构,充分发挥集合的优势。


发布网站:荣殿教程(zhangrongdian.com)

作者:张荣殿

最新文章

随机文章