简体   繁体   中英

How to use initial argument inside itertools.accumulate?

In this documentation link we can read this:

itertools.accumulate(iterable[, func, *, initial=None])

Usually, the number of elements output matches the input iterable. However, if the keyword argument initial is provided, the accumulation leads off with the initial value so that the output has one more element than the input iterable.

However, I can not figure out how to use the initial argument.

If I use it like this:

accumulate([0, 7, 19, 13], operator.add,1)

I get error "TypeError: accumulate() takes at most 2 arguments (3 given)".

I am using Python 3.4.

If you look at the function signature, you notice the "*". This means anything after it should be provided as keyword arguments. So your call should be:

accumulate([0,7,19,13], operator.add, initial=1)

but you said you're using Python 3.4, then you will not have the initial argument as it is only provided in Python 3.8, as per the documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM