On 10/26/2010 1:20 PM masawudu bature said...
How do I sum all occurrences of  a number in a file?
I have a file with a bunch of numbers, each on it's own line:

5
3
11
3
7
3
5
5
11
7
7
...
How do i sum them up so that the output will be ,
5 :  15
3 :   9
11:  22
7 :   21



Well, you'll need to read the lines of the file in and group them together. Then, for each group, print both the number and the result when multiplied by the count.

To do this, you'll need the name of the file and use open to read it in. I'd suggest using a dict to accumulate the groups, and a loop to print the results.

What have you got so far?

Emile

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to