Re: Help with cumulative sum
Maggie la.f...@gmail.com writes: [...] else: print 'The loop is finito' do you know of it.comp.lang.python? -- Sarebbe essere un atto di pieta'. Contro i miei principi.-- whip, in IFMdI -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with cumulative sum
Maggie a écrit : (snip - lots of answers and sensible suggestions already) tmp_string = str(count) + ' ' + item Mays I suggest you learn about string formatting ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with cumulative sum
On Thu, 10 Sep 2009 09:55:29 +0200, Bruno Desthuilliers wrote: Maggie a écrit : (snip - lots of answers and sensible suggestions already) tmp_string = str(count) + ' ' + item 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. from timeit import Timer Timer('str(count) + + item', ... 'count = 2345; item = abcde').repeat() [0.98372197151184082, 0.90344786643981934, 0.9030919075012207] Timer('%d%s % (count, item)', ... 'count = 2345; item = abcde').repeat() [1.4281179904937744, 1.3027360439300537, 1.3032739162445068] -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with cumulative sum
On Thu, 10 Sep 2009 17:48:32 +0200 Giacomo Boffi giacomo.bo...@polimi.it wrote: Maggie la.f...@gmail.com writes: [...] else: print 'The loop is finito' do you know of it.comp.lang.python? Neat! They use computers in IT now? *flees, snickering* /W -- INVALID? DE! -- http://mail.python.org/mailman/listinfo/python-list
Help with cumulative sum
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! -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with cumulative sum
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! -- http://mail.python.org/mailman/listinfo/python-list