logo
  • 教程列表
  • SO官方文档
  • Itertools 模块
    • itertools.dropwhile
    • Itertools 模块中的组合方法
    • 使用函数对可迭代对象中的项进行分组
    • 拿一片发电机
    • 压缩两个迭代器直到它们都耗尽
    • itertools.product
    • itertools.count
    • itertools.takewhile
    • itertools.repeat
    • 在迭代中获得累积的数字总和
    • 循环遍历迭代器中的元素
    • 和 itertools.permutations
    • 将多个迭代器链接在一起
  1. StackOverflow 文档
  2. Python Language 教程
  3. Itertools 模块
  4. 在迭代中获得累积的数字总和

在迭代中获得累积的数字总和

Created: November-22, 2018

Python 3.x >= 3.2

accumulate 产生数字的累积和(或乘积)。

>>> import itertools as it
>>> import operator

>>> list(it.accumulate([1,2,3,4,5]))
[1, 3, 6, 10, 15]

>>> list(it.accumulate([1,2,3,4,5], func=operator.mul)) 
[1, 2, 6, 24, 120]
  • 循环遍历迭代器中的元素
  • itertools.repeat

Copyright © 2018. All right reserved

tastones.com 备案号:鲁ICP备18045372号-1

  • 关于我们
  • 免责声明