collections模块
Python collections
模块功能与用法总结
collections
是 Python 标准库中提供的高性能数据结构模块,扩展了内置数据类型(如 list
、dict
),适用于特殊场景下的数据操作。以下是其核心功能及常用类的用法:
一、常用数据结构
1. namedtuple
(命名元组)
功能:创建带有字段名的元组,增强代码可读性。
适用场景:替代简单的类或元组,表示不可变数据(如坐标、数据库记录)。
示例:
from collections import namedtuple# 定义命名元组类型
Point = namedtuple("Point", ["x", "y"])
p = Point(10, 20)
print(p.x, p.y) # 10 20
print(p[0]) # 10(仍支持索引访问)
2. defaultdict
(默认字典)
功能:为字典提供默认值,避免 KeyError
。
适用场景:统计频次、分组归类。
示例:
from collections import defaultdict# 默认值为 int(初始为0)
count = defaultdict(int)
words = ["apple", "banana", "apple"]
for word in words:count[word] += 1
print(count) # {'apple': 2, 'banana': 1}# 默认值为 list
group = defaultdict(list)
data = [("A", 1), ("B", 2), ("A", 3)]
for key, value in data:group[key].append(value)
print(group) # {'A': [1, 3], 'B': [2]}
3. Counter
(计数器)
功能:快速统计可迭代对象中元素的频次。
适用场景:词频统计、TopN 查询。
示例:
from collections import Counterwords = ["apple", "banana", "apple", "orange", "banana", "apple"]
counter = Counter(words)
print(counter) # {'apple': 3, 'banana': 2, 'orange': 1}
print(counter.most_common(2)) # [('apple', 3), ('banana', 2)]
4. deque
(双端队列)
功能:高效的头尾插入和删除操作(线程安全)。
适用场景:队列/栈实现、滑动窗口算法。
示例:
from collections import dequed = deque([1, 2, 3])
d.appendleft(0) # 头部插入
d.append(4) # 尾部插入
print(d) # deque([0, 1, 2, 3, 4])d.popleft() # 头部删除
d.pop() # 尾部删除
print(d) # deque([1, 2, 3])
5. OrderedDict
(有序字典)
功能:保持键的插入顺序(Python 3.7+ 后普通 dict
已有序,但仍需此类的额外功能)。
适用场景:需要顺序遍历或记住插入顺序的字典操作。
示例:
from collections import OrderedDictod = OrderedDict()
od["a"] = 1
od["b"] = 2
od.move_to_end("a") # 将键移动到末尾
print(od) # OrderedDict([('b', 2), ('a', 1)])
6. ChainMap
(链式字典)
功能:将多个字典合并为一个逻辑视图,查找时按顺序检查。
适用场景:多层配置优先级、合并多个字典(不创建新对象)。
示例:
from collections import ChainMapdict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
chain = ChainMap(dict1, dict2)
print(chain["b"]) # 2(优先返回 dict1 的值)
print(chain["c"]) # 4(dict2 的值)
二、其他实用类
1. UserDict
/ UserList
/ UserString
功能:自定义字典、列表、字符串的子类化基类。
适用场景:需要扩展或修改内置类型行为时。
示例:
from collections import UserDictclass CaseInsensitiveDict(UserDict):def __setitem__(self, key, value):super().__setitem__(key.lower(), value)d = CaseInsensitiveDict()
d["Key"] = "value"
print(d["key"]) # "value"
三、性能对比
数据结构 | 优势场景 | 时间复杂度 |
---|---|---|
defaultdict | 避免 KeyError | O(1) 插入/查询 |
Counter | 快速频次统计 | O(n) 统计 |
deque | 高效头尾操作 | O(1) 头尾插入/删除 |
OrderedDict | 顺序遍历 | O(1) 插入/查询 |
四、总结
需求 | 推荐类 |
---|---|
需要字段名的元组 | namedtuple |
避免字典 KeyError | defaultdict |
元素频次统计 | Counter |
高效头尾操作 | deque |
保持插入顺序的字典 | OrderedDict |
合并多字典(逻辑视图) | ChainMap |
官方文档:Python collections