Help with cumulative sum

Maggie la.foma at gmail.com
Tue Sep 8 16:09:06 EDT 2009


On Sep 8, 4:05 pm, "J. Cliff Dyer" <j... at sdf.lonestar.org> wrote:
> If I gave you a list of numbers, could you come up with a summifier
> function that returns another list of numbers that are a cumulative sum?
> You've got the information in place to create a file
>
> def summifier(nums):
>     """Returns a list of numbers that are the running
>     sum totals of nums"""
>
>     # ???
>
> list_of_numbers = [1, 24, 34, 28, 4, 1]
> cumulative_sum = summifier(list_of_numbers)
> assert(cumulative_sum == [1, 25, 59, 87, 91, 92])
>
> If you can come up with the summifier function, you're all set.  I gotta
> say, though, this smells like homework.
>
> Cheers,
> Cliff
>
> On Tue, 2009-09-08 at 12:29 -0700, Maggie wrote:
> > Building on the code that I posted in one of the previous posts.. I
> > need to find a cumulative sum of the file of the times in the test
> > file:
>
> > here is the code i have:
>
> > #!/usr/bin/python
>
> > import os.path
>
> > #name of output file
> > filename = "OUTPUT.txt"
>
> > #open the file
> > test = open ("test.txt", "rU")
>
> > #read in all the data into a list
> > readData = test.readlines()
>
> > count = 0
>
> > FILE = open(filename, "w")
>
> > for item in readData:
>
> >    count = count + 1
> >    tmp_string = str(count) + '     ' + item
> >    print >> FILE, tmp_string,
>
> > else:
> >    print 'The loop is finito'
>
> > -----
>
> > my test file is this
>
> > 23
> > 241
> > 34234
> > 83
> > 123
>
> > and I need to find a CUMULATIVE sum (or the running sum)...what would
> > be the best way to go about that given the code i already have?
>
> > thank you all!
>
>

i WISH it would be homework! that way i can ask my professor and be
done with it. i need this code to pre-process fMRI data for my
research. and given i have never done python..i am at a loss...thanks
for your help i will try this right now..



More information about the Python-list mailing list