Path: g2news1.google.com!news1.google.com!goblin1!goblin3!goblin2!goblin.stu.neva.ru!feeder2.ecngs.de!ecngs!feeder.ecngs.de!news.osn.de!diablo2.news.osn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Duncan Booth Newsgroups: comp.lang.python Subject: Re: Accumulate function in python Date: 19 Jul 2010 17:51:34 GMT Lines: 20 Message-ID: References: <94c7c397-d644-4909-8b4b-709a9728901e@y21g2000pro.googlegroups.com> <1994a39d-eb84-42f1-a434-6123a100ef81@a4g2000prm.googlegroups.com> Reply-To: duncan.bo...@suttoncourtenay.org.uk Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: individual.net /Hn1OgVFQWKIFyIPgU99qw+SuwIp6JBeBZgO/HthKIjQqnoleU Cancel-Lock: sha1:I9HxxpdLPrQ+GGFHBBl6rIJsDWw= User-Agent: Xnews/2006.08.24 Hamster/2.1.0.11 X-Face: .C;/v...@2k.C(.1v-}d=`|7AQ-%,#A$0ZGtAkLPvuawAM>3#D,pXaAb31%(=Gn2ZZK/Z~fd0y4't5iKK~F":}F2*|\mQYX+BUr4ZM*|+`@o-TKzFGwsJnan{)*b~QJ-Fu^u'$$nYV dhruvbird wrote: > On Jul 19, 4:28 pm, Peter Otten <__pete...@web.de> wrote: >> dhruvbird wrote: >> >   I have a list of integers: x = [ 0, 1, 2, 1, 1, 0, 0, 2, 3 ] >> >   And would like to compute the cumulative sum of all the integers >> > from index zero into another array. So for the array above, I should >> > get: [ 0, 1, 3, 4, 5, 5, 5, 7, 10 ] >> >   What is the best way (or pythonic way) to get this. >> >> Homework? > > not really :) > > It's just that I was wondering if a built-in function for doing such > things (which I find myself doing increasingly with an explicit loop) > exists. > Why would you find yourself doing it more than once? Write it once in a function and then just re-use the code.