分享

Python不允许你不知道的 9大高效迭代技巧

 网摘文苑 2025-05-16 发布于新疆

#每天一个编程技巧#掌握高效的迭代技巧可以显著提升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()并行迭代

names = ['Alice', 'Bob', 'Charlie']ages = [25, 30, 35]# 并行迭代多个序列for name, age in zip(names, ages):    print(f'{name} is {age} years old')# 处理不等长序列from itertools import zip_longestfor name, age in zip_longest(names, ages, fillvalue='Unknown'):    print(f'{name}: {age}')

2. 高级迭代技巧

2.1 使用itertools模块

from itertools import islice, cycle, repeat, chain# islice - 迭代切片for item in islice(range(100), 5, 10): # 获取5-9print(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): # 重复3print(item)# chain - 连接多个可迭代对象for item in chain([1, 2], ['a', 'b'], [True, False]): print(item)

2.2 使用生成器表达式

# 列表推导式(立即求值)squares = [x**2 for x in range(10000)]  # 占用内存# 生成器表达式(惰性求值)squares_gen = (x**2 for x in range(10000))  # 节省内存# 在函数中使用total = sum(x**2 for x in range(10000))  # 更高效

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 字典推导式

# 创建新字典squares = {x: x**2 for x in range(5)}# 交换键值inverted = {v: k for k, v in d.items()}# 条件筛选even_squares = {x: x**2 for x in range(10) if x % 2 == 0}

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 使用生成器处理文件

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. 条件迭代技巧

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()短路评估

# any - 任意元素为True时停止迭代has_positive = any(x > 0 for x in numbers)# all - 所有元素为True时返回Trueall_positive = all(x > 0 for x in numbers)

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多重循环

from itertools import product# 替代多重嵌套循环for x, y, z in product(range(2), range(2), range(2)):    print(x, y, z)  # 相当于三重嵌套循环# 计算笛卡尔积colors = ['red', 'green']sizes = ['S', 'M', 'L']for color, size in product(colors, sizes):    print(color, size)

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 使用局部变量加速访问

# 不推荐for item in large_list:    value = math.sqrt(item) + math.sin(item)# 推荐 - 局部变量访问更快sqrt = math.sqrtsin = math.sinfor item in large_list:    value = sqrt(item) + sin(item)

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 实现迭代器协议

class CountDown:    def __init__(self, start):        self.current = start        def __iter__(self):        return self        def __next__(self):        if self.current <= 0:            raise StopIteration        num = self.current        self.current -= 1        return numfor num in CountDown(5):    print(num)  # 5,4,3,2,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

from collections import defaultdict# 自动初始化字典值word_counts = defaultdict(int)for word in words:    word_counts[word] += 1  # 无需检查键是否存在# 分组功能groups = defaultdict(list)for item in data:    groups[item.category].append(item)

9.2 使用functools.reduce

from 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中,显式的循环通常比隐式的循环慢,所以尽量使用内置函数和生成器表达式来处理迭代任务。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多