迭代协议
it = iter([1, 2, 3]) print(next(it)) # 1
生成器函数与表达式
def count_up_to(n): i = 1 while i <= n: yield i i += 1 nums = list(count_up_to(5))
管道式组合
total = sum(x*x for x in range(10) if x % 2 == 0)
转载请注明:周志洋的博客 » Python实用技巧-迭代器与生成器
it = iter([1, 2, 3]) print(next(it)) # 1
def count_up_to(n): i = 1 while i <= n: yield i i += 1 nums = list(count_up_to(5))
total = sum(x*x for x in range(10) if x % 2 == 0)
转载请注明:周志洋的博客 » Python实用技巧-迭代器与生成器