aze*_*zan 1 python python-itertools accumulate
在此文档链接中,我们可以阅读以下内容:
itertools.accumulate(iterable[, func, *, initial=None])
Run Code Online (Sandbox Code Playgroud)
通常,元素输出的数量与输入迭代匹配。但是,如果提供了关键字参数initial,则累积以初始值开始,因此输出的元素比输入的可迭代元素多一个。
但是,我无法弄清楚如何使用该initial参数。
如果我像这样使用它:
accumulate([0, 7, 19, 13], operator.add,1)
Run Code Online (Sandbox Code Playgroud)
我收到错误“TypeError:accumulate() 最多需要 2 个参数(给定 3 个)”。
我正在使用 Python 3.4。
如果您查看函数签名,您会注意到“*”。这意味着它之后的任何内容都应该作为关键字参数提供。所以你的电话应该是:
accumulate([0,7,19,13], operator.add, initial=1)
Run Code Online (Sandbox Code Playgroud)
但是您说您使用的是 Python 3.4,那么您将没有initial参数,因为它仅在 Python 3.8 中提供,根据文档。