栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

2021-09-30

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-09-30

生成器函数学习笔记
  • Fluent Python真的是一本经典之作。
    • 演示 itertools.accumulate 函数的几个用法
    • 演示用于映射的生成器函数
    • itertools.product 生成器函数

Fluent Python真的是一本经典之作。 演示 itertools.accumulate 函数的几个用法
>>> sample = [5, 4, 2, 8, 7, 6, 3, 0, 9, 1] 
>>> import itertools 
>>> list(itertools.accumulate(sample)) #  计算总和
  [5, 9, 11, 19, 26, 32, 35, 35, 44, 45]
>>> list(itertools.accumulate(sample, min))  # 计算最小值
  [5, 4, 2, 2, 2, 2, 2, 0, 0, 0] 
>>> list(itertools.accumulate(sample, max))  # 计算最大值
  [5, 5, 5, 8, 8, 8, 8, 8, 9, 9] >>> import operator
>>> list(itertools.accumulate(sample, operator.mul))  #   计算乘积
  [5, 20, 40, 320, 2240, 13440, 40320, 0, 0, 0] 
>>> list(itertools.accumulate(range(1, 11), operator.mul)) 
  [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] # 从 1! 到 10!,计算各个数的阶乘
演示用于映射的生成器函数
>>> list(enumerate('albatroz', 1)) #  从 1 开始,为单词中的字母编号。
  [(1, 'a'), (2, 'l'), (3, 'b'), (4, 'a'), (5, 't'), (6, 'r'), (7, 'o'), (8, 'z')] 
>>> import operator 
>>> list(map(operator.mul, range(11), range(11))) #  从 0 到 10,计算各个整数的平方。
  [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100] 
>>> list(map(operator.mul, range(11), [2, 4, 8])) # 计算两个可迭代对象中对应位置上的两个元素之积,元素最少的那 个可迭代对象到头后就停止。
  [0, 4, 16]
>>> list(map(lambda a, b: (a, b), range(11), [2, 4, 8])) # 作用等同于内置的 zip 函数。
  [(0, 2), (1, 4), (2, 8)] 
>>> import itertools 
>>> list(itertools.starmap(operator.mul, enumerate('albatroz', 1))) #  从 1 开始,根据字母所在的位置,把字母重复相应的次数。
  ['a', 'll', 'bbb', 'aaaa', 'ttttt', 'rrrrrr', 'ooooooo', 'zzzzzzzz'] 
>>> sample = [5, 4, 2, 8, 7, 6, 3, 0, 9, 1] 
>>> list(itertools.starmap(lambda a, b: b/a, enumerate(itertools.accumulate(sample), 1))) # 计算平均值。
  [5.0, 4.5, 3.6666666666666665, 4.75, 5.2, 5.333333333333333, 5.0, 4.375, 4.888888888888889, 4.5]
itertools.product 生成器函数
>>> list(itertools.product('ABC', range(2))) # 三个字符的字符串与两个整数的值域得到的笛卡儿积是六个元组 (因为 3 * 2 等于 6)。
  [('A', 0), ('A', 1), ('B', 0), ('B', 1), ('C', 0), ('C', 1)] 
>>> suits = 'spades hearts diamonds clubs'.split()
>>> list(itertools.product('AK', suits)) # 两张牌('AK')与四种花色得到的笛卡儿积是八个元组。
  [('A', 'spades'), ('A', 'hearts'), ('A', 'diamonds'), ('A', 'clubs'), ('K', 'spades'), ('K', 'hearts'), ('K', 'diamonds'), ('K', 'clubs')]
>>> list(itertools.product('ABC')) #  如果传入一个可迭代的对象,product 函数产出的是一系列只有一 个元素的元组,不是特别有用。
   [('A',), ('B',), ('C',)]
>>> list(itertools.product('ABC', repeat=2)) # repeat=N 关键字参数告诉 product 函数重复 N 次处理输入的各个 可迭代对象。
   [('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('C', 'A'), ('C', 'B'), ('C', 'C')]
>>> list(itertools.product(range(2), repeat=3)) 
  [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)] 
>>> rows = itertools.product('AB', range(2), repeat=2) 
>>> for row in rows: print(row)
 ... 
 ('A', 0, 'A', 0) 
 ('A', 0, 'A', 1) 
 ('A', 0, 'B', 0) 
 ('A', 0, 'B', 1) 
 ('A', 1, 'A', 0)
 ('A', 1, 'A', 1) 
 ('A', 1, 'B', 0) 
 ('A', 1, 'B', 1) 
 ('B', 0, 'A', 0) 
 ('B', 0, 'A', 1) 
 ('B', 0, 'B', 0) 
 ('B', 0, 'B', 1)
 ('B', 1, 'A', 0) 
 ('B', 1, 'A', 1) 
 ('B', 1, 'B', 0)
 ('B', 1, 'B', 1)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/313987.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号