#每天一个编程技巧#掌握高效的迭代技巧可以显著提升Python代码的性能和可读性。以下是Python中各种高效迭代的方法和技巧: 1. 基本迭代优化1.1 使用enumerate()获取索引和值# 传统方式(不推荐)for i in range(len(items)): print(i, items[i])# Pythonic方式for i, item in enumerate(items): print(i, item)# 指定起始索引for i, item in enumerate(items, start=1): print(f'Item {i}: {item}') 1.2 使用zip()并行迭代
2. 高级迭代技巧2.1 使用itertools模块from itertools import islice, cycle, repeat, chain# islice - 迭代切片for item in islice(range(100), 5, 10): # 获取5-9项 print(item)# cycle - 无限循环迭代count = 0for item in cycle(['a', 'b', 'c']): print(item) count += 1 if count > 5: break# repeat - 重复元素for item in repeat('Python', 3): # 重复3次 print(item)# chain - 连接多个可迭代对象for item in chain([1, 2], ['a', 'b'], [True, False]): print(item) 2.2 使用生成器表达式
3. 字典迭代技巧3.1 高效字典迭代方法d = {'a': 1, 'b': 2, 'c': 3}# 迭代键for key in d: # 等同于 d.keys() print(key)# 迭代值for value in d.values(): print(value)# 迭代键值对for key, value in d.items(): # Python 3中items()是高效的 print(key, value) 3.2 字典推导式
4. 文件高效迭代4.1 逐行读取大文件# 低效方式(整个文件读入内存)with open('large_file.txt') as f: lines = f.readlines() # 可能内存不足 for line in lines: process(line)# 高效方式(逐行迭代)with open('large_file.txt') as f: for line in f: # 文件对象本身就是可迭代的 process(line) 4.2 使用生成器处理文件
5. 条件迭代技巧5.1 使用filter()和map()def read_in_chunks(file_object, chunk_size=1024): '''生成器函数:按块读取文件''' while True: data = file_object.read(chunk_size) if not data: break yield datawith open('huge_file.bin', 'rb') as f: for chunk in read_in_chunks(f): process(chunk) 5.2 使用any()和all()短路评估
6. 嵌套结构迭代6.1 扁平化嵌套迭代from collections.abc import Iterabledef flatten(items): '''递归扁平化嵌套结构''' for item in items: if isinstance(item, Iterable) and not isinstance(item, (str, bytes)): yield from flatten(item) else: yield itemnested = [1, [2, [3, 4], 5]]flat_list = list(flatten(nested)) # [1, 2, 3, 4, 5] 6.2 使用itertools.product多重循环
7. 性能优化技巧7.1 避免在循环中重复计算# 不推荐for item in data: result = complex_calculation(item) * factor + offset# 推荐 - 预先计算不变的部分adjusted_factor = factor * offsetfor item in data: result = complex_calculation(item) * adjusted_factor 7.2 使用局部变量加速访问
7.3 使用__slots__减少内存占用class Point: __slots__ = ['x', 'y'] # 固定属性列表,减少内存 def __init__(self, x, y): self.x = x self.y = ypoints = [Point(i, i*2) for i in range(100000)] # 比普通类更省内存 8. 自定义可迭代对象8.1 实现迭代器协议
8.2 生成器函数def fibonacci(limit): a, b = 0, 1 while a < limit: yield a a, b = b, a + bfor num in fibonacci(100): print(num) # 0,1,1,2,3,5,8,13,21,34,55,89 9. 实用工具函数9.1 使用collections.defaultdict
9.2 使用functools.reducefrom functools import reduce# 累积计算product = reduce(lambda x, y: x * y, [1, 2, 3, 4]) # 24# 更复杂的归约def factorial(n): return reduce(lambda a, b: a * b, range(1, n+1), 1) 掌握这些高效迭代技巧可以让你写出更Pythonic、性能更好的代码。记住,在Python中,显式的循环通常比隐式的循环慢,所以尽量使用内置函数和生成器表达式来处理迭代任务。 |
|