Help with cumulative sum

Andreas Waldenburger usenot at geekmail.INVALID
Tue Sep 8 16:25:09 EDT 2009


On Tue, 8 Sep 2009 12:49:23 -0700 (PDT) Maggie <la.foma at gmail.com>
wrote:

> 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
> > 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:
> >
As was suggested elsewhere: use enumerate here:

for count, item in enumerate(readData):
    tmp_string = str(count) + '  ' + item


> >    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!
> 
> ---
> 
> was trying to plug in the sum for the loop..but for some reason it
> doesnt want to work --
> 
"For some reason it doesnt want to work" is not a very useful
description. What do you expect? And more importantly: What do you
actually get? Output? Error message?

Anyway, I'll fire up my magic crystal ball. Let's see what I can do.


> #!/usr/bin/python
> 
> import os.path
> 
> #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()
> 
NB: You don't need to do this. Just iterate over formisano. A file is
its own iterator.


> sum = 0
'sum' is a builtin function. With the above statement you're shadowing
it. While it doesn't break anything here, it is generally considered
bad practice to shadow builtins, unless you want to purposefully shadow
it with something more useful than the builtin version.

Just saying.


> count = 0
> 
> FILE = open(filename, "w")
> 
I'm probably going overboard with suggestions now, but from Python 2.5
onwards you can use the 'with' statement to work on files. If nothing
else, it frees you from having to close the file manually. And it is
'pretty damn cool'™ too.

with open(filename, "w") as FILE:
	# Do your usual stuff here.


> for item in readData:
> 
>    count = count + 1
>    sum = sum + (int(item) * int(item))
>    tmp_string = str(count) + '	' + item + '	'+ sum
>    print >> FILE, tmp_string,
> 
> else:
>    print 'The loop is finito'

It works for me (after correcting your 'typo'). Was that the problem?
Or is there anything else left?

/W


-- 
INVALID? DE!




More information about the Python-list mailing list