itertools.accumulate() versus functools.reduce()

前端 未结 3 1493
礼貌的吻别
礼貌的吻别 2021-02-02 10:40

In Python 3.3, itertools.accumulate(), which normally repeatedly applies an addition operation to the supplied iterable, can now take a function argument as a parameter; this me

3条回答
  •  轮回少年
    2021-02-02 11:11

    itertools.accumulate is like reduce but returns a generator* instead of a value. This generator can give you all the intermediate step values. So basically reduce gives you the last element of what accumulate will give you.

    *A generator is like an iterator but can be iterated over only once.

提交回复
热议问题