字典(dict)是 Python 中最常用的数据结构之一,很多数据处理的问题,本质上都可以转化为对字典的操作。这一篇聚焦一个核心点:如何把字典用得更高效。
Dict的基础操作
字典本质是 键值对( key-value )映射:
user = {
"name": "Alice",
"age": 20
}
- 取值:
get
# 直接访问
print(user["gender"]) # KeyError
# 相比直接访问,更推荐使用`get`方法
print(user.get("gender")) # None
print(user.get("gender", "未知")) # 默认值
get适合处理不确定字段的数据(例如接口返回)。
- 更新:
update
user.update({"age": 21, "city": "Beijing"})
# 等价于逐个赋值,但更简洁,适合合并数据。
- 获取所有 key:
keys
for key in user.keys():
print(key)
# 通常也可以不用keys(),直接写成:
for key in user:
print(key)
Dict的进阶操作
- Dict推导式
和列表(List)推导式类似,字典也支持推导式,语法和列表推导式几乎一样,只是多了冒号:
scores = {"a": 60, "b": 80, "c": 45}
passed = {k: v for k, v in scores.items() if v >= 60}
# {'a': 60, 'b': 80}
- 用 dict 做“索引结构”
- 计数(频率统计)
text = "apple banana apple orange banana apple"
counter = {}
for word in text.split():
counter[word] = counter.get(word, 0) + 1
print(counter)
# {'apple': 3, 'banana': 2, 'orange': 1}
核心在于:counter.get(word, 0),从而减少了初始化判断。
- 分组
users = [ {"name": "A", "city": "Beijing"}, {"name": "B", "city": "Shanghai"}, {"name": "C", "city": "Beijing"},]
group = {}
for u in users:
city = u["city"]
group.setdefault(city, []).append(u)
print(group)
# {'Beijing': [{'name': 'A', 'city': 'Beijing'}, {'name': 'C', 'city': 'Beijing'}], 'Shanghai': [{'name': 'B', 'city': 'Shanghai'}]}
其中:group.setdefault(city, [])这个方法会检查字典 group 中是否存在键 city:
- 如果存在 :返回该键对应的值(已有的列表)
- 如果不存在 :插入键 city 并设置其值为 [] (空列表),然后返回这个空列表
最后.append(u)将当前用户字典 u 添加到返回的列表中。
defaultdict简化代码
defaultdict是 collections 模块的宝藏类,访问不存在的 key 时自动创建默认值:
from collections import defaultdict
# 计数
counter = defaultdict(int)
for word in text.split():
counter[word] += 1
# 分组
group = defaultdict(list)
for u in users:
group[u["city"]].append(u) # 不需要 get 或 setdefault,代码更干净。
总结
这一篇的重点是把 dict 当作“索引工具”来用。从基础的 get、update,到推导式,再到计数、分组,本质上都是在“快速查找和归类”。
因此,如果你在写代码时遇到“需要分类、统计、去重”的问题,可以优先考虑:能不能用一个字典解决。