[Python-ideas] Reduce/fold and scan with generator expressions and comprehensions
Rob Cliffe
rob.cliffe at btinternet.com
Tue Oct 25 13:55:25 EDT 2016
On 24/10/2016 06:11, Danilo J. S. Bellini wrote:
> For example, a product:
>
> >>> [prev * k for k in [5, 2, 4, 3] from prev = 1]
> [1, 5, 10, 40, 120]
>
> That makes sense for me, and seem simpler than:
>
> >>> from itertools import accumulate, chain
> >>> list(accumulate(chain([1], [5, 2, 4, 3]), lambda prev, k: prev * k))
> [1, 5, 10, 40, 120]
>
Well, if you want an instant reaction from someone skimming this thread:
I looked at the first example and couldn't understand it. Then I looked
at the second one, and could understand it (even though I may never have
used "chain" or heard of "accumulate"). Obviously your mileage varies.
Best wishes,
Rob Cliffe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161025/f83f2d27/attachment.html>
More information about the Python-ideas
mailing list