Help with cumulative sum

M

Maggie

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!
 
M

Maggie

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!

---

was trying to plug in the sum for the loop..but for some reason it
doesnt want to work --

#!/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()

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,

else:
print 'The loop is finito'
 
M

Maggie

---

was trying to plug in the sum for the loop..but for some reason it
doesnt want to work --

#!/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()

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,

else:
   print 'The loop is finito'

sorry...typo in code:
tmp_string = str(count) + ' ' + str(sum) + ' ' + item

any suggestions are welcome
 
J

J. Cliff Dyer

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
 
M

Maggie

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

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:

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

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..
 
M

MRAB

Read the traceback.
#!/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()

sum = 0

Try to avoid using the names of builtin functions and classes, in this
case 'sum'.
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

You can't add a number to a string; a number is a number and a string is
a string! :)

tmp_string = str(count) + ' ' + item + ' '+ str(sum)
 
M

Maggie

Read the traceback.









Try to avoid using the names of builtin functions and classes, in this
case 'sum'.





You can't add a number to a string; a number is a number and a string is
a string! :)

     tmp_string = str(count) + '        ' + item + '    '+ str(sum)

I saw my mistake...now it is telling me the following --

Traceback (most recent call last):
File "formisano_count.py", line 22, in <module>
running_sum = running_sum + (int(item) * int(item))
ValueError: invalid literal for int() with base 10: ''
...
not sure what exactly i am doing wrong!
 
A

Andreas Waldenburger

As was suggested elsewhere: use enumerate here:

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

"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' 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
 
A

Andreas Waldenburger

[snip]
I saw my mistake...now it is telling me the following --

Traceback (most recent call last):
File "formisano_count.py", line 22, in <module>
running_sum = running_sum + (int(item) * int(item))
ValueError: invalid literal for int() with base 10: ''
..
not sure what exactly i am doing wrong!

What this error means is that the int() constructor is passed an empty
string (''). So there is probably an empty line in your input file.

You can catch these with a simple

if item:
# usual code goes here

/W
 
M

MRAB

Maggie said:
I saw my mistake...now it is telling me the following --

Traceback (most recent call last):
File "formisano_count.py", line 22, in <module>
running_sum = running_sum + (int(item) * int(item))
ValueError: invalid literal for int() with base 10: ''
..
not sure what exactly i am doing wrong!

You're trying to convert an empty string into an integer. Does the file
contain a blank line?
 
B

Bruno Desthuilliers

Maggie a écrit :

(snip - lots of answers and sensible suggestions already)
tmp_string = str(count) + ' ' + item

Mays I suggest you learn about string formatting ?
 
S

Steven D'Aprano

Maggie a écrit :

(snip - lots of answers and sensible suggestions already)


Mays I suggest you learn about string formatting ?


Which is generally good advice, but for a once-off simple concatenation
of three substrings, there's no great reason to prefer one over the
other. There's no difference in length of code, little difference in
readability, and concatenation is about 30% faster.

.... 'count = 2345; item = "abcde"').repeat()
[0.98372197151184082, 0.90344786643981934, 0.9030919075012207].... 'count = 2345; item = "abcde"').repeat()
[1.4281179904937744, 1.3027360439300537, 1.3032739162445068]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top