Help with cumulative sum
la.foma at gmail.com
Tue Sep 8 21:49:23 CEST 2009
On Sep 8, 3:29 pm, Maggie <la.f... at gmail.com> 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
> here is the code i have:
> 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,
> print 'The loop is finito'
> my test file is this
> 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!
was trying to plug in the sum for the loop..but for some reason it
doesnt want to work --
#name of output file
filename = "OUTPUT.txt"
#open the file
formisano = open ("test.txt", "rU")
#read in all the data into a list
readData = formisano.readlines()
sum = 0
count = 0
FILE = open(filename, "w")
for item in readData:
count = count + 1
sum = sum + (int(item) * int(item))
tmp_string = str(count) + ' ' + item + ' '+ sum
print >> FILE, tmp_string,
print 'The loop is finito'
More information about the Python-list