itertools.accumulate() versus functools.reduce()

 ̄綄美尐妖づ 提交于 2019-12-20 12:28:26

问题


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 means it now overlaps with functools.reduce(). With a cursory look, the main differences between the two now would seem to be:

  1. accumulate() defaults to summing but doesn't let you supply an extra initial condition explicitly while reduce() doesn't default to any method but does let you supply an initial condition for use with 1/0-element sequences, and
  2. accumulate() takes the iterable first while reduce() takes the function first.

Are there any other differences between the two? Or is this just a matter of behavior of two functions with initially distinct uses beginning to converge over time?


回答1:


It seems that accumulate keeps the previous results, whereas reduce (which is known as fold in other languages) does not necessarily.

e.g. list(accumulate([1,2,3], operator.add)) would return [1,3,6] whereas a plain fold would return 6

Also (just for fun, don't do this) you can define accumulate in terms of reduce

def accumulate(xs, f):
    return reduce(lambda a, x: a + [f(a[-1], x)], xs[1:], [xs[0]]) 



回答2:


You can see in the documentation what the difference is. reduce returns a single result, the sum, product, etc., of the sequence. accumulate returns an iterator over all the intermediate results. Basically, accumulate returns an iterator over the results of each step of the reduce operation.




回答3:


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.



来源:https://stackoverflow.com/questions/14132545/itertools-accumulate-versus-functools-reduce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!