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

  • 關於本站
  • 免責聲明